免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5394 | 回复: 5
打印 上一主题 下一主题

达人解释下关于Using index for group-by和Using index [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-06-11 11:50 |只看该作者 |倒序浏览
root@localhost[test] 11:40>create table cu2 like cu;
Query OK, 0 rows affected (0.03 sec)

root@localhost[test] 11:41>show create table cu2\G
*************************** 1. row ***************************
       Table: cu2
Create Table: CREATE TABLE `cu2` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `uid` int(11) DEFAULT NULL,
  `cid` int(11) DEFAULT NULL,
  `time` int(11) DEFAULT NULL,
  `ip` varchar(45) DEFAULT NULL,
  `state` int(11) DEFAULT NULL,
  `admin_id` double DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `index_cu_cid_uid_time` (`cid`,`uid`,`time`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

root@localhost[test] 11:42>insert into cu2 select * from cu;
Query OK, 6966 rows affected (0.03 sec)
Records: 6966  Duplicates: 0  Warnings: 0

两个几乎一样的表,一样的sql执行计划却不同
root@localhost[test] 11:42>explain select uid,max(time) from cu where cid<4 group by cid,uid\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: cu
         type: range
possible_keys: index_cu_cid_uid_time
          key: index_cu_cid_uid_time
      key_len: 10
          ref: NULL
         rows: 2
        Extra: Using where; Using index for group-by
1 row in set (0.00 sec)

root@localhost[test] 11:46>explain select uid,max(time) from cu2 where cid<4 group by cid,uid\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: cu2
         type: range
possible_keys: index_cu_cid_uid_time
          key: index_cu_cid_uid_time
      key_len: 5
          ref: NULL
         rows: 1511
        Extra: Using where; Using index
1 row in set (0.00 sec)

???这是为什么呢...

论坛徽章:
8
综合交流区版块每周发帖之星
日期:2015-12-02 15:03:53数据库技术版块每日发帖之星
日期:2015-10-02 06:20:00IT运维版块每日发帖之星
日期:2015-10-02 06:20:00IT运维版块每日发帖之星
日期:2015-09-14 06:20:00金牛座
日期:2014-10-10 11:23:34CU十二周年纪念徽章
日期:2013-10-24 15:41:34酉鸡
日期:2013-10-19 10:17:1315-16赛季CBA联赛之北京
日期:2017-03-06 15:12:44
2 [报告]
发表于 2010-06-11 12:48 |只看该作者
分析下2个表后再explain看看

论坛徽章:
0
3 [报告]
发表于 2010-06-22 09:50 |只看该作者
一个rows 2,一个rows 1511 数据量不一样,计划不同,正常。

论坛徽章:
9
每日论坛发贴之星
日期:2016-01-04 06:20:00数据库技术版块每日发帖之星
日期:2016-01-04 06:20:00每日论坛发贴之星
日期:2016-01-04 06:20:00数据库技术版块每日发帖之星
日期:2016-01-04 06:20:00IT运维版块每日发帖之星
日期:2016-01-04 06:20:00IT运维版块每日发帖之星
日期:2016-01-04 06:20:00综合交流区版块每日发帖之星
日期:2016-01-04 06:20:00综合交流区版块每日发帖之星
日期:2016-01-04 06:20:00数据库技术版块每周发帖之星
日期:2016-03-07 16:30:25
4 [报告]
发表于 2010-06-22 10:20 |只看该作者
analyze table 以后再试试

论坛徽章:
0
5 [报告]
发表于 2010-06-22 13:15 |只看该作者
回2,4楼analyze之后都是using index了,可是为什么不出现Using for order-by呢

论坛徽章:
9
每日论坛发贴之星
日期:2016-01-04 06:20:00数据库技术版块每日发帖之星
日期:2016-01-04 06:20:00每日论坛发贴之星
日期:2016-01-04 06:20:00数据库技术版块每日发帖之星
日期:2016-01-04 06:20:00IT运维版块每日发帖之星
日期:2016-01-04 06:20:00IT运维版块每日发帖之星
日期:2016-01-04 06:20:00综合交流区版块每日发帖之星
日期:2016-01-04 06:20:00综合交流区版块每日发帖之星
日期:2016-01-04 06:20:00数据库技术版块每周发帖之星
日期:2016-03-07 16:30:25
6 [报告]
发表于 2010-06-22 13:41 |只看该作者
cu 可能之前是乱序插入更新的吧,导致一些表的meatdata不是很准确~~
所以sql优化器在决定执行计划的时候出现偏差。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP