【问题标题】:mysql command line return execution time?mysql命令行返回执行时间?
【发布时间】:2012-03-12 09:35:11
【问题描述】:

我正在使用 mysql 命令在 Linux 主机上工作。我有一个运行批处理 mysql 命令的脚本(如mysql -e "select..."),我希望总结每个命令的执行时间。

有没有办法从命令行获取 mysql 执行时间?

例如在mysql交互模式下,执行结果带有时间,像这样:

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

我可以在命令行中获得相同的配置文件吗?

谢谢

【问题讨论】:

  • 在命令前抛出time 公平吗?或者您想要 MySQL 在计算结果时消耗的真实执行时间量吗?
  • @sarnold 是的,我希望看到“实时”。

标签: mysql profiling


【解决方案1】:

你可以使用

set profiling=1

然后,稍后,

show profiles

这将给出命令和时间的列表。

http://dev.mysql.com/doc/refman/5.0/en/show-profiles.html

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

【讨论】:

    【解决方案2】:

    您可以使用-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
    

    如果您是通过管道查询,那么它是-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
    

    时间是你的 grep。 :D

    【讨论】:

    • 这个答案在使用支持mysql tcp有线格式的ClickHouse的mysql客户端时特别好。原因是set profiling=1(如已接受的答案中所建议)在 ClickHouse 上不起作用。公平地说,最初的问题是关于将客户端与 MySQL 服务器一起使用,而不是与碰巧与 mysql 线路兼容的不同 DB 服务器一起使用。但是,对于希望将 mysql 客户端与 ClickHouse 或可能与 mysql 线路兼容的其他 DB 一起使用的任何人,这是最好的选择。
    【解决方案3】:

    这是 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';
    

    【讨论】:

      猜你喜欢
      • 2011-12-11
      • 1970-01-01
      • 2016-04-12
      • 1970-01-01
      • 1970-01-01
      • 2016-07-02
      • 1970-01-01
      • 2011-11-09
      • 1970-01-01
      相关资源
      最近更新 更多