mysql command line return execution time?

一个人想着一个人 提交于 2020-02-19 09:02:57

问题


I'm working on a Linux host with mysql command. 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.

Is there a way to get mysql exec time from the command line?

For example, in mysql interactive mode, execution result comes with a time, like this:

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


回答1:


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

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




回答2:


You can invoke mysql with -vv, it will pretty-print similar to when you're in interactive mode:

$ 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:

$ 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. :D




回答3:


Here is the exact syntax for 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';


来源:https://stackoverflow.com/questions/9388334/mysql-command-line-return-execution-time

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!