php - 为什么我的 AJAX GET 请求需要 2-3 秒,而相同的 MYSQL 命令行查询只需要不到 0.1 秒?

标签 php javascript ajax

从我的 Angular 来看,代码看起来相当高效,那么为什么 PHP 的加载时间超过 2 秒呢?

测试服务器,无负载

版本:

Apache/2.4.7(Ubuntu)

PHP/5.5.9-1ubuntu4.20

MYSQL 5.5.53-0ubuntu0.14.04.1(Ubuntu)

火狐50.0

Chrome 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 请求

火狐50.0 * 等待 2191 毫秒

Chrome 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";
}

?>

最佳答案

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

关于php - 为什么我的 AJAX GET 请求需要 2-3 秒,而相同的 MYSQL 命令行查询只需要不到 0.1 秒?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40951138/

相关文章:

PHP COM口与DIO的连接

php - 在多个 SQL 表中搜索特定行,然后返回行以及表名

php - 如何在html选项标签中添加php?

javascript - 在 adobe pro 中复制粘贴动态图章

javascript - 组合/绑定(bind)多个具有不同 id 或类的标签的更改、点击事件

javascript - 使用 jquery 或普通 js 更改 url

php - 带有准备语句的 PDO 动态查询

javascript - 使用 jquery 选择元素

javascript - 使用 Ractive.js 进行日期转换

javascript - JStree 异步搜索