简体   繁体   English

为什么mysql_query()有时在快速执行的查询上需要0.1秒

[英]Why does mysql_query() sometimes require 0.1 of a second on a query that executes fast

I've noticed that sometimes mysql_query() on particular query in my script executes immediately and sometimes it takes (almost exactly) 0.1 of a second. 我注意到有时在脚本中对特定查询的mysql_query()立即执行,有时(几乎完全是)0.1秒。 I wrote a simple script to test it: 我编写了一个简单的脚本对其进行测试:

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 />';

And results are pretty unexpected: 结果非常出乎意料:

0.02919600 1282686965 0.02919600 1282686965
0.12934100 1282686965 0.12934100 1282686965
0.22935700 1282686965 0.22935700 1282686965
0.32934100 1282686965 0.32934100 1282686965
0.32985500 1282686965 0.32985500 1282686965

or, another time, 或者,再一次

0.43041500 1282687515 0.43041500 1282687515
0.52974500 1282687515 0.52974500 1282687515
0.53034800 1282687515 0.53034800 1282687515
0.53082400 1282687515 0.53082400 1282687515
0.63109600 1282687515 0.63109600 1282687515

Do you have any ideas why mysql_query() behaves like this? 您有什么主意,为什么mysql_query()会这样表现?

我想您观察到的效果是mysql查询缓存浮点不准确性的组合

这可能是服务器负载。

Five examples isn't enough to establish a benchmark. 有五个例子不足以建立基准。 Run it 100k times and take the average. 运行10万次,取平均值。 Then do it again and take the average. 然后再做一次并取平均值。 Compare those. 比较那些。 Things like system load, query cache, etc will still affect it, but 100k times will reduce some of that variation. 系统负载,查询缓存等仍然会影响它,但是100k次会减少这种变化。

My results are perfectly sensible... 我的结果非常明智...

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

Between 0.00001s and 0.1s you have to account for MySQL liftoff, Apache dancing around, crontab jumping in, whatever... 在0.00001和0.1s之间,您必须考虑MySQL提升,Apache跳舞,crontab跳入等等。

Try something like this. 尝试这样的事情。 Makes it easier to read. 使其更易于阅读。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM