简体   繁体   English

phpMyAdmin - 查询执行时间

[英]phpMyAdmin - Query Execution Time

When I execute a simple statement in phpMyAdmin like当我在 phpMyAdmin 中执行一个简单的语句时

SELECT *
FROM a

where "a" has 500'000 rows, it gives me a time of a few milliseconds on my localhost.其中“a”有 500'000 行,它在我的本地主机上给了我几毫秒的时间。

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.一些复杂查询报告的时间更长(如预期),但有些查询报告的时间也非常快 < 1/100 秒,但 phpMyAdmin 中的结果页面需要更长的时间才能显示。

So I'm unsure, is the displayed execution time really true and accurate in phpMyAdmin?所以我不确定,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:我认为从我自己的 PHP 脚本进行测试是个好主意,例如:

$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.与 phpMyAdmin 中报告的 0.005 秒相同语句的时间相比,这需要超过 7 秒。 The query returns 300'000 rows, if I restrict it to 50 with "LIMIT 0,50" it's under 1/100s.查询返回 300'000 行,如果我使用“LIMIT 0,50”将其限制为 50,则它低于 1/100s。 Where does that difference come from?这种差异从何而来? I don't iterate over the returned objects or something...我不迭代返回的对象或其他东西......

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.显示的执行时间是查询在服务器上运行所花费的时间——它是准确的,并且来自 MySQL 引擎本身。 Unfortunately, the results have to then be sent over the web to your browser to be displayed, which takes a lot longer.不幸的是,结果必须通过网络发送到您的浏览器才能显示,这需要更长的时间。

phpMyAdmin automatically appends a LIMIT clause to your statement, so it has a smaller result set to return thus making it faster. phpMyAdmin 会自动将LIMIT子句附加到您的语句中,因此它返回的结果集更小,从而使其更快。

Even if you need all 300,000 or 500,000 results then you should really use a LIMIT .即使您需要所有 300,000 或 500,000 个结果,您也应该真正使用LIMIT Multiple smaller queries does not necessarily mean same execution time as a single big query.多个较小的查询不一定意味着与单个大查询相同的执行时间。

Besides splash21's answer, it is a good idea to use SQL_NO_CACHE when testing for execution time.除了 splash21 的回答之外,在测试执行时间时使用 SQL_NO_CACHE 也是一个好主意。 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

No, phpMyAdmin is not telling the truth.不,phpMyAdmin 没有说实话。

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. ...并且 PMA 将报告(经过一些等待)查询花费了大约 0.0045 秒。 This is not the whole query time , this is the time of getting the first 25 hits.这不是整个查询时间,这是获得前 25 次点击的时间。 Or 50, or whatever you set the page size to.或 50,或任何您将页面大小设置为的值。 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;但是你会注意到它在 looong 秒后给你这个欺骗性的结果; it's because MySQL needs to really do the job, in order to determine which rows to return.这是因为 MySQL 需要真正完成这项工作,以确定要返回哪些行。 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.在相同条件下执行 count() 。

    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.您将得到一行告诉您总行数,自然,PMA 将显示所需的确切时间。 It has to, because now the first page and the whole result means the same thing.它必须,因为现在第一页和整个结果意味着同样的事情。

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

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