Mysql 持续时间和获取时间

我正在使用 MySQL 工作台-在运行查询时,持续时间和取回时间之间有什么区别?

还有一个方法,我可以启用微秒选项在 MySQL?

69453 次浏览

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"

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.

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

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

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.