开发者

Better for SQL Query performance to use an actual denormalized table with indexes rather than an indexed view?

开发者 https://www.devze.com 2022-12-15 14:45 出处:网络
In order to improve the performance of a query I have created a denormalized indexed view that contains some of the informat开发者_JS百科ion I need to report on.When I didn\'t get the performance gain

In order to improve the performance of a query I have created a denormalized indexed view that contains some of the informat开发者_JS百科ion I need to report on. When I didn't get the performance gains that I had hoped for I created a table version of my view with indexes and got significantly better performance.

I should note that when I create my view there are a lot of ISNULLs in the SELECT. I know that these can hurt performance if these columns were joined on a regular view but I was under the impression that it would be OK if the view was indexed. Could the ISNULLs be the problem?


Did you index the columns you were actually selecting on? If you don't have a covering index on the indexed view for your query, then you will definitely find a table is quicker. If you do, though, there should be no real difference. Example:

CREATE VIEW dbo.denormalized
WITH SCHEMABINDING
AS
    SELECT  A.id,
            A.col1,
            A.col2,
            ISNULL(B.col3, '') col3
    FROM    dbo.A LEFT JOIN dbo.B ON A.Bid = B.id
GO

CREATE UNIQUE CLUSTERED INDEX UIX_denormlaized
ON dbo.denormalized (id)

So far so good. Now, we try to select from this view as follows:

SELECT id, col3 FROM denormalized

The only persisted data for this view is the index on the ID column - the remainder has to be workout out on the fly. So the ISNULL is calculated again for each row. However if we add this index:

CREATE INDEX IX_denormalized
ON dbo.denormalized (id, col3)

then the same query is served entirely from the persisted index - much quicker, in fact equivalent performance to selecting from a table.


What SQL Server SKU? Only Enterprise Edition considers indexed views in the query plan. Standard Edition will not consider the indexed view, unless the select is from the view and uses a NOEXPAND hint.

Update

Since I already got two comments indicating this is usefull to know, I'm linking the relevant MSDN page Resolving Indexes on Views:

Indexed views can be created in any edition of SQL Server. In SQL Server Enterprise, the query optimizer automatically considers the indexed view. To use an indexed view in all other editions, the NOEXPAND table hint must be used.

0

精彩评论

暂无评论...
验证码 换一张
取 消