PHP SQLSRV查询速度很慢

时间:2012-12-03 20:33:15

标签: php sql

使用完整功能代码更新

我在IIS 7.5上运行PHP以连接到另一台服务器托管的SQL Server数据库......

我的网页完全正常工作但执行这样的简单查询需要很长时间...每个循环只返回(12 * 3)* n个结果,循环在foreach“servicio_id”中给出,其中n =我有多少服务...一个简单的页面必须返回我:((12 * 3)* 14)* 3结果......我会解释: ((12 * 3)*服务)*表格数量(Banksphere和Umbrales * 2,就像你会看到......)

我认为问题存在于表中,因为我有近200万行,而且每天增加大约20.000行...但我认为运行查询不会花那么长时间,因为我指定了参数比如entidad_id,servicio_id,peticion_id,fecha ...它需要20-30秒才能显示我的页面,有时它会停止并只显示其中的一半......请一些建议吗?

public static function getValues($entidad_id, $servicio_id, $peticion_id, $fecha) {

    if(date('d', strtotime($fecha)) >= '28' || date('d', strtotime($fecha)) <= '1') {
        $dia_id = 8;
    } else {
        $dia_id = date('N', strtotime($fecha)) -1;
    }
    echo '<script type="text/javascript" language="javascript"> 
    function openwindow(URL) {
        window.open(URL,"Comentarios","menubar=1,resizable=1,width=700,height=350"); 
    }
    </script>'; 

    $conn = sqlsrv_connect(DB_SERVER.', '.DB_PORT, array("UID"=>DB_USER,  "PWD"=>DB_PASS,  "Database"=>DB_NAME));

    $sqlQuery = sqlsrv_query($conn, "
        SELECT TOP ".OPT_RESULTADOS_MAXIMOS." *
        FROM Banksphere
        WHERE entidad_id = '$entidad_id'
        AND servicio_id = '$servicio_id'
        AND peticion_id = '$peticion_id'
        AND fecha = '".$fecha."'
        ORDER BY hora_id DESC
    ");

    while ($row = sqlsrv_fetch_array($sqlQuery)) {  
        $umbralesQuery = sqlsrv_query($conn, "
            SELECT *
            FROM Umbrales
            WHERE entidad_id = '$row[entidad_id]'
            AND servicio_id = '$row[servicio_id]'
            AND peticion_id = '$row[peticion_id]'
            AND dia_id = '$row[dia_id]'
            AND hora_id = '$row[hora_id]'
            ORDER BY hora_id DESC
        "); 
        $umbralesOk = sqlsrv_query($conn, "
            SELECT *
            FROM Umbrales
            WHERE entidad_id = '$row[entidad_id]'
            AND servicio_id = '$row[servicio_id]'
            AND peticion_id = '0'
            AND dia_id = '$row[dia_id]'
            AND hora_id = '$row[hora_id]'
            ORDER BY hora_id DESC
        "); 
        $umbrales = sqlsrv_fetch_array($umbralesQuery);
        $uOK = sqlsrv_fetch_array($umbralesOk);
        sqlsrv_free_stmt($umbralesQuery);
        sqlsrv_free_stmt($umbralesOk);
        $medias = Umbrales::getValues($uOK['minimo'], $umbrales['minimo']);
        if($row['usuario'] != '') $postedby = 'Comentado por '.$row['usuario'].': '; else $postedby = '';

        if($peticion_id == 0) {
            if($row['valor'] < $medias[0]) {
                $color = "D";
            } else if($row['valor'] > $medias[1]) {
                $color = "A";
            } else {
                $color = "OK";
            }
            $minimo = $medias[0];
            $maximo = $medias[1];
        }
        if($peticion_id == 1) {
            if($row['valor'] > $medias[2]) {
                $color = "D";
            } else {
                $color = "OK";
            }
            $minimo = 0;
            $maximo = $medias[2];
        }
        if($peticion_id == 2) {
            if($row['valor'] > $medias[3]) {
                $color = "D";
            } else {
                $color = "OK";
            }
            $minimo = 0;
            $maximo = $medias[3];
        }           
        if($color == "OK"){
            echo '
            <td title="MINIMO: '.$minimo.' | MAXIMO: '.$maximo.'" class="'.$color.'">'.round($row['valor'], 3).'</td>
            ';
        } else {
            echo '
            <td title="MINIMO: '.$minimo.' | MAXIMO: '.$maximo.'" class="'.$color.'">
                <a href="javascript:openwindow(\'/includes/commentForm.php?servicio='.$umbrales['servicio_id'].'&umbral_id='.$umbrales['id'].'&id='.$row['id'].'\')">'.round($row['valor'], 3).'</a>
            </td>
            ';
        }
    }
    sqlsrv_free_stmt($sqlQuery);
    sqlsrv_close($conn);
}

2 个答案:

答案 0 :(得分:1)

你能在第一个查询中做一个包含Umbrales表的查询吗?你正在这样做的方式是为第一个查询得到的每一行调用一个事务。另一种方式是你只对结果进行一次交易。

答案 1 :(得分:0)

得到解决方案......现在可以接受速度提升......

    $sqlQuery = sqlsrv_query($conn, "
        SELECT TOP ".OPT_RESULTADOS_MAXIMOS."
            bs.id AS valor_id,
            bs.valor AS valor,
            bs.servicio_id,
            u.id AS umbral_id,
            u.minimo AS media,
            uok.minimo AS media_ok
        FROM
            [Banksphere] bs
        INNER JOIN
            Umbrales u
        ON
            u.entidad_id = bs.entidad_id
        AND
            u.servicio_id = bs.servicio_id
        AND
            u.dia_id = bs.dia_id
        AND
            u.hora_id = bs.hora_id
        AND
            u.peticion_id = bs.peticion_id
        INNER JOIN
            Umbrales uok
        ON
            uok.entidad_id = bs.entidad_id
        AND
            uok.servicio_id = bs.servicio_id
        AND
            uok.dia_id = bs.dia_id
        AND
            uok.hora_id = bs.hora_id
        AND
            uok.peticion_id = '0'
        WHERE
            bs.entidad_id = '$entidad_id'
        AND
            bs.servicio_id = '$servicio_id'
        AND
            bs.peticion_id = '$peticion_id'
        AND
            bs.fecha = '$fecha'
        ORDER BY
            bs.hora_id DESC,
            bs.peticion_id ASC
    ");