This question already has an answer here:
- mysql execution time 3 answers
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?
microtime()
takes time to execute itself. If you want to get the data straight from mysql, do this...To get it right out of MySQL you can use the general log.
If its not already on, turn it on:
To see the data after you've run your query:
Among other things, you will get the query execution time, just like you've probably seen in phpmyadmin.
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.Some example code: