Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Composite Index vs. INCLUDE Covering Index in SQL Server

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

like image 983
PseudoToad Avatar asked Oct 07 '10 16:10

PseudoToad


People also ask

What is the difference between composite index and covering index?

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.

What is a covering index in SQL?

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:

Can a nonclustered index be used as a covering index?

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.

What are the different types of indexes in SQL Server?

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.


2 Answers

No, include fields are not ordered.

Here are some additional design considerations:

http://msdn.microsoft.com/en-us/library/ms190806.aspx

like image 188
BradC Avatar answered Sep 30 '22 12:09

BradC


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)

alt text

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)

alt text

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)

alt text

like image 44
Joe Stefanelli Avatar answered Sep 30 '22 13:09

Joe Stefanelli