sqlserver 索引的结构及其存储,索引内容

您所在的位置:网站首页 索引页是在哪里 sqlserver 索引的结构及其存储,索引内容

sqlserver 索引的结构及其存储,索引内容

2024-04-19 14:35| 来源: 网络整理| 查看: 265

sqlserver 索引的结构及其存储,sql server索引内容

文章转载,原文地址: http://www.cnblogs.com/panchunting/p/SQLServer_IndexStructure.html

 

本文关注以下方面(本文所有的讨论基于SQL Server数据库):

索引的分类; 索引的结构; 索引的存储

一、索引​​定义分类

  让我们先来回答几个问题:  

什么是索引? 索引是对数据库表中一列或多列的值进行排序的一种结构,使用索引可快速访问数据库表中的特定信息。 举个例子,索引就像我们查字典时用的按拼音或笔画或偏旁部首 有哪些索引? 从物理结构上可分为两种:聚集索引和非聚集索引(此外还有空间索引、筛选索引、XML索引) 索引说明( http://msdn.microsoft.com/zh-cn/library/ms190197(v=sql.105).aspx ) 每张表上最大的聚集索引数为1; 每张表上最大的非聚集索引数为999; 每个索引最多能包含的键列数为16; 索引键记录大小最多为900字节

 

二、索引数据结构

  在SQL Server数据库中,索引的存储是以B+树(注意和二叉树的区别)结构来存储的,又称索引树,其节点类型为如下两种:

索引节点; 叶子节点

  索引节点按照层级关系,有时又可以分为根节点和中间节点,其本质是一样的,都只包含下一层节点的入口值和入口指针;

  叶子节点就不同了,它包含数据,这个数据可能是表中真实的数据行,也有可能是索引列值和行书签,前者对应于聚集索引,后者对应于非聚集索引。

 

三、索引存储结构

  在正式讨论索引的存储结构之前,我们有必要先来了解一下SQL Server数据库的存储结构。

  SQL Server数据库存储(结构)的最小单位是页,大小为8K,共8 * 1024 = 8192Byte,不论是数据页还是索引页都是以此方式存放。实际上对于SQL Server数据库而言,其页(Page)类型有很多种,大概有如下十几种(http://www.sqlnotes.info/2011/10/31/page-type/):

Type 1 – Data page. Data records in heap Clustered index leaf-level Location can be random Type 2 – Index page Non-clustered index Non-leave-level clustered index Location can be random Type 3 – Text Mixed Page Small LOB value(s), multiple types and rows. Location can be random Type 4 – Text Page LOB value from a single column value Location can be random Type 7 – Sort Page Temporary page for sort operation. Usually tempdb, but can be in user database for online operations. Location can be random Type 8 – GAM Page Global Allocation Map, track allocation of extents. One bit for each extent, if the bit is 1, means the extent is free, otherwise means the extent is allocated (not necessary full). The first GAM page in each file is page 2 Type 9 – SGAM Page Shared Global Allocation Map, track allocation of shared extents One bit for each extent, if the bit is 1, means the extent is allocated but has free space, otherwise means the extent is full The first SGAM page in each file is page 3 Type 10 – IAM Page Index Allocation Map. Extent allocation in a GAM interval for an index or heap table. Location can be random. Type 11 – PFS Page Page Free Space. Byte map, keeps track of free space of pages The first PFS is page 1 in each file. Type 13 – Boot Page Information about the page Only page 9 in file 1. Type 14 – Server Configuration Page (It may not be the official name) Part of information returned from sp_configure. It only exists in master database, file 1, page 10 SQL Server 2008 Only Type 15 – File Header Page Information about the file. It's always page 0 every data page. Type 16 – Differential Changed map Extents in GAM interval have changed since last full or differential backup The first Differential Changed Page is page 6 in each file Type 17 – Bulk Change Map Extents in GAM interval modified by bulk operations since last backup The first Bulk Change Map page is page 7 in each file

  表中所有数据页的存放在磁盘上又有两种组织方式:

堆表; 索引组织表

  如果表中所有数据页是以一种页间无序、随机存储的方式,则称这样的表为堆表;

  否则如果表中数据页间按某种方式(如表中某个字段)有序地存储与磁盘上,则称为索引组织表。

 

四、聚集索引

  下面我们将深入研究一下数据库中的索引到底是如何存储的以及如何被使用的。

  为了测试验证等,我们在数据库PCT上新建一张测试表Employee,有两个字段,其中EmployeeId为主键

USE PCT CREATE TABLE Employee ( EmployeeId NVARCHAR ( 32 ) NOT NULL PRIMARY KEY , EmployeeName NVARCHAR ( 40 ) NOT NULL , );

  插入10W笔测试数据

复制代码 SET NOCOUNT ON declare @i int set @i = 1 while @i


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3