How can I measure the execution time of a query without measuring the time it spends waiting for a lock release etc? My only idea was to continuously measure same query and record the fastest time.
2 Answers
Start the profiler with
SET profiling = 1;
Then execute your Query.
With
SHOW PROFILES;
you see a list of queries the profiler has statistics for. And finally you choose which query to examine with
SHOW PROFILE FOR QUERY 1;
or whatever number your query has.
What you get is a list where exactly how much time was spent during the query.
More info in the manual.
-
This is excellent! Do you know if this is slow? i.e. could I add this into my wrapper class, to execute for every query run on my website?– MikhailJun 30, 2012 at 18:43
-
3Never thought about that. Why would you want to do that? Usually this is just for development, I guess. If you want to know which queries are slow have a look in your slow-query-log. dev.mysql.com/doc/refman/5.1/en/slow-query-log.html Jun 30, 2012 at 18:56
-
Because queries take different amount of time for different users. slow-query-log seems to count the time it waits for a lock release– MikhailJun 30, 2012 at 23:03
-
1Not really true. Waiting for a lock is also just a symtom for another query being slow. I suggest, pick the queries in your slow-query-log, run them with the profiler to see where they spent so much time. Then, if possible, improve them. Add indexes accordingly, rewrite them, or reduce the data read if the query spends much time on
sending data
. Or whatever it takes to improve performance. I see no point in letting users know, why they have to wait so long. They don't care actually. Jul 1, 2012 at 9:42 -
1I'm enabling in-wrapper profiling for a select group of users. All the information from the profiling is saved in a table that I will check out in a week. Any query that has any of the timings over 0.2s will be examined and hopefully fixed up– MikhailJul 2, 2012 at 14:59
The accepted Answer is becoming invalid...
SHOW PROFILE[S]
are deprecated as of MySQL 5.6.7 and will be removed in a future MySQL release. Use the Performance Schema instead; see http://dev.mysql.com/doc/refman/5.6/en/performance-schema-query-profiling.html
-
2If Profile gives you anything more useful than "sending data", you win the lottery. Oct 10, 2017 at 23:47
-
1
-
2@SivaPraveen - OK, for now it still works, but "... will be removed ..." Dec 4, 2017 at 18:00
-
-