简体   繁体   English

mysql命令行返回执行时间?

[英]mysql command line return execution time?

I'm working on a Linux host with mysql command. 我正在使用mysql命令在Linux主机上工作。 I have a script that runs batch mysql commands (like mysql -e "select..." ) and I wish to summarize execution time of each of the commands. 我有一个运行批处理mysql命令的脚本(如mysql -e "select..." ),我希望总结每个命令的执行时间。

Is there a way to get mysql exec time from the command line? 有没有办法从命令行获取mysql exec时间?

For example, in mysql interactive mode, execution result comes with a time, like this: 例如,在mysql交互模式下,执行结果带有时间,如下所示:

mysql> select count(*) from trialtable;
+----------+
| count(*) |
+----------+
|     4000 |
+----------+
1 row in set (0.00 sec)

Can I get the same profile in command line? 我可以在命令行中获得相同的配置文件吗?

Thank you 谢谢

You can use 您可以使用

set profiling=1

and then, later, 然后,

show profiles

which will give a list of commands and times. 这将给出一个命令和时间列表。

See http://dev.mysql.com/doc/refman/5.0/en/show-profiles.html 请参阅http://dev.mysql.com/doc/refman/5.0/en/show-profiles.html

h/t http://ronaldbradford.com/blog/timing-your-sql-queries-2010-07-07/ h / t http://ronaldbradford.com/blog/timing-your-sql-queries-2010-07-07/

You can invoke mysql with -vv , it will pretty-print similar to when you're in interactive mode: 您可以使用-vv调用mysql,它将与您处于交互模式时类似:

$ mysql -vv -u myUser -pMyPass DBname -e 'select count(*) from mytable;'
--------------
select count(*) from mytable
--------------

+----------+
| count(*) |
+----------+
|  1068316 |
+----------+
1 row in set (0.00 sec)

Bye

If you're piping your queries, then it's -vvv : 如果您正在查询,那么它是-vvv

$ echo 'select count(*) from mytable;' | mysql -vvv -u myUser -pMyPass DBname
--------------
select count(*) from mytable
--------------

+----------+
| count(*) |
+----------+
|  1068316 |
+----------+
1 row in set (1.34 sec)

Bye

Time's yours to grep. 时间是你的grep。 :D :d

Here is the exact syntax for PHP. 这是PHP的确切语法。

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

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

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

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