Getting the actual (absolute) execution time of the last query in PHP (excluding network latency etc)

亡梦爱人 提交于 2019-12-03 20:15:40

问题


I want to get the actual Mysql query execution times while they run in my project so running PHP microtime() before and after and subtracting won't work.

When we run a query in command line, the result displays the timing information something like this:-

xxx rows affected in YY sec

How to get the same time information using PHP. I searched in PHP's Mysql functions in other SO question Here,and here but did not get anything similar. Is there no such PHP function like mysql_error(), mysql_affected_rows() which return other important information? If not why is it not there? Any reasoning?

Someone says -

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

Does anybody have a working PHP code which returns the actual execution time?


回答1:


try this:

<?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');

?>

the first if statement gives you the overall execution time for your query like this:

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

the second if statement gives you the detaild execution times of your query. The results should be exact since you are using the mysql internal profiler.



来源:https://stackoverflow.com/questions/4031934/getting-the-actual-absolute-execution-time-of-the-last-query-in-php-excluding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!