select m.c5 c5,m.co co,max(m.c1) c1 from ST1401_main m join (
select c5,co, CAST(date_add(max(c1),interval -1 month) AS UNSIGNED) c1 ,max(c1) from ST1401_main
where c5>=20111231000000 and c5<=20121231000000 group by c5,co ) n on m.c5=n.c5 and m.co=n.co and
m.c1<n.c1 group by m.c5,m.co
1 PRIMARY <derived2> ALL \N \N \N \N 4686 Using temporary; Using filesort
1 PRIMARY m ref PRIMARY,C1,C5,C5COC1 C5COC1 107 n.c5,n.co 296 Using where; Using index
2 DERIVED ST1401_main range C5,C5COC1 C5COC1 107 \N 6055 Using where; Using index for group-by就是第一步那个应该 怎么优化呢
解决方案 »
- 一个简单的sql语句想了1小时未果..求解答
- 求一条mysql sql语句。。。
- mysql查表记录合并
- mysql_row[]取值转换问题,急!
- 如何测试mysql的读写性能
- (做了5天了,求求高手了)无法创建下列URL的连接: jdbc:mysql://localhost:3306/decor?user=root&password=root&useUnicode=true&charac
- 我用mysqldump备份的mysql innodb的库不能正确恢复,因为数据内容有gb2312码和big5码!!
- JAVA 如何查询数据库列的长度限制?
- mysql不支持中文全文索引,你在建站中是怎么解决全文搜索的?
- 求助 PL/SQL,单表查询 时段内连续符合条件的日期统计
- 建立mysql集群时,选择簇,还是heartbeat的方式?谁能帮忙介绍一下两种到底用在什么情况中吗?
- 相关文章 的实现方法。
where c5>=20111231000000 and c5<=20121231000000 group by c5,co) n on m.c5=n.c5 and m.co=n.co and
m.c1<(n.c1-100000000) group by m.c5,m.co1 PRIMARY <derived2> ALL \N \N \N \N 4686 Using temporary; Using filesort
1 PRIMARY m ref PRIMARY,C1,C5,C5COC1 C5COC1 107 n.c5,n.co 296 Using where; Using index
2 DERIVED ST1401_main range C5,C5COC1 C5COC1 107 \N 6055 Using where; Using index for group-by
explainselect m.c5 c5,m.co co,max(m.c1) c1 from ST1401_main m join (select c5,co,max(c1) c1 from ST1401_main
where c5>=20111231000000 and c5<=20121231000000 group by c5,co) n on m.c5=n.c5 and m.co=n.co and
m.c1=(n.c1-100000000) group by m.c5,m.co
1 PRIMARY <derived2> ALL \N \N \N \N 4686 Using temporary; Using filesort
1 PRIMARY m ref PRIMARY,C1,C5,C5COC1 C5COC1 116 n.c5,n.co,func 1 Using where; Using index
2 DERIVED ST1401_main range C5,C5COC1 C5COC1 107 \N 6055 Using where; Using index for group-by
where c5>=20111231000000 and c5<=20121231000000 group by c5,co
单独运行上述语句,速度如何,EXPLAIN 一下
Execution Time : 00:00:00:015
Transfer Time : 00:00:00:235
Total Time : 00:00:00:250该加的索引我已经加过! 但是应该是查出结果 应该放入到一个临时的derived2中间表 然后这这个中间的和ST1401_main查的话就慢了
另外你的语句想实现的功能是什么?
ST1401_main:c5、co、c1上建立复合索引没有
建立临时表,如LSB,字段c5,co,c1,AA,在c5、co、c1上建立复合索引,
TRUNCATE LSB;
INSERT INTO LSB select c5,co, CAST(date_add(max(c1),interval -1 month) AS UNSIGNED) c1 ,max(c1) from ST1401_main
where c5>=20111231000000 and c5<=20121231000000 group by c5,co
再运行下述语句试试
select m.c5 c5,m.co co,max(m.c1) c1 from ST1401_main m join LSB n on m.c5=n.c5 and m.co=n.co and
m.c1<n.c1 group by m.c5,m.co
from ST1401_main m join (
select c5,co, max(c1) - interval 1 month as c1
from ST1401_main
where c5>=20111231000000 and c5<=20121231000000
group by c5,co
) n
on m.c5=n.c5
and m.co=n.co
and m.c1<n.c1
group by m.c5,m.co
show create table 都贴出来以供分析。
`CV` bigint(20) DEFAULT NULL,
`CKey` char(32) COLLATE utf8_bin DEFAULT NULL,
`CO` varchar(32) COLLATE utf8_bin NOT NULL DEFAULT '',
`C1` bigint(20) DEFAULT NULL,
`C2` int(10) unsigned DEFAULT NULL,
`C3` bigint(20) DEFAULT NULL,
`C4` int(10) unsigned DEFAULT NULL,
`C5` bigint(20) DEFAULT NULL,
`C6` double DEFAULT NULL,
`C7` double DEFAULT NULL,
`C8` double DEFAULT NULL,
`C9` double DEFAULT NULL,
`C10` double DEFAULT NULL,
`C11` double DEFAULT NULL,
`C12` double DEFAULT NULL,
`C13` double DEFAULT NULL,
`C14` double DEFAULT NULL,
`C15` double DEFAULT NULL,
`C16` double DEFAULT NULL,
`C17` double DEFAULT NULL,
`C18` double DEFAULT NULL,
`C19` int(10) unsigned DEFAULT NULL,
`C20` int(10) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`CO`,`C20`),
KEY `CV` (`CV`),
KEY `C1` (`C1`),
KEY `C2` (`C2`),
KEY `C5` (`C5`),
KEY `C19` (`C19`),
KEY `C5COC1` (`C5`,`CO`,`C1`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin
你们有没有注意到要是把m.c1<(n.c1-100000000) 改为 m.c1=(n.c1-100000000)执行计划一样的 只要500毫秒 之前的要4秒! 这个数据大概300W的 我想要的效果是 1秒钟出来 有谁有好的方法啊
!
KEY `C1` (`C1`),
KEY `C2` (`C2`),
KEY `C5` (`C5`),
KEY `C19` (`C19`),
KEY `C5COC1` (`C5`,`CO`,`C1`)
---------
这几个是他的key吧
SHOW INDEX FROM ST41010_main
ST41010_main 0 PRIMARY 1 CO A 1 \N \N BTREE
ST41010_main 0 PRIMARY 2 C10 A 35 \N \N BTREE
ST41010_main 1 CV 1 CV A 35 \N \N YES BTREE
ST41010_main 1 C1 1 C1 A 35 \N \N YES BTREE
ST41010_main 1 C2 1 C2 A 35 \N \N YES BTREE
ST41010_main 1 C9 1 C9 A 2 20 \N YES BTREE
ST1401_main 0 PRIMARY 2 C20 A 3703760 \N \N BTREE
ST1401_main 1 CV 1 CV A 3703760 \N \N YES BTREE
ST1401_main 1 C1 1 C1 A 690 \N \N YES BTREE
ST1401_main 1 C2 1 C2 A 4 \N \N YES BTREE
ST1401_main 1 C5 1 C5 A 7 \N \N YES BTREE
ST1401_main 1 C19 1 C19 A 1 \N \N YES BTREE
ST1401_main 1 C5COC1 1 C5 A 7 \N \N YES BTREE
ST1401_main 1 C5COC1 2 CO A 12512 \N \N BTREE
ST1401_main 1 C5COC1 3 C1 A 3703760 \N \N YES BTREE
from (
select c5,co, max(c1) - interval 1 month as c1
from ST1401_main
where c5>=20111231000000 and c5<=20121231000000
group by c5,co
) n inner join ST1401_main m
on m.c5=n.c5
and m.co=n.co
and m.c1<n.c1
group by m.c5,m.co
试一下。
select c5,co, CAST(date_add(max(c1),interval -1 month) AS UNSIGNED) c1 ,max(c1) from ST1401_main
where c5>=20111231000000 and c5<=20121231000000 group by c5,co ) n on m.c5=n.c5 and m.co=n.co and
m.c1<n.c1 group by m.c5,m.co
1 PRIMARY <derived2> ALL \N \N \N \N 4686 Using temporary; Using filesort
1 PRIMARY m ref PRIMARY,C1,C5,C5COC1 C5COC1 107 n.c5,n.co 296 Using where; Using index
2 DERIVED ST1401_main range C5,C5COC1 C5COC1 107 \N 6055 Using where; Using index for group-by就是第一步那个应该 怎么优化呢第一步:
从你的需求看,可以考虑借助索引覆盖的技巧解决,即创建索引:
ALTER TABLE ST1401_main ADD INDEX idx_c5_co_c1(c5,co,c1);第二步:
SET GLOBAL JOIN_BUFFER_SIZE=1024*1024*16;
SET GLOBAL TMP_BUFFER_SIZE=1024*1024*64;
SET GLOBAL HEAP_BUFFER_SIZE=1024*1024*64;SET GLOBAL READ_BUFFER_SIZE=1024*1024*2;
#****************************************#
MySQL技术及运维自动化网:www.mysqlops.com新浪微博账号:http://weibo.com/mysqlops
#****************************************#