代码之家  ›  专栏  ›  技术社区  ›  Sandeepan Nath

获取PHP中最后一个查询的实际(绝对)执行时间(不包括网络延迟等)

  •  5
  • Sandeepan Nath  · 技术社区  · 14 年前

    我想得到实际的mysql查询执行时间,当它们在我的项目中运行时,所以运行php microtime() 前后和减法都不行。

    当我们在命令行中运行查询时,结果显示如下时间信息:

    xxx rows affected in YY sec
    

    如何使用PHP获取相同的时间信息。我查过了 PHP's Mysql functions 在另一个问题上 Here here 但没有得到类似的结果。没有这样的PHP函数吗 mysql_error() , mysql_affected_rows() 哪个返回其他重要信息?如果没有,为什么没有呢?有什么理由吗?

    有人说-

    我认为这是执行死刑的最好方法 时间就是使用一些程序执行 函数,如exec()或system()。

    有没有人有一个可以返回实际执行时间的有效PHP代码?

    1 回复  |  直到 14 年前
        1
  •  11
  •   ITroubs    14 年前

    试试这个:

    <?php
    
    $host='localhost';
    $username='testing';
    $password='testing';
    $dbname='test';
    
    $DBC = new mysqli($host,$username,$password,$dbname);
    
    $DBC->query('set profiling=1');
    $DBC->query('SELECT * FROM abc');
    if ($result = $DBC->query("SHOW profiles", MYSQLI_USE_RESULT)) {
    
    
        while ($row = $result->fetch_row()) {
            var_dump($row);
        }
        $result->close();
    }
    if ($result = $DBC->query("show profile for query 1", MYSQLI_USE_RESULT)) {
    
    
        while ($row = $result->fetch_row()) {
            var_dump($row);
        }
        $result->close();
    }
    $DBC->query('set profiling=0');
    
    ?>
    

    第一个if为您的查询提供如下总体执行时间:

    array(3) { [0]=>  string(1) "1" [1]=>  string(10) "0.00024300" [2]=>  string(17) "SELECT * FROM abc" }
    

    第二个if给出查询的详细执行时间。 结果应该是准确的,因为您使用的是MySQL内部分析器。