3

I am running a sample database from a SQL Server 2012 training book, learning a bit about indexes so I thought I'd try and see how it works.

The table Orders has an index on shippostalcode, so I tried to put that in the where clause, I know there are 850+ records in that table, and this code is split well like:

10328
10195
10342
10318
10196
10139
10294
10354
10199
10157
10137
10258
10274
10286
10158
10167
10329
10351

So when I do this, why do I get a clustered index scan, which I read is the same as a table scan which is sub-optimal?

enter image description here

EDIT:

I did :

SELECT [orderid] 
      ,[shippostalcode]
  FROM [TSQL2012].[Sales].[Orders]
  WHERE shippostalcode = '10307'

Which gave me an index seek. Which leads me to think (from the little I learnt) that SQL Server may think that a scan is faster than doing a bookmark lookup for the rest of the columns?

EDIT 2:

After reading about the tipping point here : http://www.sqlskills.com/blogs/kimberly/why-arent-those-nonclustered-indexes-being-used/

It says its around 30%. I used the query from here to get the total pages in my table :

Determining page count on each SQL table without using DBCC

I get 49 used and 53 reserved. So 30% of 49 = 14.7 * 17 (each page stores this much) is about 250 rows. But my query was only returning 9 so it was no where near the tipping point.

4

1 回答 1

0

我已经弄清楚了,正如我阅读的一些文章中提到的,选择性必须低于 1%。我被一篇关于临界点的文章误导了,认为只有在返回 30%+ 时才会进入扫描模式

但实际测试表明,返回 6+ 行的任何内容都是扫描,而返回 6 行以上的内容都是搜索。

因此,如果它返回大约 0.5%,它将进行搜索,否则进行扫描。我的 830 行中有 0.5% 是 4.15 行。

希望这可以帮助某人。

于 2013-11-15T13:11:02.933 回答