tags:

views:

37

answers:

4

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. 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.12934100 1282686965
0.22935700 1282686965
0.32934100 1282686965
0.32985500 1282686965

or, another time,

0.43041500 1282687515
0.52974500 1282687515
0.53034800 1282687515
0.53082400 1282687515
0.63109600 1282687515

Do you have any ideas why mysql_query() behaves like this?

+1  A: 

I would guess the effect your observe is a combination of the mysql query cache and floating-point inaccuracy.

Hammerite
Thank you. Indeed, it looks like mysql cache has something to do with it. But how could floating-point inaccuracy be involved? Microtime() gives much more precise results than 0.1 sec.
Danylo Mysak
I just mean that I imagine that's why the last 10 decimal places seem to be persistently the same - unless I'm misinterpreting the output somehow.
Hammerite
Oh, last 10 digits represent the whole seconds (in PHP4 microtime() works like this).
Danylo Mysak
Oh OK, that makes more sense.
Hammerite
A: 

It could be the server load.

aromawebdesign.com
A: 

Five examples isn't enough to establish a benchmark. Run it 100k times and take the average. 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.

CaseySoftware
A: 

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...

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