SQL太多了

时间:2017-08-16 17:03:44

标签: php sql-server wamp

由于某些原因,我的脚本需要花费太多时间,这就是为什么我必须添加ini_set('max_execution_time', 300);,因为30秒是默认的,这给我一个致命的错误。

我无法理解为什么会这样,如果我直接进入SSMS,我会在0秒内得到该查询。可能会发生什么?我正在使用php 5.4.16和扩展名php_sqlsrv_54_ts

运行
<?php
error_reporting(E_ALL ^ E_DEPRECATED);
ini_set('max_execution_time', 300);
include "ChromePhp.php";
$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'Cliente';
$order = isset($_POST['order']) ? strval($_POST['order']) : 'DESC';

include "includes/db_config.php";
$conn = sqlsrv_connect(SV_NAME, $connectionInfo) OR die("Unable to connect to the database");
$sql = 
"SELECT  *  FROM
(Select 
    Id
    ,Cliente
    ,Contrato
    ,Anexo
    ,SO
    ,NombreFlota
    ,(SELECT count(*) FROM LiveTest LEFT JOIN Producto ON Producto.Id=LiveTest.Producto_Id WHERE Producto.Order_Id=Orders.Id) as Hechas
    ,((SELECT count(*) FROM Producto WHERE Order_Id=Orders.Id) - (SELECT count(*) FROM LiveTest LEFT JOIN Producto ON Producto.Id=LiveTest.Producto_Id WHERE Producto.Order_Id=Orders.Id and RMA is  null )) as Pendientes
    ,(SELECT count(*) FROM Producto WHERE Order_Id=Orders.Id ) as Total
FROM
    Orders
WHERE 
    Orders.FechaPick is not null) as A
WHERE Total - Pendientes >0
ORDER BY $sort $order";
ChromePHP::log($sql);     
$params = array();
$options =  array( "Scrollable" => SQLSRV_CURSOR_KEYSET );
$result = array();  
$res = sqlsrv_query($conn, $sql, $params, $options);   
while($row = sqlsrv_fetch_object($res))            
{
    array_push($result, $row);
}   
ChromePHP::log($result);
echo json_encode($result);

&GT;

1 个答案:

答案 0 :(得分:0)

尝试像这样修改您的查询:

注意:你做的是f0b.Order_Id = f1.Id&#39;然后你的左边的连接被取消,因为你测试了一个值(这就是为什么我用内连接替换了),如果你也想要空值,你必须这样做:&#39;其中f0b.Order_Id = f1。 Id或f0b.Order_Id为空&#39;

[Test]
public void Index_ReturnCorrectView()
{
    var controller = new HomeController();
    var result = controller.Index("COMD") as ViewResult;
    Assert.AreEqual("Index", result.ViewName); 
}