<div id="content" style="overflow-x: hidden; word-break: break-all;"><p>using filesort 通常出如今 使用了 order by 語句當中。</p> <p>using filesort不必定引發mysql的性能問題。可是若是查詢次數很是多,那麼每次在mysql中進行排序,仍是會有影響的。</p> <p>這裏的優化方式是在order by 的字段創建索引,例如 語句:</p> <p>SELECT * FROM yw_syjgb ORDER BY result_date desc LIMIT 0,1000;</p> <p> 查看執行計劃:</p> <p>+----+-------------+----------+------+---------------+------+---------+------+---------+----------------+<br>| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |<br>+----+-------------+----------+------+---------------+------+---------+------+---------+----------------+<br>| 1 | SIMPLE | yw_syjgb | ALL | NULL | NULL | NULL | NULL | 1312418 | Using filesort |<br>+----+-------------+----------+------+---------------+------+---------+------+---------+----------------+</p> <p><br>則須要在result_date 創建索引:</p> <p>此時查看執行計劃:</p> <p>+----+-------------+----------+-------+---------------+-------------+---------+------+------+-------+<br>| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |<br>+----+-------------+----------+-------+---------------+-------------+---------+------+------+-------+<br>| 1 | SIMPLE | yw_syjgb | index | NULL | result_date | 6 | NULL | 1000 | NULL |<br>+----+-------------+----------+-------+---------------+-------------+---------+------+------+-------+</p> <p>能夠看到執行計劃中使用索引後沒有 Using filesort</p> <p>須要注意的是:因爲 Using filesort是使用算法在 內存中進行排序,MySQL對於排序的記錄的大小也是有作限制:max_length_for_sort_data,默認爲1024<br>show variables like '%max_length_for_sort_data%';</p> <p>+--------------------------+-------+<br>| Variable_name | Value |<br>+--------------------------+-------+<br>| max_length_for_sort_data | 1024 |<br>+--------------------------+-------+<br>通過筆者測試,若是排序查詢的數據兩大於這個默認值的話,仍是會使用Using filesort。</p> <p>總結一句,當排序查詢的數據量在默認值的範圍內是,在排序的字段上加上索引能夠提高MySQL查詢的速度。</p> <p><span style="font-size: small;"><strong>本文永久更新連接地址</strong></span>:<a href="../../Linux/2015-11/124881.htm">http://www.linuxidc.com/Linux/2015-11/124881.htm</a></p><a href="http://www.linuxidc.com" target="_blank"><img src="/linuxfile/logo.gif" alt="linux" width="15" height="17"></a></div>mysql