Well, After reading Erik Darling's post Filtered Indexes: Just Add Includes I'm really curious and sad about how I use INCLUDES:
He gives some examples and etc. about INCLUDE, but I'm confused about the actual index he creates.
This is the query:
SELECT Id, DisplayName
FROM Users
WHERE Reputation > 400000;
What I would do is:
CREATE INDEX ix_bla on USERS (Reputation)
INCLUDE (Id, DisplayName)
I thought we should use what's in the WHERE
on the index, and use INCLUDE
on those fields in the SELECT
.
But Erik did this:
CREATE UNIQUE NONCLUSTERED INDEX Users_400k_Club
ON dbo.Users ( DisplayName, Id )
INCLUDE ( Reputation )
WHERE Reputation > 400000
WITH ( DROP_EXISTING = ON );
The problem here is not the filtering index. My question is why he's using DISPLAYNAME
and ID
in the index, and REPUTATION
in the include clause?
And just for a simple test, running this query for a test in a table with millions of rows:
select COL1
,COL2
,COL3
from MyTable
where COL2 > 4513516
SQL tells me to create this index:
CREATE NONCLUSTERED INDEX ix_nada
ON MyTable
(
[COL2]
)
INCLUDE
(
[COL1],
[COL3]
)
GO