使用 MySQL 的 PHP 很慢

2022-01-14 00:00:00 performance sql xampp php mysql

(重要)编辑 3: 单独运行 testajax2.php 而不 Ajax.持续时间大致相同,1.02-1.03 秒.所以我想这意味着问题出在 PHP-MySQL 或 XAMPP??

(IMPORTANT) EDIT 3: Running the testajax2.php by itself and not Ajax. The duration is about the same, 1.02-1.03 seconds. So I guess that means the problem is in PHP-MySQL or XAMPP??

当我通过 phpMyAdmin 查询运行它时,结果如下:显示行 0 -29(总共约 50 个.查询耗时 0.0015 秒).看来问题毕竟不在于 Ajax,而可能在于 PHP.我怎样才能解决这个问题?(我也刚刚编辑了问题标题.)

When I ran it through a phpMyAdmin query, here's the result: Showing rows 0 - 29 ( ~50 total. The query took 0.0015 seconds). It appears the problem lies not in Ajax after all, but perhaps in PHP. How can I fix this? (I've also just edited the question title.)

答案: 在位于 C:WindowsSystem32driversetc"的 hosts 文件中添加以下行

   127.0.0.1 localhost


之前的问题:

jQuery Ajax 是否正常,另一端的 SQL 查询具有 1 秒的最短持续时间?我试过 $.get, $.post,$.getjson,$.ajax({type:'POST'})$.ajax({type:'GET'}).正如我所说,这是最低限度的.甚至可能会变得更糟到大约 3 秒.我怀疑这是 SQL 查询,因为当我在 phpMyAdmin 中尝试它们时,结果非常快.

Is it normal for jQuery Ajax with SQL queries on the other side have the minimum duration of 1 second? I've tried $.get, $.post,$.getjson,$.ajax({type:'POST'}), $.ajax({type:'GET'}). As I've said, it's the minimum. It could get worse to about 3 seconds even. I doubt it's the SQL queries though, as when I try them in phpMyAdmin, the results come up extremely fast.

如果查询非常简单并且表只有 2 个元素也没关系,它仍然会遵循 1 秒的最小值.我用的是最新的XAMPP,不知道有没有关系,但是我是通过localhost和127.0.0.1访问文件的.

It doesn't matter also if the queries are very simple and the table only has 2 elements, it would still follow the 1 sec minimum. I'm using the latest XAMPP, and I don't know if it matters, but I'm accessing the files through localhost and 127.0.0.1.

我在本地环境中运行它,在我制作这些文件的同一台笔记本电脑上.jQuery 已更新.返回的值/数组是 json_encoded.我正在使用 mysqli.数据库在InnoDB中,里面只有五张左右的表,几乎什么都没有他们.这是一个非常简单的示例查询:

I'm running it on a local environment, on the same laptop I made these files on. jQuery is updated. The returned value/array is json_encoded. I'm using mysqli. The database is in InnoDB, and within are only about five tables, and there are hardly anything on them. Here's a very simple sample query:

    var test_id =2;
    testcall();
    function testcall(){
        $.ajax({
            url: 'testajax2.php',
            type: 'POST',
            data: {test_id: test_id},
            success: function(data){

            }
        });
    }

文件testajax2.php

    $mysqli = new mysqli('localhost', 'root', '', 'testdb');
    $mysqli->set_charset("utf8");
    if(mysqli_connect_errno()) {
        echo "Connection Failed: " . mysqli_connect_errno();
        exit();
    }

    $testreceive = $_POST['test_id'];
    $query = "SELECT First_Name from tblperson";
    $result = $mysqli->query($query);
    $row = $result->fetch_all(MYSQLI_ASSOC);

    echo json_encode($row);

tblperson 包含 50 条记录,并且只有四列.根据 Firebug 的说法,完成这个极其简单的任务需要 1.03 秒.我不确定它的真正含义,但通过 Net 选项卡查看nofollow noreferrer">Firebug,条形图完全是紫色的.0 和 1.03 秒等待.+1.03 秒和 0 接收.

The tblperson contains 50 records, and there are only four columns. According to Firebug, it took 1.03 seconds to do that extremely simple task. I'm not sure what it really means, but viewing through the Net tab in Firebug, the bar is entirely violet. 0 and 1.03 seconds Waiting. +1.03 seconds and 0 receiving.

如果我将它们发送为 json_encode($row)foreach($row as $value){ echo $value['First_Name'];}.它仍然至少需要 1 秒.我在 Chrome 和 Safari 上尝试过,虽然我无法确定确切的持续时间,但我可以说它大致相同.但是对于一个简单的 Ajax 调用,没有 SQL 查询.如果我没记错的话,速度非常快.我会带着样本和持续时间输出回来.

It also doesn't matter if I send them as json_encode($row) or foreach($row as $value){ echo $value['First_Name']; }. It would still be about at least 1 second. I've tried on Chrome and Safari, and though I can't have the exact duration, I can tell that it's about the same. But for a simple Ajax call with no SQL queries. If I remember correctly, it's very fast. I'd be back with a sample and duration output.

推荐答案

在位于"C:WindowsSystem32driversetc"的hosts文件中添加以下行

   127.0.0.1 localhost

这解决了我的问题.PHP-MySQL 现在可以完成其任务,具体取决于 20-500 ms 之间的复杂程度.

This solved my problem. PHP-MySQL can now complete its task depending on how complex somewhere between 20-500 ms.

相关文章