PHP Mysql查询优化报告

Posted

技术标签:

【中文标题】PHP Mysql查询优化报告【英文标题】:PHP Mysql query optimization for report 【发布时间】:2018-05-07 11:05:20 【问题描述】:

我有这份报告,它从 5 个表格中过滤数据,但速度非常慢,大约需要 10 秒。我尝试在某些列上使用索引,但没有帮助。

基本上第一个查询是主查询,其他查询用于过滤它,如果其他查询条件满足,它将跳过它。

这是脚本:

<div class="col-md-12">

    <h3>List of Outstandings</h3>

    <table class="table table-condensed table-bordered table-hover small">
        <thead>
            <tr>
                <th >#</th>
                <th>PR #</th>
                <th>PR Type</th>
                <th>Description</th>
                <th>Dep.</th>
                <th>Date</th>
                <th>Requester</th>
                <th>Assigned to</th>
            </tr>
        </thead>
        <tbody>
            <?php
            $chkk = 0;
            $sql = "SELECT * FROM msr WHERE Status ='Approved'  ";
            $result6 = $connn->query($sql);
            if ($result6->num_rows > 0) 
                $vo = 1;
                while ($row0 = $result6->fetch_assoc()) 
                    $chkk = 0;
                    $MSRID = $row0["MSRID"];
                    $MSRType = $row0["MSRType"];

                    $result4 = "SELECT owner FROM tracking WHERE MSRID='$MSRID' ";
                    $MyRow4 = $connn->query($result4);
                    $row4 = $MyRow4->fetch_assoc();
                    $actionBy = $row4["owner"];


                    $resultusr = "SELECT RFQID FROM rfq WHERE MSRID='$MSRID' AND NOPO='No'  ";
                    $MyRowusr = $connn->query($resultusr);
                    $rowusr = $MyRowusr->fetch_assoc();
                    $rfqcount = mysqli_num_rows($MyRowusr);
                    if ($rfqcount > 0) 
                        $chkk = 1;
                    

                    $resultusr4 = "SELECT POID FROM po WHERE MSRID='$MSRID'  ";
                    $MyRowusr4 = $connn->query($resultusr4);
                    $rowusr4 = $MyRowusr4->fetch_assoc();
                    $rfqcount4 = mysqli_num_rows($MyRowusr4);
                    if ($rfqcount4 > 0) 
                        $chkk = 1;
                    

                    $resultusr1 = "SELECT MSRID FROM contract WHERE MSRID='$MSRID'  ";
                    $MyRowusr1 = $connn->query($resultusr1);
                    $rowusr1 = $MyRowusr1->fetch_assoc();
                    $rfqcount1 = mysqli_num_rows($MyRowusr1);
                    if ($rfqcount1 > 0) 
                        $chkk = 1;
                    
                    if ($chkk == 1) 
                        continue;
                    
                    ?>
                    <tr>
                        <td>
                            <?php echo $vo; ?>
                        </td>
                        <td>
                            <?php echo $row0["MSRID"]; ?>
                        </td>
                        <td>
                            <?php echo $row0["MSRType"]; ?>
                        </td>
                        <td>
                            <?php echo $row0["purposeofbuying"]; ?>
                        </td>
                        <td>
                            <?php echo depName($row0["DepRequester"]); ?>
                        </td>
                        <td>
                            <?php echo $row0["RequestDate"]; ?>
                        </td>
                        <td>
                            <?php echo reqName($row0["RequestPer"]); ?>
                        </td>
                        <td>
                            <?php echo reqName($actionBy); ?>
                        </td>
                    </tr>
                    <?php
                    $vo++;
                
            
            ?>
        </tbody>
    </table>
</div>
</div>

【问题讨论】:

嗨 Pakanj 你能提供你的表结构吗? 【参考方案1】:

您可以使用子查询方法而不是循环。

例子:

 $sql = "SELECT  *,
        ( SELECT  owner
            FROM  tracking
            WHERE  tracking.MSRID= msr.MSRID
        ) AS _owner, 
        ( SELECT  RFQID
            FROM  rfq
            WHERE  rfq.MSRID= msr.MSRID
              AND  rfq.NOPO='No'
        )  AS _RFQID
    FROM  msr
    WHERE  rfq.Status ='Approved'";

【讨论】:

(第二个子查询缺少括号。)

以上是关于PHP Mysql查询优化报告的主要内容,如果未能解决你的问题,请参考以下文章

条件检查之间的Mysql中有多个条件?有没有办法优化这个查询?

大表连接的mysql查询优化

php+mysql 如何优化千万级数据模糊查询加快

php mysql 一个查询优化的简单例子

使用慢日志查询的 PHP Mysql 优化

如何在 mysql 或 php 中优化或加速我的子查询