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

标签 php mysql function execution-time

我想在我的项目中运行时获取实际的 Mysql 查询执行时间,因此在前后运行 PHP microtime() 并减去将不起作用。

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

xxx rows affected in YY sec

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

有人说——

I think that best way to get execution time is to use some program execution functions like exec() or system().

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

最佳答案

试试这个:

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

关于php - 获取 PHP 中最后一个查询的实际(绝对)执行时间(不包括网络延迟等),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4031934/

相关文章:

php - C中的多线程程序 : calculating thread stack space

php - 如何在PHP中有效防止跨站请求伪造(CSRF)

c# - 如何在 C# 中编写自定义 "textChanged"函数?

javascript - 使用两个选择控件编辑表单,以便在首次启动时进行过滤

php - 在子域上实现 Facebook Connect

SQL 从列表中过滤项目

php - 复选框来自 mysql 循环的数组项目未发布到邮件页面

mysql - React Native 使用 axios 获取数据返回源过早耗尽

python - 全局名称未定义错误

PHP 为 mysql 交互创建函数还是每次都使用纯代码?