获取PHP中最后一个查询的实际(绝对)执行时间(不包括网络延迟等) [英] Getting the actual (absolute) execution time of the last query in PHP (excluding network latency etc)

查看:221
本文介绍了获取PHP中最后一个查询的实际(绝对)执行时间(不包括网络延迟等)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的项目中运行时获得实际的Mysql查询执行时间,所以在运行PHP之前和之后运行PHP microtime()都不起作用。 p>

当我们在命令行中运行查询时,结果会显示如下的时间信息: - $ /
$ b $ pre > xxx行在YY中受到影响

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



有人说 -


我认为执行$ b的最佳方式$ b时间是使用某些程序执行$ ​​b $ b函数,如exec()或system()。

有没有人有工作的PHP代码,它返回的实际执行时间?

解决方案

试试这个:

 <?php 

$ host ='localhost';
$ username ='testing';
$ password ='testing';
$ dbname ='test';

$ DBC = new mysqli($ host,$ username,$ password,$ dbname);

$ DBC-> query('set profiling = 1');
$ DBC->查询('SELECT * FROM abc');
if($ result = $ DBC-> query(SHOW profiles,MYSQLI_USE_RESULT)){


while($ row = $ result-> fetch_row() ){
var_dump($ row);
}
$ result-> close(); $

while($ row = $ DBC-> query(show profile for query 1,MYSQLI_USE_RESULT)){

$ b if $ result-> fetch_row()){
var_dump($ row);
}
$ result-> close();
}
$ DBC-> query('set profiling = 0');

?>

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

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

第二个if给你的detaild执行次数您的查询。
结果应该是精确的,因为您使用的是mysql内部分析器。


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?

解决方案

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 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 gives you the detaild execution times of your query. the results should be exact since you are using the mysql internal profiler.

这篇关于获取PHP中最后一个查询的实际(绝对)执行时间(不包括网络延迟等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆