Wednesday Jun 25, 2008
Wednesday Jun 25, 2008
Filesort, as the name implies, is used to sort records when there is an ORDER BY clause in the query. The reason it has the prefix "file" is because temporary files may be used to store intermediate results. filesort() uses a per thread sort buffer of size sort_buffer_size to sort the table. filesort() is implemented in sql/filesort.cc.
filesort may not know how many records need to be sorted. It asks the storage engine for an estimate of the number of records in the table via estimate_rows_upper_bound(). If the number of records that fit in the sort buffer is less than the estimate, filesort will use temporary files to store intermediate results. The flow is as follows (or atleast the important steps to understand the bug mentioned above)
Initializing the sort buffer can be very expensive when the estimate for the number of rows is off. For example, in my benchmark, the table has 210 Million rows. Innodb returns 210 Million for estimate_rows_upper_bound(). The actual number of rows that fulfill the WHERE clause of my query is 3!. For a default sort buffer size of 2MB, the number of rows that fit in the sort buffer is 70,000. So filesort() unnecessarily initializes space for 70,000 rows, and does the sort. You might think initializing space for 70,000 rows is not a big deal, but when it constitutes 10% of the query execution time, it IS a big deal.
So how can we improve this?
Sorry for stupid question, how can I see your svg image?
Posted by xuekun on June 26, 2008 at 02:09 AM PDT #
I think this is the best visual explanation I've seen of how this works and why the sort buffer size is so important. I've seen people setting it to 128MB (!!!!). Explaining why that's a bad idea is always time-consuming.
Posted by Xaprb on June 26, 2008 at 05:06 AM PDT #
xuekun, you can use firefox to view the svg file.
Posted by realneel on June 26, 2008 at 06:17 AM PDT #
Baron, Thanks! Setting the sort_buffer_size to 128MB would have definitely killed the performance of my benchmark. BTW, you can generate your own callstacks using ruby and dtrace! checkout http://blogs.sun.com/realneel/entry/visualizing_callstacks_via_dtrace_and
Posted by realneel on June 26, 2008 at 06:26 AM PDT #