【问题标题】:mysql execution timemysql执行时间
【发布时间】:2009-10-27 14:46:10
【问题描述】:

有没有办法获取mysql中最后一次执行查询的执行时间?

【问题讨论】:

    标签: sql mysql execution-time


    【解决方案1】:

    mysql 有一个builtin profiler。您可以通过发出set profiling=1; 来启用分析,并使用show profiles; 来获取执行时间。

    【讨论】:

    【解决方案2】:

    如果使用 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
    

    【讨论】:

    • 这并没有告诉你 MySQL 执行查询需要多长时间。它告诉您 PHP 发送请求、等待接收请求以及接收结果所用的时间。它会因 PHP 客户端上可能运行的任何东西而减慢,如果 PHP 客户端和 MySQL 服务器之间存在任何延迟,则结果将进一步不准确。
    • 我得到了奇怪的结果,有时它报告2.15 s 8000 行,其他时候报告1,503,023,491.52 s 6000 行。如果我申请number_format($msc,2),那么我会得到像负数这样的陌生事件结果。
    【解决方案3】:

    试试这个...

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

    【讨论】:

    • 这被标记为 MySQL 问题。不是每个使用 MySQL 的人都在使用 PHP。
    猜你喜欢
    • 1970-01-01
    • 2011-10-08
    • 2018-01-19
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 2012-03-12
    • 2012-03-11
    • 2020-11-15
    相关资源
    最近更新 更多