Sergey,
How do I measure query efficiency?
Often, there are multiple ways of writing a single query. For example,
SELECT Table1.Field1
FROM Table1
INNER JOIN Table2
ON Table1.PK1 = Table2.FK2
WHERE Table2.Field2 = value1
could be written,
SELECT Table1.Field1
FROM Table1
INNER JOIN
(SELECT Table2.Field2, Table2.FK1
FROM Table2
WHERE Table2.Field2 = value1)
ON Table1.PK1 = Table2.FK1
or,
SELECT Table1.Field1
FROM Table1
WHERE Table1.PK1 IN
(SELECT Table2.FK1
FROM Table2
WHERE Table2.Field2 = value1)
Are any of these more efficient than the others?
The type of query that I am having the most trouble with is when I want to select the rows in a table that don't have their primary keys listed as a foreign key in a table with which it has a 0..* relationship. I have been coding it as,
SELECT Table1.Field1
FROM Table1
LEFT OUTER JOIN Table2
ON Table1.PK1 = Table2.PK2
WHERE Table2.FK1 IS NULL
Is there a more efficient way to write this query? Microsoft SQL doesn't support MINUS, which would be my first inclination.
Thanks,
>David,
>
>You've no control over how SQL Server utilizes CPU or memory so you should concentrate yor efforts on making the queries as efficient as possible.
David S. Alexander
Kettley Publishing
20271 SW Birch Street, 2nd Floor
Newport Beach, CA 92660-1752