I understand that Composite Indexes are always used Left to Right (e.g. if an Index was on City, State, WHERE City = "Blah" or WHERE City = "Blah" AND State = "AA" would work fine but WHERE State = "AA" would not).
Does this same principle apply to INCLUDE indexes?
Thanks in advance!
Clay
When the column in the composite index contains all the columns in the query, the index is also a covering index.When the composite index does not contain all the data to be queried, the query needs to find the location information of the data through the non-clustered index, and then go to the basic table to find the data.
A covering index is one that contains all the columns referenced in the query for a single table. We will demonstrate just how useful this can be. Here is our example query:
However unless your clustered index contains the required columns, which is not the case in our example, it will be insufficient for covering our query. To increase the likelihood that a nonclustered index is a covering index, it is tempting to begin adding additional columns to the index key.
There are two types of indexes in SQL Server, clustered and nonclustered indexes. A clustered index is an index whose leaf nodes, that is the lowest level of the index, contain the actual data pages of the underlying table.
No, include fields are not ordered.
Here are some additional design considerations:
http://msdn.microsoft.com/en-us/library/ms190806.aspx
Include columns can only be used to supply columns to the SELECT
portion of the query. They cannot be used as part of the index for filtering.
EDIT: To further clarify my point, consider this example:
I create a simple table and populate it:
create table MyTest (
ID int,
Name char(10)
)
insert into MyTest
(ID, Name)
select 1, 'Joe' union all
select 2, 'Alex'
Now consider these 3 indexes and their corresponding execution plans for a simple SELECT.
select ID, Name
from MyTest
where Name = 'Joe'
Case 1: An index on just ID results in a TABLE SCAN.
create index idx_MyTest on MyTest(ID)
Case 2: An index on ID including name. Somewhat better because the index covers the query, but I still get a SCAN operation.
create index idx_MyTest on MyTest(ID) include (Name)
Case 3: An index on Name including ID. This is the best. The index is built on the column in my WHERE clause, so I get a SEEK operation, and the index covers the query because of the included column.
create index idx_MyTest on MyTest(Name) include (ID)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With