当相同的MYSQL命令行查询花费少于0.1秒时,为什么我的AJAX GET请求需要2-3秒?

时间:2016-11-29 15:50:43

标签: php javascript ajax

从我的角度来看,代码看起来效率很高,那么为什么加载PHP需要2秒钟呢?

测试服务器,空载

版本:

Apache / 2.4.7(Ubuntu)

PHP / 5.5.9-1ubuntu4.20

MYSQL 5.5.53-0ubuntu0.14.04.1(Ubuntu)

Firefox 50.0

Chromium 53.0.2785.143

从mysql命令行:

SELECT company, customer_number, invoice_date, invoice_number, invoice_rep, item_number, item_description, item_qty, item_price FROM customer_data.invoices WHERE customer_number = "047811" ORDER BY invoice_date, invoice_number, item_number;

39行(0.01秒)

AJAX请求

Firefox 50.0 *等待2191毫秒

Chromium 53.0.2785.143 *等待2193毫秒

javascript函数:

function show_invoices(customer_number){
document.body.style.cursor = 'wait';
document.getElementById('float_panel1_body').innerHTML = "<h2 class='ticket_yellow'>Searching...</h2>";

var link = "http://ls1/portal/include/customer_data/CustomerData_invoices.php?customer_number=" + customer_number;
var xhttp = new XMLHttpRequest();

xhttp.open("GET", link, true);

xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
        search_results_raw = xhttp.responseText;

        if (search_results_raw == "false") {
            //document.getElementById('float_panel1_header').innerHTML = xhttp.responseText;
            //show_no_results();
        }else{
            //document.getElementById('float_panel1_header').innerHTML = xhttp.responseText;
            //show_results_invoices(xhttp.responseText);
            test_results(xhttp.responseText);
        }
    }
}
xhttp.send();

}

PHP代码:(CustomerData_invoices.php)

<?php
$cd_invoices = [];
$user="user";
$host="localhost";
$password="password";
$database="customer_data";

if ( !isset($_GET['customer_number']) ) {
    exit();
}
$customer_number = $_GET['customer_number'];

if (!$cxn = mysqli_connect($host,$user,$password,$database)) {
    $error = "SQL error in connecting to server. ";
    $error = $error.mysqli_error($cxn);
    echo "<strong>$error</strong>";
    //include 'backtohome.php';
    exit();
}
/// this is where the text is escaped for mysql
/// after mysqli_connect, but before mysqli_query
$customer_number_escaped = mysqli_real_escape_string($cxn, $customer_number);

// now setup query with escaped strings
$query="SELECT company, customer_number, invoice_date, invoice_number, invoice_rep, item_number, item_description, item_qty, item_price FROM customer_data.invoices WHERE customer_number = $customer_number_escaped ORDER BY invoice_date, invoice_number, item_number";

if (!$result = mysqli_query($cxn,$query)) {
    $error = "SQL error in query. ";
    $error = $error.mysqli_error($cxn);
    echo "<strong>$error</strong>";
    exit();
}

$returned = mysqli_affected_rows($cxn);
if ($returned > 0) {
    // record exist
    // temp change to array
    while($row = mysqli_fetch_assoc($result))
    {
        $cd_invoices[] = $row;
    }
    mysqli_close($cxn);
    echo json_encode($cd_invoices);
}else{
    echo "false";
}

?>

1 个答案:

答案 0 :(得分:1)

在确定执行时间时,MySQL查询不是唯一考虑的因素。它还取决于脚本的其他部分以及服务器的延迟。从Tracking the script execution time in PHP获取,您可以使用以下代码来确定脚本的执行时间:

// Script start
$rustart = getrusage();

// Code ...

// Script end
function rutime($ru, $rus, $index) {
    return ($ru["ru_$index.tv_sec"]*1000 + intval($ru["ru_$index.tv_usec"]/1000))
     -  ($rus["ru_$index.tv_sec"]*1000 + intval($rus["ru_$index.tv_usec"]/1000));
}

$ru = getrusage();
echo "This process used " . rutime($ru, $rustart, "utime") .
    " ms for its computations\n";
echo "It spent " . rutime($ru, $rustart, "stime") .
    " ms in system calls\n";