82

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 2

156

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.

10
  • 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?
    – Mikhail
    Jun 30, 2012 at 18:43
  • 3
    Never 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
    – fancyPants
    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
    – Mikhail
    Jun 30, 2012 at 23:03
  • 1
    Not 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.
    – fancyPants
    Jul 1, 2012 at 9:42
  • 1
    I'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
    – Mikhail
    Jul 2, 2012 at 14:59
40

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

5
  • 2
    If Profile gives you anything more useful than "sending data", you win the lottery.
    – Rick James
    Oct 10, 2017 at 23:47
  • 1
    I am using 5.7.19 and still show profiles work properly Dec 4, 2017 at 9:41
  • 2
    @SivaPraveen - OK, for now it still works, but "... will be removed ..."
    – Rick James
    Dec 4, 2017 at 18:00
  • Yeah, it is still in 8.0. And still mostly useless.
    – Rick James
    Feb 27 at 16:37
  • See also EXPLAIN ANALYZE.
    – Rick James
    Feb 27 at 16:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.