MySql索引的一个技巧
2013-12-20 01:20
661 查看
索引的建立,直接会影响到查询性能。
看下面的查询:
select * from ddd where id>1 order by score;
我们查询学号大于1的学生的各科成绩得分。
那么按照一般的思路,是这样建立索引的(id,score)。
explain一下:
[sql] view
plaincopy
mysql> explain select * from ddd where id>1 order by score;
+----+-------------+-------+-------+---------------+------+---------+------+------+------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+------+---------+------+------+------------------------------------------+
| 1 | SIMPLE | ddd | range | id | id | 4 | NULL | 12 | Using where; Using index; Using filesort |
+----+-------------+-------+-------+---------------+------+---------+------+------+------------------------------------------+
1 row in set (0.00 sec)
查看explain的结果,我们发现,
查询过程使用了order by,出现了using filesort。也就是说,mysql在查询到符合条件的数据之后,做了排序。
【---------------------------------------------------------】
这是因为,当使用(id,score)索引的时候,查询where id > 1 order by score使用了一个非常量来限定索引的前半部分,所以只用到了索引的前半部分,后半部分没有使用。所以,排序还要mysql另外来做。如果这里的查询是where id = 1 order by score,那么必然就不会出现filesort了。
【---------------------------------------------------------】
怎么优化掉排序过程呢?
我们删掉(id,score)这个索引,新建一个(score,id)索引。
[sql] view
plaincopy
mysql> alter table ddd drop index id;
Query OK, 0 rows affected (0.98 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table ddd add index(score,id);
Query OK, 0 rows affected (0.11 sec)
Records: 0 Duplicates: 0 Warnings: 0
再次explain:
[sql] view
plaincopy
mysql> explain select * from ddd where id>1 order by score;
+----+-------------+-------+-------+---------------+-------+---------+------+------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+-------+---------+------+------+--------------------------+
| 1 | SIMPLE | ddd | index | NULL | score | 9 | NULL | 28 | Using where; Using index |
+----+-------------+-------+-------+---------------+-------+---------+------+------+--------------------------+
1 row in set (0.00 sec)
我们可以看到,explain结果就已经没有了using filesort。
这是因为,我们要取得id大于1的学生的score,最后按照score排序,那么我们扫描一遍(score,id)索引,找到id大于1的学生,然后直接取出信息即可。由于(score,id)索引已经排序好了,所以免去了排序的过程。
看下面的查询:
select * from ddd where id>1 order by score;
我们查询学号大于1的学生的各科成绩得分。
那么按照一般的思路,是这样建立索引的(id,score)。
explain一下:
[sql] view
plaincopy
mysql> explain select * from ddd where id>1 order by score;
+----+-------------+-------+-------+---------------+------+---------+------+------+------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+------+---------+------+------+------------------------------------------+
| 1 | SIMPLE | ddd | range | id | id | 4 | NULL | 12 | Using where; Using index; Using filesort |
+----+-------------+-------+-------+---------------+------+---------+------+------+------------------------------------------+
1 row in set (0.00 sec)
查看explain的结果,我们发现,
查询过程使用了order by,出现了using filesort。也就是说,mysql在查询到符合条件的数据之后,做了排序。
【---------------------------------------------------------】
这是因为,当使用(id,score)索引的时候,查询where id > 1 order by score使用了一个非常量来限定索引的前半部分,所以只用到了索引的前半部分,后半部分没有使用。所以,排序还要mysql另外来做。如果这里的查询是where id = 1 order by score,那么必然就不会出现filesort了。
【---------------------------------------------------------】
怎么优化掉排序过程呢?
我们删掉(id,score)这个索引,新建一个(score,id)索引。
[sql] view
plaincopy
mysql> alter table ddd drop index id;
Query OK, 0 rows affected (0.98 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table ddd add index(score,id);
Query OK, 0 rows affected (0.11 sec)
Records: 0 Duplicates: 0 Warnings: 0
再次explain:
[sql] view
plaincopy
mysql> explain select * from ddd where id>1 order by score;
+----+-------------+-------+-------+---------------+-------+---------+------+------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+-------+---------+------+------+--------------------------+
| 1 | SIMPLE | ddd | index | NULL | score | 9 | NULL | 28 | Using where; Using index |
+----+-------------+-------+-------+---------------+-------+---------+------+------+--------------------------+
1 row in set (0.00 sec)
我们可以看到,explain结果就已经没有了using filesort。
这是因为,我们要取得id大于1的学生的score,最后按照score排序,那么我们扫描一遍(score,id)索引,找到id大于1的学生,然后直接取出信息即可。由于(score,id)索引已经排序好了,所以免去了排序的过程。
相关文章推荐
- MySql索引的一个技巧
- mysql索引类型总结和使用技巧以及注意事项
- MySQL索引类型总结和使用技巧以及注意事项
- MySQL索引类型总结和使用技巧以及注意事项
- 使用mysql索引技巧及注意事项
- mysql 1亿条记录建立一个索引 耗时2小时
- mysql索引使用技巧及注意事项
- 使用mysql索引技巧及注意事项
- MySQL索引类型总结和使用技巧以及注意事项
- MySQL索引类型总结和使用技巧以及注意事项
- MySQL索引类型总结和使用技巧以及注意事项
- mysql索引优化技巧
- MySQL一个索引最多有多少个列?真实的测试例子
- [MySQL]索引类型总结和使用技巧以及注意事项
- 使用mysql索引技巧及注意事项
- MySQL索引使用方法及技巧大全
- MySQL索引类型总结和使用技巧以及注意事项
- 在MYSQL中使用全文索引(FULLTEXTindex)MYSQL的一个很有用的特性是使用全文索引
- 关于mysql使用索引的一个问题
- 2014.06.26 MySQL 用索引解决的一个性能问题