Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find time to execute MySQL query via PHP

Tags:

php

mysql

I've seen this question around the internet (here and here, for example), but I've never seen a good answer. Is it possible to find the length of time a given MySQL query (executed via mysql_query) took via PHP?

Some places recommend using php's microtime function, but this seems like it may be inaccurate. The mysql_query may be bogged down by network latency, or a sluggish system which isn't responding to your query quickly, or some other unrelated cause. None of these are directly related to the quality of your query, which is the only thing I really want to test out here. (Please mention in the comments if you disagree!)

like image 312
eykanal Avatar asked Feb 27 '26 15:02

eykanal


1 Answers

My answer is similar, but varied. Record the time before and after the query, but do it within your database query class. Oh, you say you are using mysql_query directly? Well, now you know why you should use a class wrapper around those raw php database functions (pardon the snark). Actually, one is already built called PDO:

https://www.php.net/pdo

If you want to extend the functionality to do timing around each of your queries... extend the class! Simple enough, right?

like image 94
Zak Avatar answered Mar 01 '26 03:03

Zak