I have read that indexing columns should be used when using a WHERE clause to restrict returned records (i.e. SELECT * from Table1 WHERE Field1 = 'abc'
).
Does this rule apply when using APPLY
, as in
SELECT t.Field1,x.Field2 from Table1 t
OUTER APPLY (select * from Table2 x WHERE t.Field1 = x.Field1
?
In other words, would putting an index on Table2.Field1 speed up the above query?
Sorry if the answer is obvious, I don't have a solid understanding of the mechanics of OUTER and CROSS APPLY, only that they can be used in place of JOINs.
EDIT
A better question, based off of this thread (Hard and Fast Rules for include columns in Index) would be, should I use INCLUDE for all of the columns that I am pulling from the other table, and if so, is it more efficient to use OUTER APPLY (select Field1,Field2
instead of OUTER APPLY (select *
Well, whenever you specify a condition for column then an index will help to speed up the search.
It comes at a cost though: During insert the index will have to be recalculated and of course you will need disk space. But apart from that indexes have (almost) only advantages.