【发布时间】:2017-12-27 09:15:34
【问题描述】:
让 db1 和 db2 db1.table1
annee code code2 var1 ....
1991 11 12 779
1991 11 14 105
1991 11 15 10
1991 12 11 466
1991 12 14 296
1991 12 15 270
1991 14 11 15
1991 14 12 510
1991 14 15 6
1991 15 11 193
1991 15 12 455
1991 15 14 4
....
1992 11 12 779
1992 11 14 105
1992 11 15 10
1992 12 11 466
1992 12 14 296
1992 12 15 270
1992 14 11 15
1992 14 12 510
1992 14 15 6
1992 15 11 193
1992 15 12 455
1992 15 14 4
....
db2.table2
var1 code ...
test 11
test 12
test 14
test2 11
test2 14
test2 15
...
我需要优化以下查询(因为 db1.table1 包含 8 000 000 行):
select annee,sum(var1) from db1.table1 as M where
M.code in
(select t1.code from db2.table2 as t1 cross join db2.table2 as t2 where t1.var1='Test2' and t2.var1='Test2' and t1.code <> t2.code)
and M.code2 in
(select t2.code from db2.table2 as t1 cross join db2.table2 as t2 where t1.var1='Test2' and t2.var1='Test2' and t1.code <> t2.code)
group by annee order by annee desc
db1.table1 和 db2.table2 被索引和排序。 任何建议将不胜感激! 谢谢
【问题讨论】:
标签: mysql select optimization