I defined a non clustered index with Include and Filter on Students table. The SQL Server version is 2017.
Students table definition:
CREATE TABLE [dbo].[Students]
(
[Id] [INT] IDENTITY(1,1) NOT NULL,
[Name] [NVARCHAR](50) NOT NULL,
[CreatedOn] [DATETIME2](7) NOT NULL,
[Active] [BIT] NOT NULL,
[Deleted] [BIT] NOT NULL,
CONSTRAINT [PK_Students]
PRIMARY KEY CLUSTERED ([Id] ASC)
) ON [PRIMARY]
Non-clustered index with include and filter:
CREATE NONCLUSTERED INDEX [NonClusteredIndex-20200508-225254]
ON [dbo].[Students] ([CreatedOn] ASC)
INCLUDE([Name])
WHERE ([Active] = (1) AND [Deleted] = (0))
ON [PRIMARY]
GO
This query uses NonClusteredIndex-20200508-225254
SELECT Name, CreatedOn FROM dbo.Students
WHERE Active = 1
AND Deleted = 0
ORDER BY CreatedOn
Actual execution plan
But when I use the parameterized query as following, it doesn't use the NonClusteredIndex-20200508-225254
. Why does this happen? Where am I wrong?
DECLARE @Active BIT = 1
DECLARE @Deleted BIT = 0
SELECT Name, CreatedOn
FROM dbo.Students
WHERE Active = @Active
AND Deleted = @Deleted
ORDER BY CreatedOn
Actual execution plan