mysql 查询时遇到的奇怪的问题!求高手解决!
时间:2011-06-30
来源:互联网
1.explain select * from(select a.id,sum(a.in_bytes) as in_bytes,sum(a.out_bytes) as out_bytes,sum(a.in_bytes + a.out_bytes) as total_bytes from t_connect as a where a.record_time between '2011-06-30 00:00:00'and '2011-06-30 14:04:55' group by a.app_id) as c order by total_bytes desc
对应的结果如下:
+----+-------------+------------+------+---------------+------+---------+------+----------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+------+---------------+------+---------+------+----------+----------------------------------------------+
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 101 | Using filesort |
| 2 | DERIVED | a | ALL | record_time | NULL | NULL | NULL | 10989554 | Using where; Using temporary; Using filesort |
+----+-------------+------------+------+---------------+------+---------+------+----------+----------------------------------------------+
2 rows in set (16.59 sec)
2.explain select * from(select a.id,sum(a.in_bytes) as in_bytes,sum(a.out_bytes) as out_bytes,sum(a.in_bytes + a.out_bytes) as total_bytes from t_connect as a where a.record_time between '2011-06-30 00:00:00'and '2011-06-30 11:04:55' group by a.app_id) as c order by total_bytes desc
对应的结果如下:
+----+-------------+------------+-------+---------------+-------------+---------+------+---------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+-------+---------------+-------------+---------+------+---------+----------------------------------------------+
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 93 | Using filesort |
| 2 | DERIVED | a | range | record_time | record_time | 9 | NULL | 1882981 | Using where; Using temporary; Using filesort |
+----+-------------+------------+-------+---------------+-------------+---------+------+---------+----------------------------------------------+
2 rows in set (2.08 sec)
+-------------+----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+----------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| app_id | int(10) unsigned | YES | MUL | NULL | |
| user_id | int(10) unsigned | NO | MUL | NULL | |
| src_addr | int(10) unsigned | YES | | 0 | |
| dst_addr | int(10) unsigned | YES | | 0 | |
| dst_port | smallint(5) unsigned | YES | | 0 | |
| protocol | char(32) | YES | | NULL | |
| in_bytes | int(10) unsigned | YES | | 0 | |
| out_bytes | int(10) unsigned | YES | | 0 | |
| record_time | datetime | YES | MUL | NULL | |
| end_time | datetime | YES | | NULL | |
+-------------+----------------------+------+-----+---------+----------------+
t_connect表中对app_id、user_id、record_time3个字段建立了索引
但是第一条sql语句和第二天sql语句的区别就是时间改了下,第二天sql语句时间改短了,所以数据量少一些,而第一条sql语句时间间隔长些,数据量稍微大些。为什么会出现第一条sql不会运用到索引record_time,而第二条sql语句会用到索引record_time呢?而且第二条sql运行的速度明显比第一条快(快了14s)。这就导致了我页面中查询数据时,大数据量的情况下(>1000w)有时候数据10几秒就能加载出来,有时候却要将近1分钟。 求高手帮忙解决!小弟不胜感激!
作者: USERNAME_FB 发布时间: 2011-06-30
你把2个查询的条件改成一样的,写法 不一样,结果贴出来。
作者: zuoxingyu 发布时间: 2011-06-30
作者: zuoxingyu 发布时间: 2011-06-30
一种常见的不用索引的情况是,条件范围内包含的记录数占据了总记录数的绝大部分,这时候通过索引去查询比全表扫描还要慢,然后优化器就不走索引了。
您意思就是说,条件范围内包含的记录数占据了总记录数的绝大部分时索引反而影响了sql的性能吗? 那对于这种情况 有什么办法能避免吗?这应该怎样优化啊?
作者: USERNAME_FB 发布时间: 2011-06-30
不会影响SQL性能。并不是走索引的查询就一定快。
作者: zuoxingyu 发布时间: 2011-06-30
作者: USERNAME_FB 发布时间: 2011-06-30
还有另外很多情况。
作者: zuoxingyu 发布时间: 2011-06-30
作者: USERNAME_FB 发布时间: 2011-06-30
作者: zuoxingyu 发布时间: 2011-06-30
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28