I'm writing a query to determine if a record exists in the db according to some conditions met in other tables.
Now I came up with a situation where I need to know what is the best approach:
either OR the subqueries:
SELECT * from table as tbl WHERE
Exists
(SELECT 1 FROM t1 WHERE t1.id = tbl.id)
OR Exists
(SELECT 1 FROM t2 WHERE t2.col2 = tbl.col2)
OR Exists
(SELECT 1 FROM t3 WHERE t3.othercol = tbl.somecol)
or Use UNION without ORs:
SELECT * from tbl WHERE
Exists
(SELECT 1 FROM t1 WHERE t1.id = tbl.id
UNION
SELECT 1 FROM t2 WHERE t2.col2 = tbl.col2)
UNION
SELECT 1 FROM t3 WHERE t3.othercol = tbl.somecol)
I need this to be the best in performance, thus the question.
Some fields/columns might not be indexed and this might happen in a set of different columns not just 3, maybe even more than 1 per subquery/table.
For best resolution I'm going to publish some complex examples here:
How would this be with joins?
SELECT * from table as cli WHERE
Exists
(
SELECT
1
from
tbl_import_line_reference as l,
tbl_import_doc as d
WHERE
d.import_key = l.import_key AND
CAST(Left(d.doc_date,8) as DATE) BETWEEN LAST_DAY(NOW() - INTERVAL 6 MONTH) + INTERVAL 1 day AND NOW()
AND
l.prod_ref like '---fmidref%'
AND
d.doc_type ='F'
AND
d.car_miposreg_ext_id = cli.car_miposreg_ext_id
)
OR
Exists
(
select
1
from
tbl_import_line_reference as l,
tbl_import_doc as d
WHERE
d.import_key = l.import_key AND
CAST(Left(d.doc_date,8) as DATE) BETWEEN @data_final + INTERVAL 1 day AND NOW()
AND
l.prod_ref not RLIKE '---fmidneo|---fmidevo'
AND
l.act_code = 5
AND
l.act_subcode = "7"
AND
d.doc_type ='F'
AND
d.car_miposreg_ext_id = cli.car_miposreg_ext_id
)
After that this: http://pastebin.com/gTFBFurV became this: http://pastebin.com/y13xKcMg