I have a working query that seems awfully inefficient; I'm wondering if I'm missing a simple way to improve it.
Simple table:
id date master_id ------------------------- 1 2015-02-01 0 2 2015-02-02 0 3 2015-02-03 0 4 2015-02-04 1 5 2015-02-02 1 6 2015-02-17 1 7 2015-02-27 1 8 2015-01-01 1
Objective: Get all rows where the master_id is zero, OR the master_id is not zero and no other rows of the same master_id have an earlier date. Order every result by date.
Current query, using a groupwise minimum subquery to create the second WHERE condition.
SELECT * FROM `test` WHERE `master_id` =0 OR `id` IN ( SELECT test.`id` FROM ( SELECT `master_id`, MIN(`date`) AS mindate FROM `test` WHERE `master_id` 0 GROUP BY `master_id` ) AS x INNER JOIN `test` ON x.`master_id` = test.`master_id` AND x.mindate= test.`date` ) ORDER BY `date`
It works, but the EXPLAIN makes it seem inefficient:
id select_type table type possible_keys key key_len ref rows Extra ------------------------------------------------------------------------------------------------------------- 1 PRIMARY test ALL NULL NULL NULL NULL 8 Using where; Using filesort 2 DEPENDENT SUBQUERY derived3 system NULL NULL NULL NULL 1 2 DEPENDENT SUBQUERY test eq_ref PRIMARY PRIMARY 4 func 1 Using where 3 DERIVED test ALL NULL NULL NULL NULL 8 Using where; Using temporary; Using filesort
Can I improve this? Should I break it into two queries, one for ID=0 and one for the groupwise min? Thanks in advance.