php mysql查询时间,如何从PHP获取MySQL查询的执行时间?

I execute MySQL queries from PHP and would like to know how time consuming they are. Is there any way to get the execution time of a MySQL query from PHP?

I also wonder if the execution time depends on how loaded is the web server. I can imagine that a query will take more time to execute if the server is busy with other queries. On the other hand, I can imagine that, if the server is busy, the query will just wait for its turn and then it will be executed (without any queries executed in parallel) and than the waiting time is not included into the execution time. So, what scenarios (out of two) is correct?

解决方案

There is probably some way to do this via MySQL, however, the easy (and reliable) way is using PHP's microtime function, which returns the current time as milliseconds.

microtime() returns the current Unix timestamp with microseconds. This function is only > available on operating systems that support the gettimeofday() system call.

getasfloat - When called without the optional argument, this function returns the string "msec sec" where sec is the current time measured in the number of seconds since the Unix Epoch (0:00:00 January 1, 1970 GMT), and msec is the microseconds part. Both portions of the string are returned in units of seconds.

If the optional get_as_float is set to TRUE then a float (in seconds) is returned.

Some example code:

$sql = '...';

$msc = microtime(true);

mysql_query($sql);

$msc = microtime(true)-$msc;

echo $msc . ' s'; // in seconds

echo ($msc * 1000) . ' ms'; // in millseconds

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值