蝈蝈俊.net

-- 用随笔来记录自己的技术感触
随笔 - 655, 评论 - 4839, 引用 - 276

导航

关于

这里是我的技术Blog,下一代CSDN社区Blog在 http://blog.csdn.net/ghj1976/

标签

每月存档

最新留言

  • re:WPF/Silverlight 控件的几幅继承关系图
    <p class="MsoNormal" style="text-align: center; line-height: 20pt; margin: 0cm 0cm...
    by oaoffices(注册) on 2010/2/7 18:38:37
  • tWTqXHvjmOmv
    XZ5W2T &lt;a href=&quot;http://zecclkdejwou.com/&quot;&gt;zecclkdejwou&lt;/a&am...
    by uwwkhptujtw(匿名) on 2010/2/7 11:26:16
  • Re
    Many times we wanted to order significant example thesis close to this topic in the &lt;a href=&...
    by Ellieru(匿名) on 2010/2/6 10:25:37
  • Rrwvthsw
    Very Good Site &lt;a href=&quot; http://www.newsland.it/nr/article/it-alt.faq/4163.html &...
    by Rrwvthsw(匿名) on 2010/2/6 5:51:43
  • Edozadcx
    Good crew it's cool :) &lt;a href=&quot; http://www.newsland.it/nr/article/it-alt.faq/4163.h...
    by Edozadcx(匿名) on 2010/2/6 3:56:25
  • Ptryjfhi
    i'm fine good work &lt;a href=&quot; http://www.newsland.it/nr/article/it-alt.faq/4143.html ...
    by Ptryjfhi(匿名) on 2010/2/6 2:52:34
  • samuel
    5l05ro http://www.cRk2bdPqQls602mIa4bgo.com
    by samuel(匿名) on 2010/2/5 4:00:27
  • samuel
    5l05ro http://www.cRk2bdPqQls602mIa4bgo.com
    by samuel(匿名) on 2010/2/5 4:00:25
  • qCSFwFqaMVQizlCas
    2dnR2u &lt;a href=&quot;http://geunkaxpmhqt.com/&quot;&gt;geunkaxpmhqt&lt;/a&am...
    by spahxbypafn(匿名) on 2010/2/5 1:47:48
  • Re
    Very oft the &lt;a href=&quot;http://quality-papers.com/topics/comparison_essay&quot;&am...
    by Gwen18uV(匿名) on 2010/2/4 16:16:33
  • samuel
    SnU3S6 http://www.cRk2bdPqQls602mIa4bgo.com
    by samuel(匿名) on 2010/2/4 15:37:58
  • Koxtggnh
    It's serious &lt;a href=&quot; http://www.newsland.it/nr/article/it-alt.faq/3298.html &q...
    by Koxtggnh(匿名) on 2010/2/4 6:09:20
  • SrehssllMZjp
    XefKdm &lt;a href=&quot;http://onztwqzldpty.com/&quot;&gt;onztwqzldpty&lt;/a&am...
    by fsfirjqclpn(匿名) on 2010/2/4 0:43:23
  • OLDLqAKsuD
    BnPVvu &lt;a href=&quot;http://jzyknfzplvon.com/&quot;&gt;jzyknfzplvon&lt;/a&am...
    by ofudghioo(匿名) on 2010/2/4 0:07:18
  • Hwbjmqub
    Punk not dead &lt;a href=&quot; http://www.newsland.it/nr/article/it-alt.faq/3238.html &...
    by Hwbjmqub(匿名) on 2010/2/3 14:14:20
  • Re
    From time to time different students have assignments to compose the &lt;a href=&quot;http:/...
    by Rebeccamp28(匿名) on 2010/2/3 11:04:43
  • emRTIwMZOMDFfCg
    nQf5M6 &lt;a href=&quot;http://eaykuvqnrful.com/&quot;&gt;eaykuvqnrful&lt;/a&am...
    by pnxjuwq(匿名) on 2010/2/2 23:36:36
  • jPRCuLgHySwb
    qc0qb5 &lt;a href=&quot;http://ciktzfijfsfj.com/&quot;&gt;ciktzfijfsfj&lt;/a&am...
    by yuplvewx(匿名) on 2010/2/2 19:05:07
  • re:IP to Integer
    IPV6呢?
    by howie(匿名) on 2010/2/2 10:34:10
  • IcqpxCPAVoKn
    Ff4AZo &lt;a href=&quot;http://bovwngagtlua.com/&quot;&gt;bovwngagtlua&lt;/a&am...
    by mbnbusiuh(匿名) on 2010/2/1 18:40:41
  • tJZDsNznVk
    scXyAq &lt;a href=&quot;http://pqtnkiwdacoa.com/&quot;&gt;pqtnkiwdacoa&lt;/a&am...
    by lwummgsncve(匿名) on 2010/2/1 3:53:51
  • Okurlpzt
    i'm fine good work &lt;a href=&quot; http://littlelolitasmodels.vox.com/ &quot;&gt;p...
    by Okurlpzt(匿名) on 2010/1/31 19:30:08
  • Sgoonhvn
    Excellent work, Nice Design &lt;a href=&quot; http://littlelolitasmodels.vox.com/ &quot;...
    by Sgoonhvn(匿名) on 2010/1/31 18:29:47
  • Zloozuuj
    Punk not dead &lt;a href=&quot; http://lolitatoplist.vox.com/ &quot;&gt;lolita mpeg...
    by Zloozuuj(匿名) on 2010/1/31 17:52:04
  • Jmqskdlf
    I love this site &lt;a href=&quot; http://younglolitasbbs.vox.com/ &quot;&gt;preetee...
    by Jmqskdlf(匿名) on 2010/1/31 15:09:12
  • Tmmhaxbr
    Wonderfull great site &lt;a href=&quot; http://lolitabbs969.vox.com &quot;&gt;lolita...
    by Tmmhaxbr(匿名) on 2010/1/31 14:31:36
  • Asnejtjx
    real beauty page &lt;a href=&quot; http://underagelolitas.vox.com/ &quot;&gt;top 50 ...
    by Asnejtjx(匿名) on 2010/1/31 12:51:18
  • Tibtihsu
    &lt;a href=&quot; http://lolitamodels.vox.com/ &quot;&gt;real young lolitas&lt;...
    by Tibtihsu(匿名) on 2010/1/31 7:50:15
  • Jlcdzoem
    Very Good Site &lt;a href=&quot; http://lolitamodels.vox.com/ &quot;&gt;kds pedo lol...
    by Jlcdzoem(匿名) on 2010/1/31 6:47:06
  • Eqczveev
    i'm fine good work &lt;a href=&quot; http://mipagina.univision.com/preteenlolitabbs/ &qu...
    by Eqczveev(匿名) on 2010/1/31 6:08:16
  • Ndhvewjd
    Very interesting tale &lt;a href=&quot; http://mipagina.univision.com/preteenlolitabbs/ &...
    by Ndhvewjd(匿名) on 2010/1/31 5:06:34
  • Jktojpnv
    &lt;a href=&quot; http://mipagina.univision.com/preteenlolitamodels/ &quot;&gt;goth...
    by Jktojpnv(匿名) on 2010/1/31 3:27:26
  • Lftiemmn
    Very interesting tale &lt;a href=&quot; http://mipagina.univision.com/preteenyounglolitas/ &...
    by Lftiemmn(匿名) on 2010/1/31 2:51:02
  • ITYAtUVjqijsAcZ
    4SnRK4 &lt;a href=&quot;http://lbmuqlnscvjk.com/&quot;&gt;lbmuqlnscvjk&lt;/a&am...
    by pqoqxfvrbcb(匿名) on 2010/1/30 19:25:06
  • bjhbHxudMahxGCkiue
    3mRPzg &lt;a href=&quot;http://rzitclgaemis.com/&quot;&gt;rzitclgaemis&lt;/a&am...
    by quglkot(匿名) on 2010/1/30 19:20:51

广告

 

由于需要给同事培训数据库的索引知识,就收集整理了这个系列的博客。发表在这里,也是对索引知识的一个总结回顾吧。通过总结,我发现自己以前很多很模糊的概念都清晰了很多。

不论是 聚集索引,还是非聚集索引,都是用B+树来实现的。我们在了解这两种索引之前,需要先了解B+树。如果你对B树不了解的话,建议参看以下几篇文章:

BTree,B-Tree,B+Tree,B*Tree都是什么
http://blog.csdn.net/manesking/archive/2007/02/09/1505979.aspx

B+ 树的结构图:

B+ 树的特点:

  • 所有关键字都出现在叶子结点的链表中(稠密索引),且链表中的关键字恰好是有序的;
  • 不可能在非叶子结点命中;
  • 非叶子结点相当于是叶子结点的索引(稀疏索引),叶子结点相当于是存储(关键字)数据的数据层;

B+ 树中增加一个数据,或者删除一个数据,需要分多种情况处理,比较复杂,这里就不详述这个内容了。 

聚集索引(Clustered Index)

  • 聚集索引的叶节点就是实际的数据页
  • 在数据页中数据按照索引顺序存储
  • 行的物理位置和行在索引中的位置是相同的
  • 每个表只能有一个聚集索引
  • 聚集索引的平均大小大约为表大小的5%左右

下面是两副简单描述聚集索引的示意图: 

在聚集索引中执行下面语句的的过程:

select * from table where firstName = 'Ota'

 在聚集索引中搜索

一个比较抽象点的聚集索引图示:

聚集索引单个分区中的结构

 

非聚集索引 (Unclustered Index)  

  • 非聚集索引的页,不是数据,而是指向数据页的页。
  • 若未指定索引类型,则默认为非聚集索引
  • 叶节点页的次序和表的物理存储次序不同
  • 每个表最多可以有249个非聚集索引
  • 在非聚集索引创建之前创建聚集索引(否则会引发索引重建)

在非聚集索引中执行下面语句的的过程:

select * from employee where lname = 'Green'

Selecting rows using a nonclustered index

一个比较抽象点的非聚集索引图示:

非聚集索引的级别

 

什么是 Bookmark Lookup

虽然SQL 2005 中已经不在提  Bookmark Lookup 了(换汤不换药),但是我们的很多搜索都是用的这样的搜索过程,如下:
先在非聚集中找,然后再在聚集索引中找。

Bookmark Lookup 

http://www.sqlskills.com/ 提供的一个例子中,就给我们演示了 Bookmark Lookup  比 Table Scan 慢的情况,例子的脚本如下:

USE CREDIT
go

-- These samples use the Credit database. You can download and restore the
-- credit database from here:
-- http://www.sqlskills.com/resources/conferences/CreditBackup80.zip

-- NOTE: This is a SQL Server 2000 backup and MANY examples will work on 
-- SQL Server 2000 in addition to SQL Server 2005.
-------------------------------------------------------------------------------
-- (1) Create two tables which are copies of charge:
-------------------------------------------------------------------------------

-- Create the HEAP
SELECT * INTO ChargeHeap FROM Charge
go

-- Create the CL Table
SELECT * INTO ChargeCL FROM Charge
go

CREATE CLUSTERED INDEX ChargeCL_CLInd ON ChargeCL (member_no, charge_no)
go

-------------------------------------------------------------------------------
-- (2) Add the same non-clustered indexes to BOTH of these tables:
-------------------------------------------------------------------------------

-- Create the NC index on the HEAP
CREATE INDEX ChargeHeap_NCInd ON ChargeHeap (Charge_no)
go

-- Create the NC index on the CL Table
CREATE INDEX ChargeCL_NCInd ON ChargeCL (Charge_no)
go

-------------------------------------------------------------------------------
-- (3) Begin to query these tables and see what kind of access and I/O returns
-------------------------------------------------------------------------------

-- Get ready for a bit of analysis:
SET STATISTICS IO ON
-- Turn Graphical Showplan ON (Ctrl+K)

-- First, a point query (also, see how a bookmark lookup looks in 2005)
SELECT * FROM ChargeHeap WHERE Charge_no = 12345
go

SELECT * FROM ChargeCL WHERE Charge_no = 12345
go

-- What if our query is less selective?
-- 1000 is .0625% of our data... (1,600,000 million rows)
SELECT * FROM ChargeHeap WHERE Charge_no < 1000
go

SELECT * FROM ChargeCL WHERE Charge_no < 1000
go

-- What if our query is less selective?
-- 16000 is 1% of our data... (1,600,000 million rows)
SELECT * FROM ChargeHeap WHERE Charge_no < 16000
go

SELECT * FROM ChargeCL WHERE Charge_no < 16000
go

-------------------------------------------------------------------------------
-- (4) What's the EXACT percentage where the bookmark lookup isn't worth it?
-------------------------------------------------------------------------------

-- What happens here: Table Scan or Bookmark lookup?
SELECT * FROM ChargeHeap WHERE Charge_no < 4000
go

SELECT * FROM ChargeCL WHERE Charge_no < 4000
go

-- What happens here: Table Scan or Bookmark lookup?
SELECT * FROM ChargeHeap WHERE Charge_no < 3000
go

SELECT * FROM ChargeCL WHERE Charge_no < 3000
go

-- And - you can narrow it down by trying the middle ground:
-- What happens here: Table Scan or Bookmark lookup?
SELECT * FROM ChargeHeap WHERE Charge_no < 3500
go

SELECT * FROM ChargeCL WHERE Charge_no < 3500
go

-- And again:
SELECT * FROM ChargeHeap WHERE Charge_no < 3250
go

SELECT * FROM ChargeCL WHERE Charge_no < 3250
go

-- And again:
SELECT * FROM ChargeHeap WHERE Charge_no < 3375
go

SELECT * FROM ChargeCL WHERE Charge_no < 3375
go

-- Don't worry, I won't make you go through it all :)



-- For the Heap Table (in THIS case), the cutoff is: 0.21%
SELECT * FROM ChargeHeap  WHERE Charge_no < 3383
go
SELECT * FROM ChargeHeap WHERE Charge_no < 3384
go


-- For the Clustered Table (in THIS case), the cut-off is: 0.21%
SELECT * FROM ChargeCL WHERE Charge_no < 3438

SELECT * FROM ChargeCL WHERE Charge_no < 3439
go

这个例子也就是 吴家震 在Teched 2007 上的那个演示例子。

小结:

这篇博客只是简单的用几个图表来介绍索引的实现方法:B+数, 聚集索引,非聚集索引,Bookmark Lookup 的信息而已。

参考资料:

表组织和索引组织
http://technet.microsoft.com/zh-cn/library/ms189051.aspx
http://technet.microsoft.com/en-us/library/ms189051.aspx

How Indexes Work
http://manuals.sybase.com/onlinebooks/group-asarc/asg1200e/aseperf/@Generic__BookTextView/3358

Bookmark Lookup
http://blogs.msdn.com/craigfr/archive/2006/06/30/652639.aspx 

Logical and Physical Operators Reference
http://msdn2.microsoft.com/en-us/library/ms191158.aspx

打印 | 张贴于 2008-01-02 14:38:00 | Tag:数据库开发管理心得

留言反馈

#SrehssllMZjp 编辑
XefKdm <a href="http://onztwqzldpty.com/">onztwqzldpty</a>, [url=http://fbtekwofcmnu.com/]fbtekwofcmnu[/url], [link=http://hpqkzxxhlvew.com/]hpqkzxxhlvew[/link], http://kansejlotthb.com/
2010-02-04 00:43:23 | [匿名:fsfirjqclpn]
#OLDLqAKsuD 编辑
BnPVvu <a href="http://jzyknfzplvon.com/">jzyknfzplvon</a>, [url=http://hvxnuutpjqjc.com/]hvxnuutpjqjc[/url], [link=http://lpqxwerrswps.com/]lpqxwerrswps[/link], http://qlpnyormmkcj.com/
2010-02-04 00:07:18 | [匿名:ofudghioo]
#Re 编辑
From time to time different students have assignments to compose the <a href="http://www.exclusivepapers.com/writing-services.php">writing services</a> essay related to this good topic. But lots of them are not experienced research paper writers. Thus, what should they do under such circumstances? We instantly advice to <a href="http://www.exclusivepapers.com/buy-essay.php">buy essay</a> in the essays writing service.
2010-02-03 11:04:43 | [匿名:Rebeccamp28]
#回复: SQL Server 索引基础知识(2)----聚集索引,非聚集索引 编辑
学习!
2008-02-19 16:22:00 | [匿名:111]
#数据库优化 编辑
由于想学习怎样优化数据库,所以找了些资料,并整理如下: SQLServer索引基础知识(2)----聚集索引,非聚集索引很好的文章,很详细,值得研究! 海量数据库的查询优化及分页算法方案...
2008-02-03 09:36:00 | [匿名:Rocky84]
#SQL Server 索引基础知识(6)----索引的代价,使用场景 编辑
前几天给同事培训了聚集索引,非聚集索引的知识后,在一个同事新作的项目中,竟然出现了滥用聚集索引的问题。看来没有培训最最基础的索引的意义,代价,使用场景,是一个非常大的失误。这篇博客就是从这个角度来罗列索引的基础知识
2008-01-19 02:08:00 | [匿名:ASP.NET Chinese Blogs]
#SQL Server 索引基础知识(3)----测试中一些常看的指标和清除缓存的方法 编辑
之前的两篇博客中有2个例子,来演示要讲述的内容。其中提到了部分查看数据库状态的方法,那里并不是很全面,这篇博客罗列几个我们在后面系列博客中会用到查看这些状态,数据的地方。以及测试中清除缓存的方法。 前面两篇博客的链接地址如下
2008-01-19 02:08:00 | [匿名:ASP.NET Chinese Blogs]
#Interesting Finds: 2008.01.03 编辑
.NET: Questionsevery.NETdevelopershouldbeabletoanswer Other: Disablemessagepopupa...
2008-01-06 11:13:00 | [匿名:gOODiDEA]
#[导入]SQL Server 索引基础知识(3)----测试中一些常看的指标和清除缓存的方法 编辑
之前的两篇博客中有2个例子,来演示要讲述的内容。其中提到了部分查看数据库状态的方法,那里并不是很全面,这篇博客罗列几个我们在后面系列博客中会用到查看这些状态,数据的地方。以及测试中清除缓存的方法。前面...
2008-01-04 22:30:00 | [匿名:HappyQQ]
#SQL Server 索引基础知识(3)----测试中一些常看的指标和清除缓存的方法 编辑
之前的两篇博客中有2个例子,来演示要讲述的内容。其中提到了部分查看数据库状态的方法,那里并不是很全面,这篇博客罗列几个我们在后面系列博客中会用到查看这些状态,数据的地方。以及测试中清除缓存的方法。前面两篇博客的链接地址如下: SQL Server 索引基础知识(1)--- 记录数据的基本格式 http://blog.joycode.com/ghj/archive/2008/01/02/113290.aspx SQL Server 索引基础知识(2)----聚集索引,非聚集索引 http://blog.joycode.com/ghj/archive/2008/01/02/113291.aspx...
2008-01-04 10:34:00 | [匿名:ghj1976]
#回复: SQL Server 索引基础知识(2)----聚集索引,非聚集索引 编辑
受益匪浅
2008-01-03 16:09:00 | [匿名:felix.zhu]
#回复: SQL Server 索引基础知识(2)----聚集索引,非聚集索引 编辑
好文章,我只有学习的份儿
2008-01-03 14:18:00 | [匿名:RickyLin]
#回复: SQL Server 索引基础知识(2)----聚集索引,非聚集索引 编辑
学习。
2008-01-03 14:17:00 | [匿名:RickyLin]
#回复: SQL Server 索引基础知识(2)----聚集索引,非聚集索引 编辑
>聚集索引的平均大小大约为表大小的5%左右
聚集索引的叶节点就是实际的数据页,所以聚集索引比表大小要大。
2008-01-02 15:08:00 | [匿名:怡红公子]

发表留言

标题
姓名
邮件
主页
留言 

Powered by: Joycode.MVC引擎 0.5.2.0