我想在我的项目中运行时获取实际的 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/