Optimizing a SQL join statement
I am joining two tables. The first contains work orders and their associated part numbers. The second contains the BOM for all of the part numbers. They are both large tables. Individually, I can query the two tables in seconds if not less. When I perform the join, 开发者_如何学编程it takes minutes. Is it possible that the where at the end of this statement is being performed after the join? If the join is performed first, I could see this taking a long time. But if the first table is reduced first by the where, I would think this should go fast. Is there someway to write a more optimized query?
SELECT Table2.ItemNum As ItemNum
FROM Table1
INNER Join Table2
ON Table1.PartNum = Table2.PartNum
WHERE Table1.WorkOrder = 10100314
That will do a better job:
SELECT Table2.ItemNum As ItemNum
FROM Table2
INNER JOIN
(
SELECT *
FROM Table1
WHERE Table1.WorkOrder = 10100314
)AS Table1
ON Table1.PartNum = Table2.PartNum
精彩评论