MySQL优化order by致使的 using filesort

<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&nbsp; ORDER BY result_date desc LIMIT 0,1000;</p> <p>&nbsp;查看执行计划:</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&nbsp; 创建索引:</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

相关文章
相关标签/搜索