【问题标题】:Why does mysql_query() sometimes require 0.1 of a second on a query that executes fast为什么 mysql_query() 有时需要 0.1 秒来执行快速执行的查询
【发布时间】:2010-08-24 22:08:30
【问题描述】:

我注意到有时我脚本中特定查询的 mysql_query() 会立即执行,有时它需要(几乎完全)0.1 秒。我写了一个简单的脚本来测试它:

mysql_connect('<server>','<login>','<pass>');
mysql_select_db('<db>');

print microtime(true).'<br />';
mysql_query("select * from `messages` where `sq_id`=1");

print microtime(true).'<br />';
mysql_query("select * from `messages` where `sq_id`=1");

print microtime(true).'<br />';
mysql_query("select * from `messages` where `sq_id`=1");

print microtime(true).'<br />';
mysql_query("select * from `messages` where `sq_id`=1");

print microtime(true).'<br />';

结果出乎意料:

0.02919600 1282686965
0.12934100 1282686965
0.22935700 1282686965
0.32934100 1282686965
0.32985500 1282686965

或者,换个时间,

0.43041500 1282687515
0.52974500 1282687515
0.53034800 1282687515
0.53082400 1282687515
0.63109600 1282687515

你知道为什么mysql_query() 会这样吗?

【问题讨论】:

    标签: php mysql timing


    【解决方案1】:

    我猜你观察到的效果是the mysql query cachefloating-point inaccuracy 的组合。

    【讨论】:

    • 谢谢。确实,看起来mysql缓存与它有关。但是怎么会涉及浮点不准确性呢? Microtime() 提供比 0.1 秒更精确的结果。
    • 我的意思是我想这就是为什么最后 10 位小数似乎始终相同的原因 - 除非我以某种方式误解了输出。
    • 哦,最后 10 位数字代表整秒(在 PHP4 microtime() 中是这样的)。
    • 哦,好吧,这更有意义。
    【解决方案2】:

    可能是服务器负载。

    【讨论】:

      【解决方案3】:

      五个示例不足以建立基准。运行 100k 次并取平均值。然后再做一次并取平均值。比较那些。系统负载、查询缓存等仍然会影响它,但 10 万次会减少一些变化。

      【讨论】:

        【解决方案4】:

        我的结果非常合理...

        1 second + processing time is 1.0010678768158 seconds
        query in 0.00055694580078125 seconds
        query in 0.00095701217651367 seconds
        query in 0.0003049373626709 seconds
        query in 0.0012030601501465 seconds
        query in 0.0003972053527832 seconds
        

        在 0.00001s 和 0.1s 之间,您必须考虑 MySQL 启动、Apache 跳舞、crontab 加入等等......

        试试这样的。使其更易于阅读。

        $time_start = microtime(true);
        sleep(1);
        $time_end = microtime(true);
        $time = $time_end - $time_start;
        echo "1 second + processing time is actually $time seconds<br />\n";
        
        $time_start = microtime(true);
        mysql_query("select * from `messages` where `sq_id`=1");
        $time_end = microtime(true);
        $time = $time_end - $time_start;
        echo "query in $time seconds<br />\n";
        
        $time_start = microtime(true);
        mysql_query("select * from `messages` where `sq_id`=1");
        $time_end = microtime(true);
        $time = $time_end - $time_start;
        echo "query in $time seconds<br />\n";
        
        $time_start = microtime(true);
        mysql_query("select * from `messages` where `sq_id`=1");
        $time_end = microtime(true);
        $time = $time_end - $time_start;
        echo "query in $time seconds<br />\n";
        
        $time_start = microtime(true);
        mysql_query("select * from `messages` where `sq_id`=1");
        $time_end = microtime(true);
        $time = $time_end - $time_start;
        echo "query in $time seconds<br />\n";
        
        $time_start = microtime(true);
        mysql_query("select * from `messages` where `sq_id`=1");
        $time_end = microtime(true);
        $time = $time_end - $time_start;
        echo "query in $time seconds<br />\n";
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-01-28
          • 2019-07-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多