So I have this query
EXPLAIN SELECT r.* FROM r_table r
LEFT JOIN f_table f ON f.id = r.fid
LEFT JOIN f_table ff ON r.fid = 0 AND r.type = 'f' AND r.id = ff.id
WHERE (r.fid = 0 AND ff.cid = 421563) OR (r.fid <> 0 AND f.cid = 421563);
And it's performing a full table scan on table r
Explain output:
1 SIMPLE r ALL Index1 2314 Using where
1 SIMPLE f eq_ref PRIMARY PRIMARY 4 r.fid 1 Using where
1 SIMPLE ff eq_ref PRIMARY PRIMARY 4 r.id 1 Using where
How can I modify this query to make it not perform a full table scan? I know that I can probably use UNION nonetheless I'd rather not do UNION unless necessary.
Rewriting the query into a UNION indeed worked...here's the UNION query
EXPLAIN SELECT r.* FROM r_table r
LEFT JOIN f_table f ON r.fid <> 0 AND f.id = r.fid
WHERE f.cid = 421563
UNION SELECT r.* FROM r_table r
LEFT JOIN f_table ff ON r.fid = 0 AND r.obj_type = 'f' AND r.obj_id = ff.id
WHERE ff.cid = 421563;
And explain output
'2', 'UNION', 'ff', 'ref', 'PRIMARY,cid', 'cid', '4', 'const', '2', 'Using where; Using index'
'2', 'UNION', 'r', 'ref', 'Index1,obj_id_type', 'Index1', '13', 'const,const,ff.id', '1', 'Using where'
'1', 'PRIMARY', 'f', 'ref', 'PRIMARY,cid', 'cid', '4', 'const', '2', 'Using where; Using index'
'1', 'PRIMARY', 'r', 'ref', 'Index1', 'Index1', '4', 'f.id', '1', ''
NULL, 'UNION RESULT', '<union1,2>', 'ALL', NULL, NULL, NULL, NULL, NULL, ''
But once again, I would rather not do a UNION for this
Just move the filters on ff.cid and f.cid from the where clause to the join. In fact, since you are doing a left join, you have to do this anyway.