代码之家  ›  专栏  ›  技术社区  ›  Jules

如何在php中测量mysql时间、sql查询的时间和/或负载?

  •  7
  • Jules  · 技术社区  · 14 年前

    如果可能的话,我需要找到一些度量我的查询花费的时间和服务器上的负载的方法。

    我怀疑它的可能性,但我也希望获得cpu使用率。

    有什么想法吗?

    7 回复  |  直到 14 年前
        1
  •  21
  •   Alix Axel    14 年前

    PHP语法

    $starttime = microtime(); 
    $query = mysql_query("select * from table"); 
    $endtime = microtime();
    

    然后计算$starttime和$endtime之间的差异。

        2
  •  6
  •   Jaydee    8 年前

    查看查询探查器。这可以做你需要的查询时间位。

    https://www.digitalocean.com/community/tutorials/how-to-use-mysql-query-profiling

    https://dev.mysql.com/doc/refman/5.5/en/show-profile.html

    有各种工具可以查看服务器上的负载

        3
  •  2
  •   ajreal    14 年前

    由于数据库和web服务器的位置不同,几乎不可能
    另外,您还尝试使用PHP实现它

    限制

    • 您需要同时访问这两个服务器(如ssh)
    • 如果将CPU负载检查(如exec_shell ssh)嵌入数据库并返回 uptime 每次执行查询时

    解决办法

    在数据库服务器中嵌入cronjob,
    如果服务器负载过高,则定期发送电子邮件

    或者

    在数据库里,
    使用定期发送当前运行查询的电子邮件
    show full processlist

    存储SHOW FULL PROCESSLIST的示例

    $con = mysqli_connect(...) or die('unable to connect');
    $sql = "show full processlist";
    $res = mysqli_query($con, $sql) or die($sql);
    
    /* optional path is /tmp */
    $fp  = fopen('/tmp/'.date('YmdHis'), 'w+');
    while ($row = $res->fetch_row())
    {
      fputcsv($fp, $row);
    }
    $res->free_result();
    

    上述操作应该足以将当前mysql进程列表转储到文件中。
    在linux中,有很多命令允许用户显示CPU负载。
    但如果是windows,我想你可以在google上搜索一下

        4
  •  0
  •   Ivan    14 年前

    关于使用

    SHOW STATUS;
    

    查询。

    我认为最后一个查询成本变量对您的测量非常有用。以及显示运行时间超过一定时间的请求数量的慢速查询。这里是这个变量的完整列表 http://dev.mysql.com/doc/refman/5.1/en/server-status-variables.html 还有一个php函数 mysql_stat() 返回一些数据库使用数据的php函数。例如,每秒的查询可能有些有用。 但要获得更为确定的数据,需要ssh访问数据库。

        5
  •  0
  •   Quamis    14 年前

    使用 microtime 测量查询所需的时间。

    有关服务器负载,请参见 http://www.php.net/manual/en/function.sys-getloadavg.php . CPU使用率基本上是不相关的,因为mysql大部分时间都是磁盘绑定的。

        6
  •  0
  •   ts.    14 年前

    要获得更详细的统计信息,应该使用mysqli::get_connection_stats(如果使用php 5.3+)

    http://www.php.net/manual/en/mysqli.get-connection-stats.php

        7
  •  0
  •   Community CDub    13 年前

    PHP实际上只能计时 mysql_query() 执行时间,包括整个往返、延迟和传输时间。要进行分解,您需要使用前面提到的MySQL分析器。下面的代码应该输出您需要知道的信息。如果要将分析合并到PHP进程中,则必须使用分析程序并从 information_schema.profiling 表,但如果只是为了检查性能,下面的内容就足够了。

    <?php
    
    $con = mysql_connect("localhost","root","my-password");
    if (!$con)
    {
     die('Could not connect: ' . mysql_error());
    }
    
    mysql_select_db("my-database", $con);
    
    function microtime_float()
    {
     list($usec, $sec) = explode(" ", microtime());
     return ((float)$usec + (float)$sec);
    }
    
    mysql_query("set profiling=1;");
    
    $starttime = microtime_float();
    $query = mysql_query("SELECT * FROM my-table"); 
    $endtime = microtime_float();
    $trans_result = mysql_query("select sum(duration) as transtime from information_schema.profiling where query_id=1");
    $transtime = mysql_result($trans_result, 0, 'transtime');
    $total_time = ($endtime - $starttime); 
    $transtime = ($total_time - $transtime); 
    
    echo 'Total time: '.$total_time.' secs<br />';
    echo 'Transfer time: '.$transtime.' secs<br />';
    echo 'Query time break-down;<br />';
    
    $debug_result = mysql_query("show profile cpu for query 1;");
    while ($row = mysql_fetch_assoc($debug_result)) {
         echo $row['Status'].' (Time: '.$row['Duration'].', CPU_User: '.$row['CPU_user'].', CPY_sys: '.$row['CPU_system'].')<br />';    
    }
    ?>
    

    一旦您对性能满意,就删除所有需要的MySQL查询。