问题
I am trying to identify value from one column which does exist in another column in same table
Sub query
SELECT DISTINCT `Wear it With - Outfits 1`
FROM `product list`
WHERE `Wear it With - Outfits 1` NOT IN (SELECT `sku`
FROM `product list`)
...returns result in 2.7287sec
I tried to replace sub query by left join
SELECT DISTINCT table1.`Wear it With - Outfits 1`
FROM `product list` as table1
LEFT JOIN `product list` as table2 ON table1.`Wear it With - Outfits 1`=table2.sku
WHERE table2.sku IS NULL
AND table1.`Wear it With - Outfits 1` IS NOT NULL
...which returns result in 5.7651 sec
Normally joins return results much faster. So i believe i did something funny in my query? But can not find any reason why my subquery is running faster than
回答1:
The statement 'Normally joins return results much faster.' is silly, especially without reference to any particular database system.
Many factors go into determining the performance of a particular query. You can use the EXPLAIN tool in whatever database product you're using to determine exactly why the sub-query is preferable in this case (hint: it likely has to do with the use of the keyword DISTINCT).
回答2:
The main reason is that your left join statement is not optimized .
The WHERE condition WHERE table2.sku IS NULL and table1.Wear it With - Outfits 1
IS NOT NULL
can waste a lot of time especially in this case.
You should optimize table2 before you left join.
PS:The number of records in table2 should reach a considerable amount.
来源:https://stackoverflow.com/questions/7830725/subquery-is-running-faster-than-join