mysql 执行时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1631316/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 14:22:01  来源:igfitidea点击:

mysql execution time

sqlmysqlexecution-time

提问by mck89

Is there a way to get the execution time of the last executed query in mysql?

有没有办法在mysql中获取上次执行查询的执行时间?

回答by soulmerge

mysql has a builtin profiler. You can enable profiling by issuing set profiling=1;and use show profiles;to get execution times.

mysql 有一个内置的分析器。您可以通过发出set profiling=1;和用于show profiles;获取执行时间来启用分析。

回答by Sabeen Malik

if using PHP .. you can use microtime() before the query and after the query to figure out how long it took for the query to execute.

如果使用 PHP .. 你可以在查询之前和查询之后使用 microtime() 来计算查询执行所需的时间。

$sql_query='SELECT * FROM table';
$msc=microtime(true);
$mysql_query($sql_query);
$msc=microtime(true)-$msc;
echo $msc.' seconds'; // in seconds
echo ($msc*1000).' milliseconds'; // in millseconds

回答by G-J

Try this...

尝试这个...

mysql_query("SET profiling = 1;");
if (mysql_errno()) { die( "ERROR ".mysql_errno($link) . ": " . mysql_error($link) ); }

/* It goes without saying that this is your actual query that you want to measure the execution time of */
$query="SELECT some_field_name FROM some_table_name";
$result = mysql_query($query);
if (mysql_errno()) { die( "ERROR ".mysql_errno($link) . ": " . mysql_error($link) ); }

$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';