114

I am using MySQL workbench - What is the difference between duration and fetch times when a query is run?

Also is there a way I can enable the microsecond option in MySQL?

OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
user1189851
  • 4,861
  • 15
  • 47
  • 69
  • 1
    Strange bounty. All answers are correct but you just dont believe them? [Here](http://forums.mysql.com/read.php?152,560430,560470#msg-560470) and [here](http://forums.mysql.com/read.php?152,560430,560610#msg-560610) are the exact same answers from Mike Lischke, who works for Oracle, on mysql forum. Credible enough? – Hugo Delsing May 07 '14 at 13:07

5 Answers5

205

Fetch time - measures how long transferring fetched results take, which has nothing to do with query execution. I would not consider it as sql query debugging/optimization option since fetch time depends on network connection, which itself does not have anything to do with query optimization. If fetch time is bottleneck then more likely there's some networking problem.

Note: fetch time may vary on each query execution.

Duration time - is the time that query needs to be executed. You should try to minimize it when optimizing performance of sql query.

Reference

Leri
  • 12,367
  • 7
  • 43
  • 60
  • 7
    For optimization purporses, remember that the option `query_cache_type` must be `OFF`, in order that the result doesn't affected by cache. See http://stackoverflow.com/questions/181894/mysql-force-not-to-use-cache-for-testing-speed-of-query – deldev Nov 21 '14 at 17:30
  • 10
    While I agree that **duration** is the value you usually want to optimize, since it directly relates to the efficiency of your query, unusually high **fetch** time can also point out important logic errors and/or inefficiencies. For example, you might be transferring large amounts of text with each query that the client should be caching. Or you might be returning 100k rows when you only expected 10. – GrandOpener Jul 17 '15 at 15:19
  • 1
    good point @GrandOpener - or you could be doing select(*) when all you really need is the row ID. – andrew lorien May 25 '16 at 04:34
  • I turned on profiler in MySQL and run a query. I then examined both Workbench and profiler output. The Duration reported by Workbench is 0.373, while the total Duration from the profiler barely reaches 0.01. Does it mean the Duration output in Workbench is NOT the total amount of time spent by MySQL for query execution? – Meglio Oct 30 '17 at 06:58
  • 2
    This doesn't seem right. I have two queries which return the same rows(15300) but their stats are always different. one takes 14,443 sec Duration / 1111,810 sec Fetch and other 443,986 sec Duration/ 0,0089 sec Fetch everytime. why is that? – Shahid May 28 '18 at 08:06
  • 1
    If this is the case, then for a fixed amount of columns in a select statement and a fixed limit for results the fetch time should always be more or less the same if the amount of limited items is delivered. I used a sql - query where i changed the where - clause and the fetch time needs 40 seconds longer then without the where clause. In the where clause im using a subquery that references another table. the *duration* doesn't change significantly but the fetch time does. How is that possible? – Matthias Gwiozda Apr 14 '20 at 06:20
19

Duration shows the time needed to execute the query and fetch is the time needed to read the result set (retrieve the data)

I am unsure about the microsecond option. If this is in regards to optimization, remember - "premature optimization is the root of all evil"

Kurt
  • 7,102
  • 2
  • 19
  • 16
11

The answer of Leri is a good start but ignores the fact that MySQL can stream the data to client before having all the results of the query.

Below is an example with 2 queries with the same results. The first one use a group by, so MySQL have to calculate the full aggregate before sending all the data. The second one use a subquery so MySQL calculate row by row the results set and is able to send the first line of result to client immediately.

MySQL Workbench durations

As you can see, the fetch time of the second query is 5 time longer (for the same data), because MySQL Workbench show the time before the first received data as Duration and the time after as Fetch, but as streaming can be involved, it doesn't mean that fetch duration is network duration only.

During Fetch time, the database could still be calculating results. So, if you see a big fetch duration, you can actually do someting! It probably mean that you're MySQL database is streaming results rows one by one and struggling to calculate the full results set.

PCO
  • 695
  • 8
  • 12
  • 1
    Is there a reference anywhere that can validate this. I tend to agree this may be true, but it would be nice to validate. – Ross Oct 07 '21 at 10:51
3

Execution time is time spent preparing the query and running the query AND The fetch time is time spent pulling-in the row results

Satyam
  • 60
  • 4
1

About the microsecond, try to enable it in the Preferences menu, and I also got a question about the duration and fetch time before, now I seems get the answer that the duration is the execution time of the query, and the fetch is retrieve the result and send them to wherever you want. For example, I get a query which duration time is 0.078 but will take 60 secs to send the data back to my website.

Spark8006
  • 635
  • 1
  • 7
  • 15