Raphael Jeger
Raphael Jeger

Reputation: 5232

phpMyAdmin - Query Execution Time

When I execute a simple statement in phpMyAdmin like

SELECT *
FROM a

where "a" has 500'000 rows, it gives me a time of a few milliseconds on my localhost.

Some complex queries report times that are way longer (as expected), but some queries report also very fast times < 1/100s but the result page in phpMyAdmin takes way longer to display.

So I'm unsure, is the displayed execution time really true and accurate in phpMyAdmin? How is it measured? Does it measure the whole query with all subselects, joins etc?

Thanks!

UPDATE

I thought it'd be a good idea to test from my own PHP-script like:

$start = microtime(true);
$sql = "same statement as in phpMyAdmin";
$db = new PDO('mysql:host=localhost;dbname=mydb', 'root', 'lala');
$statement = $db -> prepare($sql);
$statement -> execute();
echo microtime(true) - $start . ' seconds';

and that takes more than 7 seconds compared to a reported time in phpMyAdmin for the same statement of 0.005s. The query returns 300'000 rows, if I restrict it to 50 with "LIMIT 0,50" it's under 1/100s. Where does that difference come from? I don't iterate over the returned objects or something...

Upvotes: 2

Views: 13229

Answers (4)

Phil W.
Phil W.

Reputation: 91

Besides splash21's answer, it is a good idea to use SQL_NO_CACHE when testing for execution time. This makes sure that you are looking at the real time to do the query, not just grabbing a cached result.

SELECT SQL_NO_CACHE *
FROM a

Upvotes: 2

dkellner
dkellner

Reputation: 9936

No, phpMyAdmin is not telling the truth.

Imagine you have a pretty big table, let's say a million rows. Now you do a select, something you know is going to take a while:

    SELECT * FROM bigTable WHERE value > 1234

...and PMA will report (after some waiting) that the query took some 0.0045 seconds. This is not the whole query time, this is the time of getting the first 25 hits. Or 50, or whatever you set the page size to. So it's obviously fast - it stops as soon as you get the first screenful of rows. But you will notice that it gives you this deceptive result after looong seconds; it's because MySQL needs to really do the job, in order to determine which rows to return. It runs the whole query, and then it takes another look and returns only a few rows. That's what you get the time of.

How to get the real time?

Do a count() with the same conditions.

    SELECT COUNT(1) FROM bigTable WHERE value > 1234

You will get ONE row telling you the total number of rows, and naturally, PMA will display the exact time needed for this. It has to, because now the first page and the whole result means the same thing.

Upvotes: 0

Kim
Kim

Reputation: 2787

phpMyAdmin automatically appends a LIMIT clause to your statement, so it has a smaller result set to return thus making it faster.

Even if you need all 300,000 or 500,000 results then you should really use a LIMIT. Multiple smaller queries does not necessarily mean same execution time as a single big query.

Upvotes: 1

splash21
splash21

Reputation: 809

The displayed execution time is how long the query took to run on the server - it's accurate and comes from the MySQL engine itself. Unfortunately, the results have to then be sent over the web to your browser to be displayed, which takes a lot longer.

Upvotes: 3

Related Questions