MySQL优化order by导致的 using filesort
2015-11-06 12:52
501 查看
using filesort 一般出现在 使用了 order by 语句当中。
using filesort不一定引起mysql的性能问题。但是如果查询次数非常多,那么每次在mysql中进行排序,还是会有影响的。
这里的优化方式是在order by 的字段建立索引,例如 语句:
SELECT * FROM yw_syjgb ORDER BY result_date desc LIMIT 0,1000;
查看执行计划:
+----+-------------+----------+------+---------------+------+---------+------+---------+----------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+------+---------------+------+---------+------+---------+----------------+
| 1 | SIMPLE | yw_syjgb | ALL | NULL | NULL | NULL | NULL | 1312418 | Using filesort |
+----+-------------+----------+------+---------------+------+---------+------+---------+----------------+
则需要在result_date 建立索引:
此时查看执行计划:
+----+-------------+----------+-------+---------------+-------------+---------+------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+-------+---------------+-------------+---------+------+------+-------+
| 1 | SIMPLE | yw_syjgb | index | NULL | result_date | 6 | NULL | 1000 | NULL |
+----+-------------+----------+-------+---------------+-------------+---------+------+------+-------+
可以看到执行计划中使用索引后没有 Using filesort
需要注意的是:由于 Using filesort是使用算法在 内存中进行排序,MySQL对于排序的记录的大小也是有做限制:max_length_for_sort_data,默认为1024
show variables like '%max_length_for_sort_data%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| max_length_for_sort_data | 1024 |
+--------------------------+-------+
经过笔者测试,如果排序查询的数据两大于这个默认值的话,还是会使用Using filesort。
总结一句,当排序查询的数据量在默认值的范围内是,在排序的字段上加上索引可以提升MySQL查询的速度。
using filesort不一定引起mysql的性能问题。但是如果查询次数非常多,那么每次在mysql中进行排序,还是会有影响的。
这里的优化方式是在order by 的字段建立索引,例如 语句:
SELECT * FROM yw_syjgb ORDER BY result_date desc LIMIT 0,1000;
查看执行计划:
+----+-------------+----------+------+---------------+------+---------+------+---------+----------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+------+---------------+------+---------+------+---------+----------------+
| 1 | SIMPLE | yw_syjgb | ALL | NULL | NULL | NULL | NULL | 1312418 | Using filesort |
+----+-------------+----------+------+---------------+------+---------+------+---------+----------------+
则需要在result_date 建立索引:
此时查看执行计划:
+----+-------------+----------+-------+---------------+-------------+---------+------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+-------+---------------+-------------+---------+------+------+-------+
| 1 | SIMPLE | yw_syjgb | index | NULL | result_date | 6 | NULL | 1000 | NULL |
+----+-------------+----------+-------+---------------+-------------+---------+------+------+-------+
可以看到执行计划中使用索引后没有 Using filesort
需要注意的是:由于 Using filesort是使用算法在 内存中进行排序,MySQL对于排序的记录的大小也是有做限制:max_length_for_sort_data,默认为1024
show variables like '%max_length_for_sort_data%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| max_length_for_sort_data | 1024 |
+--------------------------+-------+
经过笔者测试,如果排序查询的数据两大于这个默认值的话,还是会使用Using filesort。
总结一句,当排序查询的数据量在默认值的范围内是,在排序的字段上加上索引可以提升MySQL查询的速度。
相关文章推荐
- windows 7下mysql5.5.18主从同步设置
- mysql主从同步,双向同步
- Mysql日期函数使用汇总
- MySQL创建用户与授权方法
- 总结:大唐电信遇到MySQL访问慢问题
- Mysql update 子查询锁表问题。
- MySQL存储过程详解 mysql 存储过程
- mysql的查询、子查询及连接查询
- MySQL 4.表操作与列操作
- MySQL:Table XXX is marked as crashed and should be repaired
- Mysql远程登陆及常用命令
- 查看mysql数据库连接数、并发数相关信息
- MySql操作时间
- MySQL存储过程传参之in,out,inout参数用法
- Lift-web下搭建MySql数据库
- MySql中having字句对组记录进行筛选使用说明
- mysql group by 用法解析(详细)
- Mysql函数:Last_insert_id()语法讲解
- mysql 索引 建立 查询 优化
- MySQL索引之前缀索引和索引选择性