2012-08-28 135 views
0

我测试非聚簇索引的好处。如何执行非聚簇索引查找而不是聚簇索引扫描

我用分贝的AdventureWorks 当我执行查询:

SELECT [address].City, [address].[AddressLine1] 
FROM [AdventureWorks].[Person].[Address] as [address] 
WHERE [address].City = 'Seattle' 

我在执行计划选项卡

/* 
Missing Index Details from SQLQuery3.sql - 
The Query Processor estimates that implementing the following index could improve the query cost by 97.9636%. 
*/ 

/* 
USE [AdventureWorks] 
GO 
CREATE NONCLUSTERED INDEX [<Name of Missing Index, sysname,>] 
ON [Person].[Address] ([City]) 

GO 
*/ 

我在执行简单的标签图标看的见“聚集索引扫描”和我知道这是不好的,因为指数寻求更好

但是,当我执行查询

USE [AdventureWorks] 
GO 
CREATE NONCLUSTERED INDEX CityIdx 
ON [Person].[Address] ([City]) 

GO 

我仍然看到在执行平原选项卡“聚集索引扫描”。为什么不“聚集索引查找”?它应该是“聚集索引查找”吗?在哪些情况下,它应该是“聚集索引查找”。

+1

是“聚集”,而不是“分类”。 –

+0

谢谢我修复了这些错别字 – testCoder

回答

5

你击中index tipping point:有太多的条目与City = 'Seattle'打扰求,为每一个在聚集索引AddressLine1。一种方法,特定的查询,是包括投影柱:

CREATE NONCLUSTERED INDEX CityIdx 
ON [Person].[Address] ([City]) 
INCLUDE ([AddressLine1]); 

但是,隐藏真正问题,即为什么是你有兴趣在这样一个选择所有行非选择性谓词?应用程序不应该提出这样的请求。

+0

为什么它不是选择性谓词? – testCoder

+0

因为在谓词'City ='Seattle''上选择将返回许多条目。 –

+0

在这种情况下,我应该做些什么来检索使用索引的城市'Seatle'的所有行? – testCoder

相关问题