This appears to be a simple query:
SELECT CUS_ID, CUS_LOCATION, CUS_COU_ABBREV FROM CUS WHERE CUS_ID > 1800000 AND CUS_LOCATION_ATTEMPTED = 'N'
There is an index covering all of these columns:
CREATE NONCLUSTERED INDEX [IX_CUS_59] ON [dbo].[CUSTOMER]
(
[CUS_ID] ASC,
[CUS_LOCATION_ATTEMPTED] ASC,
[CUS_LOCATION] ASC,
[CUS_COU_ABBREV] 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]
Strangely, to me the accidental DBA just trying to keep the wheels on the bus, it's not being used in the query plan.
The database is choosing a different index:
CREATE NONCLUSTERED INDEX [IX_CUS_9] ON [dbo].[CUSTOMER]
(
[CUS_LOCATION_ATTEMPTED] ASC
)
INCLUDE([CUS_LOCATION],[CUS_COU_ABBREV]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 95) ON [PRIMARY]
It seems strange to me since IX_CUS_59 includes every column as a key column. Shouldn't that be better?
Looking at the Actual Exec Plan, the estimated CPU cost (and other costs) are lower for the second query so we know why SQL Server is picking this second index.
I assumed the first index should be preferred b/c it includes everything the query needs.
My question is how would the estimated CPU cost be lower for IX_CUS_9 over IX_CUS_59?
An assumption is that SQL Server already uses an index to retrieve CUS_ID so it doesn't need that in an index, however, still this second index includes the other three columns as key columns. Shouldn't that be better/ (more efficient) than 1 key and two included?
As requested, I've added the links to both query plans.
No index hint: https://www.brentozar.com/pastetheplan/?id=r1Xbebxut
With index hint for IX_CUS_59: https://www.brentozar.com/pastetheplan/?id=r1qmgbx_F