I have a table and a (non-pk) index
CREATE TABLE [UniqueIdentifier]
(
[Id] [varchar](100) NOT NULL,
[ShortUI] [varchar](50) NULL,
[StateId] [tinyint] NOT NULL,
[TypeId] [tinyint] NOT NULL,
[GenerationRequestedOn] [datetimeoffset](7) NULL,
[AnticipatedUsageOn] [datetimeoffset](7) NULL,
[IssuerNotificationTime] [datetimeoffset](7) NULL,
[ParentId] [varchar](100) NULL,
[ProductItemId] [int] NULL,
[RecalledByCode] [varchar](100) NULL,
[FacilityId] [varchar](300) NULL,
[PendingArrival] [bit] NULL,
CONSTRAINT [PK_UniqueIdentifier]
PRIMARY KEY NONCLUSTERED ([Id] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [idx_IssuerNotificationTime]
ON [UniqueIdentifier]([IssuerNotificationTime] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
I have a select
SELECT
*
FROM
[UniqueIdentifier] u
WHERE
u.IssuerNotificationTime BETWEEN DATEADD(DAY, -7, DATEADD(YEAR, -5, GETDATE()))
AND DATEADD(YEAR, -5, GETDATE()) ;
This query should return "only" 972000 rows out of 141000000, but it reads everything!
If I do the explain plan, I see SQL Server does a full table scan, and doesn't use the index at all. Why why why?
Index properties: idx_issuerNotificationTime has PageFullness 95% and fragmentation 95%.
I also tried adding ORDER BY IssuerNotificationTime to the query. This adds a sort step to the execution plan, but index is still not used.
How can I make this use the index? Or, at least, how I can help SQL Server not need to read the entire table to get these rows?
SELECT *can you limit the columns to a smaller set? Then add those columns asINCLUDEto the non-clustered index