[英]SQL query performance optimisation - fetching max(B) for corresponding A
我有一个看起来像这样的数据库方案(请参阅http://sqlfiddle.com/#!2/4c9b4/1/0 ):
create table t( id int, dataA int, dataB int);
insert into t select 1 ,1 ,1;
insert into t select 2 ,1 ,2;
insert into t select 3 ,1 ,3;
insert into t select 4 ,2 ,1;
insert into t select 5 ,2 ,2;
insert into t select 6 ,2 ,4;
insert into t select 7 ,3 ,1;
insert into t select 8 ,3 ,2;
insert into t select 9 ,4 ,1;
以及一个SQL查询,用于获取与“dataA”对应的最大“dataB”的“dataA”列表
SELECT * FROM t a WHERE dataB = (SELECT MAX(dataB) FROM t b WHERE b.dataA = a.dataA)
它工作正常,但在我的数据集上运行最多可能需要90秒。
如何提高此查询的性能?
也许MySQL一次又一次地执行子查询,即使对于重复的数据也是如此。 以下语句只为每个dataA找到一次max(dataB)。 其余的是一个简单的连接。 希望这更快。
select t.*
from t
join (select dataA, max(dataB) as maxDataB from t group by dataA) max_t
on t.dataA = max_t.dataA and t.dataB = max_t.maxDataB;
编辑:这是你的SQL小提琴: http : //sqlfiddle.com/# !2/4c9b4/2。
MySQL不能很好地进行聚合。 首先要尝试的是索引:
create index t_dataA_dataB on t(dataA, dataB);
这可能会解决问题。 第二个是使用以下技巧:
select a.*
from t a
where not exists (select 1
from t a2
where a2.dataA = a.dataA and
a2.dataB > a.dataB
);
这些将“让我最大”,以等价的:“给我所有的行t
那里有长着相同的,行dataA
和更大的dataB
”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.