BUG: The query does not use the indexed view in SQL Server 2005 Enterprise Edition (912389)
The information in this article applies to:
- Microsoft SQL Server 2005 Enterprise Edition
BUG #: 414902 (SQLBUDT) BUG #: 416347 (SQLBUDT) SYMPTOMSWhen you define an indexed view that involves an ISNULL
function on a nonnullable column in Microsoft SQL Server 2005 Enterprise Edition, the
query does not use the indexed view. The query does not use the indexed view even if the predicate in the query is
identical to the predicate in the indexed view. SQL Server 2005 Enterprise
Edition creates an execution plan for the query to access the base tables
instead of taking advantage of the indexed view.CAUSEThis problem occurs because SQL Server 2005 Enterprise Edition
automatically removes the ISNULL function on the nonnullable column from the
query. Therefore, the SQL Server 2005 Enterprise Edition optimizer cannot
match the ISNULL function from the view definition to the query where the
unnecessary ISNULL function was removed.WORKAROUNDTo work around this problem, you can drop the view, rewrite
the view definition without using the ISNULL function on the nonnullable
column, and then re-create the index. This lets SQL Server 2005
Enterprise Edition match the indexed view to the query. Note that
this does not change the semantics of the view because only the nonnullable
column is affected. The following sample code provides a workaround for
this problem. Note Before you run the following sample code, you must run the code in the "More information" section to set up the working environment. Otherwise, the
following sample code does not work. USE AdventureWorksDW
GO
--Remove the view if it exists.
IF EXISTS (
SELECT *
FROM INFORMATION_SCHEMA.VIEWS
WHERE TABLE_NAME = 'VProdQty')
DROP VIEW VProdQty
GO
CREATE VIEW VProdQty
WITH SCHEMABINDING
AS
SELECT ProductKey, SUM(OrderQuantity) qty, COUNT_BIG(*) cnt
FROM dbo.factResellerSales
GROUP BY ProductKey
GO
-- Re-create the index.
CREATE UNIQUE CLUSTERED INDEX VProdQty_idx
ON VProdQty(ProductKey)
GO
-- The query now uses the indexed view.
SELECT ProductKey, SUM(ISNULL(OrderQuantity, 0)) qty
FROM dbo.factResellerSales
GROUP BY ProductKey
ORDER BY qty DESC
GO STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are
listed in the "Applies to" section.
Modification Type: | Major | Last Reviewed: | 1/11/2006 |
---|
Keywords: | kbsql2005tsql kbbug KB912389 kbAudDeveloper kbAudITPRO |
---|
|