SQL Fiddle: http://sqlfiddle.com/#!6/d4496/1 (data is pre-generated for your experiments)
There is obvious table:
CREATE TABLE Entity
(
ID int,
Classificator1ID int,
Classificator2ID int,
Classificator3ID int,
Classificator4ID int,
Classificator5ID int
);
and the view :
CREATE VIEW dbo.EntityView (ID, Code1, Code2, Code3, Code4, Code5)
WITH SCHEMABINDING
where entities fields Classificator1ID..Classificator5ID resolved to classificators values Code1..Code5
and there are a lot of indexes on this view:
CREATE UNIQUE CLUSTERED INDEX [IXUC_EntityView$ID] ON EntityView
([ID]);
CREATE UNIQUE NONCLUSTERED INDEX [IXU_EntityView$ID$include$ALL] ON EntityView
([ID]) INCLUDE (Code1, Code2, Code3, Code4, Code5);
CREATE UNIQUE NONCLUSTERED INDEX [IXU_EntityView$ALL] ON EntityView
([ID],Code1, Code2, Code3, Code4, Code5);
CREATE UNIQUE NONCLUSTERED INDEX [IXU_EntityView$ID$Code1] ON EntityView
([ID],Code1);
CREATE UNIQUE NONCLUSTERED INDEX [IXU_EntityView$ID$include$Code1] ON EntityView
([ID])INCLUDE (Code1);
CREATE NONCLUSTERED INDEX [IX_EntityView$Code1] ON EntityView
(Code1);
CREATE NONCLUSTERED INDEX [IX_EntityView$Code1$include$ID] ON EntityView
(Code1) INCLUDE (ID);
But QO never use them! Try this:
SELECT * FROM EntityView;
SELECT ID, Code1 FROM EntityView;
SELECT ID, Code1, Code2, Code3, Code4, Code5 FROM EntityView;
SELECT ID, Code1, Code2, Code3, Code4, Code5 FROM EntityView WHERE ID=1;
SELECT ID, Code1 FROM EntityView Where Code1 like 'NR%';
Why? And especially What is wrong with "include" indexes? index created , has all fields and still unused...
ADDED: THIS IS JUST TEST! Please do not be so angry and do not push me to analyze those indexes maitinence problems.
In my real project I can't explain why QO ignores indexed views (very-very usefull indexed views). But sometimes I see it utilize them in other places. I have created this db snippet to experiment with index formulas but may be I should do something more: tune statistcs somehow ?
WITH (NOEXPAND)
it uses the index just fine. – MilburnSELECT * FROM EntityView
will need to get all the data (all the columns) so doing a table/clustered index scan is the typically much faster way than doing a huge number of expensive key lookups ...... – Murchison...$ALL
indices whenever the base tables are updated .... – MurchisonAutomatic use of indexed view by query optimizer
(as compared toDirect use
which specifies the need forWITH (NOEXPAND)
) – Milburn