select 出现排序过慢(非插入顺序)

在查询测试时发现,查询很慢,60万条查一,用了10秒,

使用了explain分析一下sql解释过程,发现没使用主键,而是使用了filesort,把缓存放到文件中进行了排序;

觉得很奇怪,在其它机器中,使用了id来排序,很快.才1秒,或是少于这个.

后来才发现,原来此机内存过小(设置过小也会导致),导致数据量大时,内存无法进行排序放到硬盘中来了.才这么慢.

暂时解决方式是小表/多表了.

 

mysql> select count(*) from email;
+----------+
| count(*) |
+----------+
|   148973 |
+----------+
1 row in set (0.00 sec)

mysql> explain select * from email where id > 400000  order by id asc limit 400000, 100;
+----+-------------+-------+------+---------------+------+---------+------+--------+-----------------------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows   | Extra                       |
+----+-------------+-------+------+---------------+------+---------+------+--------+-----------------------------+
|  1 | SIMPLE      | email | ALL  | PRIMARY       | NULL | NULL    | NULL | 148973 | Using where; Using filesort |
+----+-------------+-------+------+---------------+------+---------+------+--------+-----------------------------+
1 row in set (0.00 sec)

mysql> explain select * from email where id > 100000  order by id asc limit 100000, 100;
+----+-------------+-------+-------+---------------+---------+---------+------+--------+-------------+
| id | select_type | table | type  | possible_keys | key     | key_len | ref  | rows   | Extra       |
+----+-------------+-------+-------+---------------+---------+---------+------+--------+-------------+
|  1 | SIMPLE      | email | range | PRIMARY       | PRIMARY | 4       | NULL | 148971 | Using where |
+----+-------------+-------+-------+---------------+---------+---------+------+--------+-------------+
1 row in set (0.00 sec)

mysql> select * from email where id > 100000  order by id asc limit 100000, 100;
+---------+-----------------------------------+---------+------+
| id      | email                             | unexist | time |

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值