mysql报表查询优化

ztyzrc3y  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(230)

我有一个从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>
92dk7w1h

92dk7w1h1#

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

$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'";

相关问题