6

我想在我的项目中运行它们时获得实际的 Mysql 查询执行时间,因此在运行 PHPmicrotime()之前和之后并且减去将不起作用。

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

xxx rows affected in YY sec

如何使用 PHP 获取相同的时间信息。我在其他 SO 问题Herehere中搜索了PHP 的 Mysql 函数,但没有得到任何类似的东西。有没有这样的 PHP 函数,它返回其他重要信息?如果不是,为什么它不存在?有什么道理吗?mysql_error()mysql_affected_rows()

有人说——

我认为获得执行时间的最佳方法是使用一些程序执行函数,如 exec() 或 system()。

有人有返回实际执行时间的有效 PHP 代码吗?

4

1 回答 1

12

试试这个:

<?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 内部分析器。

于 2010-10-27T10:26:41.030 回答