Using mssql server 2008. I need to compare performance. I don’t have access to query analyzer until next week. It is just a crazy thought, I want to know which script will perform best.
I am wondering which script(s) can use the index on txt and why.
The 2 tables has a total of 3 indexes and both tables has approximate 1 mil rows
table1(id int(clustered index), name varchar(10))
table2(t1_id int(non-clustered index), txt varchar(10)(non-clustered index))
SQL1: (2*joins)
SELECT *
FROM table1 t1 LEFT JOIN
table2 t2 ON t1.id = t2.t1_id
AND NOT t2.txt LIKE 'blue%'
AND NOT t2.txt LIKE 'green%'
INNER JOIN table2 t3 ON t1.id = t3.t1_id
WHERE t2.id is NULL
SQL2: (1*join 1*’or’)
SELECT *
FROM table1 t1 LEFT JOIN
table2 t2 ON t1.id = t2.t1_id
AND (t2.txt LIKE 'blue%' or t2.txt LIKE 'green%')
In your first sql query you should really not use two joins and instead us a NOT EXISTS, because this will not affect the number of columns thrown around in the temp DB.
It’s much more readable and does the exact same work, however you should avoid double negatives (“NOT EXISTS”, “NOT LIKE”) because it makes readability harder.
If the performance of the second query is usable or almost the same, I would suggest using that.
Also have you thought of a full text index instead?