php 在Query中获取MYSQL查询执行时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16434879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
To get MYSQL query execution time in Query
提问by DonOfDen
I am developing a query browser, working with some large database and I need to know the time taken for executing the query.
我正在开发一个查询浏览器,使用一些大型数据库,我需要知道执行查询所花费的时间。
I can see the time once the query is executed succesfully in PHPMYADMIN.
在 PHPMYADMIN 中成功执行查询后,我可以看到时间。
Profiling or Showing rows 0 - 29 (2,000 total, Query took 0.0145 sec)
分析或 Showing rows 0 - 29 (2,000 total, Query took 0.0145 sec)
Ex: Profiling
例如:分析
SELECT * FROM `larger_table`;
Status Time
starting 0.000026
checking permissions 0.000006
Opening tables 0.000014
System lock 0.000010
init 0.000022
optimizing 0.000004
statistics 0.000007
preparing 0.000005
executing 0.000001
Sending data 0.014138
end 0.000004
query end 0.000002
closing tables 0.000005
freeing items 0.000091
logging slow query 0.000003
cleaning up 0.000002
In Query Browser I can see the time it takes in the bottom of the browser window.
在查询浏览器中,我可以在浏览器窗口的底部看到它花费的时间。
So how can I get the execution time of the query when submit the query for execution.
那么如何在提交查询执行时获取查询的执行时间。
i.e When I give the following query:
即当我给出以下查询时:
SELECT * FROM `larger_table`;
The query should return the time for execution it.
查询应该返回执行它的时间。
This I should catch in PHP and show to the Users.When the user gives some query to executed in browser.
这我应该在 PHP 中捕获并显示给用户。当用户给出一些在浏览器中执行的查询时。
Is there any way to find the execution time when the query is submitted.?
有什么办法可以找到提交查询时的执行时间。?
Kindly check the Images where I marked the estimated time.
请检查我标记估计时间的图像。
I found some thing Check this
我发现了一些东西检查这个
回答by kapil
Please use the code below
请使用下面的代码
$sql_query = 'SELECT * FROM larger_table';
$msc = microtime(true);
mysql_query($sql_query);
$msc = microtime(true) - $msc;
echo $msc . ' seconds'; // in seconds
echo ($msc * 1000) . ' milliseconds'; // in millseconds
回答by Abhijit
Check out below -
看看下面——
$sql_query='SELECT * FROM tablename';
$result = mysql_query($query);
$exec_time_result=mysql_query("SELECT query_id, SUM(duration) FROM information_schema.profiling GROUP BY query_id ORDER BY query_id DESC LIMIT 1;");
if (mysql_errno()) { die( "ERROR ".mysql_errno($link) . ": " . mysql_error($link) ); }
$exec_time_row = mysql_fetch_array($exec_time_result);
echo "<p>Query executed in ".$exec_time_row[1].' seconds';
OutPut - Query executed in 0.000121 seconds
OutPut - 查询在 0.000121 秒内执行