1

我注意到有时我脚本中特定查询的 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()会这样?

4

4 回答 4

1

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

于 2010-08-24T22:34:05.257 回答
0

可能是服务器负载。

于 2010-08-24T22:41:17.993 回答
0

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

于 2010-08-24T22:52:37.663 回答
0

我的结果是完全合理的......

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";
于 2010-08-25T03:26:30.787 回答