I have a database scheme that looks like this (see 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;
And an SQL query to fetch a list of "dataA" for the maximum "dataB" corresponding to "dataA"
SELECT * FROM t a WHERE dataB = (SELECT MAX(dataB) FROM t b WHERE b.dataA = a.dataA)
It works OK, however it can take up to 90 seconds to run on my dataset.
How can I improve performance of this query ?
The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:
ALTER TABLE `t` ADD INDEX `t_idx_datab_dataa` (`dataB`,`dataA`);
SELECT
*
FROM
t AS t1
LEFT JOIN
t AS t2
ON (
t2.dataA = t1.dataA
)
AND (
t1.dataB < t2.dataB
)
WHERE
(
1 = 1
)
AND (
t2.dataB IS NULL
)