SQL Server Performance Forum – Threads Archive
Strange behaviour of Indexed ViewHi All, We are using indexed views in our application for performance benefit and better utilisation of resources on MS SQL Server 2000 Enterprise Edition… We are facing a strange problem… Before explaining the problem let me explain how we are making use of indexed views in our application Illustration: Indexed View
CREATE VIEW dbo.vwIndexed
INNER JOINdbo.Table_B ONTable_A.ForeginKey = Table_B.PrimaryKey
INNER JOINdbo.Table_D ONTable_A.ForeginKey = Table_D.PrimaryKey
INNER JOINdbo.Table_C ONTable_A.ForeginKey = Table_C.PrimaryKey
WHERE ((Table_A.ColumnM = ‘A’)
OR (Table_A.ColumnM = ‘B’))
OR((Table_A.ColumnM = ‘C’)
OR(Table_A.ColumnM = ‘D’))
AND Table_A.ColumnN = 1 We have a clustered indexe on the identity column and a non-clustered composite index consisting of Table_B.ColumnA, Table_A.ColumnA, Table_D.ColumnA, Table_D.ColumnB, Table_D.ColumnC, Table_D.ColumnD, Table_A.ColumnC, TableB_ColumnC We have created a view on top of this so that we can have a aggregate column, which is as follows CREATE VIEW dbo.view
INNER JOIN TableE ON dbo.vwIndexed.ColumnA = Table_E.PrimaryKey Then we run one of the following queries from a stored procedure which are dynamically generated based on the inputs parameter to the stored procedure Select Distinct Table_D.ColumnA from dbo.view where view.[Table_A.ColumnA] = ‘1234’ AND ((view.[Table_A.ColumnM] = ‘A’) OR (view.[Table_A.ColumnM] = ‘B’)) AND view.[Table_A.ColumnJ] > 0 Select Distinct Department_Id from dbo.view where view.[Table_D.ColumnA] = ’02’ AND view.[Table_A.ColumnA] = ‘1234’ AND ((view.[Table_A.ColumnM] = ‘A’) OR (view.[Table_A.ColumnM] = ‘B’)) AND view.[Table_A.ColumnJ] > 0 Select Distinct Stroke_id from dbo.view where view.[Table_D.ColumnB] = ‘A02’ AND view.[Table_A.ColumnA] = ‘1234’ AND ((view.[Table_A.ColumnM] = ‘A’) OR (view.[Table_A.ColumnM] = ‘B’)) AND view.[Table_A.ColumnJ] > 0 All the objects mentioned above are created using
SET QUOTED_IDENTIFIER ON
SET ANSI_NULLS ON
GO We have around 1.25 million rows in the underlying table as well as the Indexed view that we created… The first two selects in the above query uses indexed view to fetch the data while the third query goes to the base table to retireve the data… But if the underlying table has 1.25 million rows and the indexed view we created holds 25% of the data held by underlying table, then all the three queries uses indexed view… Though we have proper indexes created it still uses Clustered index scan in all the cases… We found this through the execution plan… We are not able to understand this behaviour Are we doing something wrong? Or is there a known issue with indexed view… Response immediately is greatly appreciated as the performance of our applications is greatly dependent upon this indexed view…
Can you post the indexes used on the view and the base tables please? MeanOldDBA
[email protected] When life gives you a lemon, fire the DBA.
For Indexed View we have a clustered index on Table_A.Pk_identitycolumn and a non-clustered composite index consisting of Table_B.ColumnA, Table_A.ColumnA, Table_D.ColumnA, Table_D.ColumnB, Table_D.ColumnC, Table_D.ColumnD, Table_A.ColumnC, TableB_ColumnC For base table we have clustered index as well as non-clustered index (wherever applicable) Cheers
How about SET options to obtain Consistent Results and using Deterministic Functions.
Are you using any Views in the queries, if so how about SCHEMABINDING option setup?
The SQL Server query optimizer automatically determines when an indexed view can be used for a given query execution. The view does not need to be referenced directly in the query for the optimizer to use it in the query execution plan. Therefore, existing applications may take advantage of the indexed views without any changes to the application itself. Only the indexed views have to be created. Satya SKJ
This posting is provided â€œAS ISâ€ with no rights for the sake of knowledge sharing.