I've got to update records in a table containing about 210,000 records and I'm curious if different versions of the query will make much difference in the time it takes to execute.
Specifically, is it better to use IN with a subquery that returns a large recordset or NOT IN with a subquery that returns a small recordset? Or will it not make much difference?
Version 1:
The subquery in the above returns about 204,000 records, so I'm thinking the following might be faster:
Version 2
Now the subquery returns about 6,000 records.
Will putting "ORDER BY table3.table4_id" in the subquery make any difference?
Specifically, is it better to use IN with a subquery that returns a large recordset or NOT IN with a subquery that returns a small recordset? Or will it not make much difference?
Version 1:
Code:
UPDATE table1
SET Field1 = NULL, Field2 = NULL, Field3 = NULL, Field4 = NULL, Field5 = null
WHERE table1_id IN
(
SELECT table1.table1_id FROM table
LEFT JOIN table2 ON table1.table1_id = table2.table1_id
LEFT JOIN table3 ON table2.table3_id = table3.table3_id
WHERE
ISNULL(table3.table4_id, 0) <> '1436'
AND ISNULL(table3.table4_id,0) <> '2477'
AND ISNULL(table3.table4_id,0) <> '6428'
AND ISNULL(table3.table4_id, 0) <> '7241'
)
The subquery in the above returns about 204,000 records, so I'm thinking the following might be faster:
Version 2
Code:
UPDATE table1
SET Field1 = NULL, Field2 = NULL, Field3 = NULL, Field4 = NULL, Field5 = null
WHERE table1_id NOT IN
(
SELECT table1.table1_id FROM table
LEFT JOIN table2 ON table1.table1_id = table2.table1_id
LEFT JOIN table3 ON table2.table3_id = table3.table3_id
WHERE
(ISNULL(table3.table4_id, 0) = '1436'
OR ISNULL(table3.table4_id,0) = '2477'
OR ISNULL(table3.table4_id,0) = '6428'
OR ISNULL(table3.table4_id, 0) = '7241')
)
Now the subquery returns about 6,000 records.
Will putting "ORDER BY table3.table4_id" in the subquery make any difference?