查询速度很慢,查询语句如下:
SELECT
a.stockid ,
b.freq ,
d.vwap ,
a.exdate,
a.comp ,
c.methodid – 6000000 ,
c.nums ,
c.nums * b.freq / 100
FROM
ana a,
credit b,
result c,
source d
WHERE
a.exdate = “2016-04-15”
AND a.comp = b.comp
AND b.willarise > 0
AND c.methodid = b.methodid
AND c.comp = b.comp
AND d.qtid = a.stockid
AND d.date = a.exdate
ORDER BY b.freq;
explain的结果如下:
各个表的情况如下:
b表比较小,96万多点,索引:
PRIMARY KEY (`sid`),
UNIQUE KEY `methodid` (`methodid`,`comp`) USING BTREE,
KEY `comp` (`comp`) USING BTREE
a,d,都是2300多万条数据,
a表索引:
PRIMARY KEY (`sid`),
UNIQUE KEY `stockid` (`stockid`,`exdate`) USING BTREE,
KEY `iddate` (`sid`,`stockid`,`exdate`) USING BTREE,
KEY `comp1` (`comp`,`arise1`) USING BTREE,
KEY `comp2` (`comp`,`arise2`) USING BTREE,
KEY `comp3` (`comp`,`arise3`) USING BTREE,
KEY `comp4` (`comp`,`arise4`) USING BTREE,
KEY `comp5` (`comp`,`arise5`) USING BTREE,
KEY `comp6` (`comp`,`arise6`) USING BTREE,
KEY `comp7` (`comp`,`arise7`) USING BTREE,
KEY `comp8` (`comp`,`arise8`) USING BTREE,
KEY `comp9` (`comp`,`arise9`) USING BTREE,
KEY `comp10` (`comp`,`arise10`) USING BTREE,
d表的索引:
PRIMARY KEY (`sid`),
KEY `dd` (`qtid`,`date`,`volume`,`value`) USING BTREE,
KEY `date` (`date`) USING BTREE,
KEY `sss` (`sid`,`date`,`volume`,`vwap`) USING BTREE
c表,5000万出头,索引:
PRIMARY KEY (`sid`),
KEY `comp` (`comp`) USING BTREE,
KEY `numsgt0` (`nums`,`gt0`) USING BTREE,
KEY `methodidcomp` (`methodid`,`comp`) USING BTREE
本人不擅长SQL,请高手帮忙优化一下。机器性能不错,插入能每秒4万条左右。
SELECT
a.stockid ,
b.freq ,
d.vwap ,
a.exdate,
a.comp ,
c.methodid – 6000000 ,
c.nums ,
c.nums * b.freq / 100
FROM
ana a,
credit b,
result c,
source d
WHERE
a.exdate = “2016-04-15”
AND a.comp = b.comp
AND b.willarise > 0
AND c.methodid = b.methodid
AND c.comp = b.comp
AND d.qtid = a.stockid
AND d.date = a.exdate
ORDER BY b.freq;
explain的结果如下:
各个表的情况如下:
b表比较小,96万多点,索引:
PRIMARY KEY (`sid`),
UNIQUE KEY `methodid` (`methodid`,`comp`) USING BTREE,
KEY `comp` (`comp`) USING BTREE
a,d,都是2300多万条数据,
a表索引:
PRIMARY KEY (`sid`),
UNIQUE KEY `stockid` (`stockid`,`exdate`) USING BTREE,
KEY `iddate` (`sid`,`stockid`,`exdate`) USING BTREE,
KEY `comp1` (`comp`,`arise1`) USING BTREE,
KEY `comp2` (`comp`,`arise2`) USING BTREE,
KEY `comp3` (`comp`,`arise3`) USING BTREE,
KEY `comp4` (`comp`,`arise4`) USING BTREE,
KEY `comp5` (`comp`,`arise5`) USING BTREE,
KEY `comp6` (`comp`,`arise6`) USING BTREE,
KEY `comp7` (`comp`,`arise7`) USING BTREE,
KEY `comp8` (`comp`,`arise8`) USING BTREE,
KEY `comp9` (`comp`,`arise9`) USING BTREE,
KEY `comp10` (`comp`,`arise10`) USING BTREE,
d表的索引:
PRIMARY KEY (`sid`),
KEY `dd` (`qtid`,`date`,`volume`,`value`) USING BTREE,
KEY `date` (`date`) USING BTREE,
KEY `sss` (`sid`,`date`,`volume`,`vwap`) USING BTREE
c表,5000万出头,索引:
PRIMARY KEY (`sid`),
KEY `comp` (`comp`) USING BTREE,
KEY `numsgt0` (`nums`,`gt0`) USING BTREE,
KEY `methodidcomp` (`methodid`,`comp`) USING BTREE
本人不擅长SQL,请高手帮忙优化一下。机器性能不错,插入能每秒4万条左右。
解决方案
10
b 表上没有索引可以利用。
AND b.willarise > 0
ORDER BY b.freq;
或LZ认为的查询方案是什么?先从哪个表查起会理想一些? 可以考虑强制索引?
AND b.willarise > 0
ORDER BY b.freq;
或LZ认为的查询方案是什么?先从哪个表查起会理想一些? 可以考虑强制索引?
30
这样的话基本就没有歧义了,a上用exdate的索引,过滤出8~9千条,这第一步以后的数据集就很小了。
假如用这个方案,对于这个sql,b上的willarise有没有索引就无所谓了。
假如特别需要提高效率,进一步可以考虑(comp, willarise)的包含索引,或是willarise的partitioin(partition有诸多限制,要谨慎)。