sqlserver索引的原理及索引建立的注意事项小结
本文起点可能会相对高点,首先你的很熟悉索引以及他们的存储结构有很多地方你可能觉得有异议,欢迎一起讨论
聚集索引,数据实际上是按顺序存储的,数据页就在索引页上。就好像参考手册将所有主题按顺序编排一样。一旦找到了所要搜索的数据,就完成了这次搜索,对于非聚集索引,索引是安全独立于数据本身结构的,在索引中找到了寻找的数据,然后通过指针定位到实际的数据。SQL Server中的索引使用标准的B-树来存储他们的信息,如下图所示,B-树通过查找索引中的一个关键之来提供对于数据的快速访问,B-树以相似的键记录聚合在一起,B不代表二叉(binary),而是代表balanced(平衡的),而B-树的一个核心作用就是保持树的平衡。同伙向下遍历这棵树以找到一个数值并定位记录。因为树是平衡的,所以寻找任何记录都只需要等量的资源,而且获取的速度总是一致的—因为从根索引叶索引都具有相同的深度。
索引的中间层次是根据表的行数一级索引行的大小而变化的,如果使用一个较长的键(KEY)来创建索引,一个分页上就只容纳较少的条目,因而索引就需要更多分页(或者说更多层),页越多那么查找就需要话费相对较长的时间来找到所需要的信息,索引就可能不太有用了。
聚集索引
聚集索引的叶级别不仅包含了索引键,还包含了数据页。另一种说法数据本身也是聚集索引的一部分,聚集索引基于键值保持表中的数据有序,表中的数据页是通过一个被称作页链(page chain)的双向链接表来维护的,由于实际的数据页的页链只能按一种方式排序,因此一张表只能拥有一个聚集索引。
这里可能有一个误区,有很多介绍SQL Server索引的文档会告诉读者:聚集索引按照排序顺序(sorted order)物理地存储数据。如果以为物理存储就是磁盘本身的话就会产生误解。试想如果聚集索引需要按照特定顺序在实际的磁盘上维护数据的话,那么任何修改操作都将会产生相当高昂的代价。当一个页变满了需要一分为二的时候,所有后续页面上的数据都必须向后移动。聚集索引中的排序顺序(sorted order)仅仅表示数据页链在逻辑上是有序的。
大多数表都应该需要一个聚集索引。优化器非常倾向于采用聚集索引,因为聚集索引能够直接在叶级别找到数据。由于定义了数据的逻辑顺序,聚集索引能够特别快的访问针对范围值的查询,查询优化器能够发现只有某一段范围的数据页需要扫描。
非聚集索引
对于非聚集索引,叶级别不包含全部的数据。除了键值之外,每个叶级别(树的最底层)中的索引行包含了一个书签(bookmark),告诉SQL Server可以在那里找到与索引键相应的数据行。一个书签可能有两种形式。如果表上存在聚集索引,书签就是相应的数据行的聚集索引键。如果彪是堆(heap)结构,书签就是一个行表示(row identifier,RID),以“文件号:页号:槽号”的格式来定位实际的行。
主键(PRIMARY KEY)与聚集索引(CLUSTER INDEX)
严格来说,主键与聚集索引没有任何关系,如果要说有话,那就是表中没有聚集索引的时候,创建的主键默认就是聚集索引(除非有特别设置为NOCLUSTER)。
在主键与聚集索引的处理方面,注意以下事项:
1、主键不与聚集索引分离
2、聚集索引键列尽量避免使用int之外的数据类型
3、尽量避免使用复合主键
当表中不包含聚集索引时,表中的数据是无序的,这会降低数据检索效率。即使通过索引缩小了数据检索的范围,但由于数据本身是无序的,当从表中提取实际数据时,会产生频繁的定位问题,这也使得SQL Server基本上不会使用无聚集索引表中的索引来检索数据。
由于聚集索引是非聚集索引的行定位器,如果它不唯一,则会使行定位器中包含辅助数据,同时也导致从表中提取数据时,需要借助行定位器中的辅助数据来定位,这会降低处理效率。
每个聚集键值都是所有非聚集索引的叶结点记录,它越小,意味着每个非聚集索引的索引叶包含的有效数据越多,这对于提升索引效率很有好处。
覆盖索引是指索引中的列包含了数据处理中涉及的所有列,覆盖索引相当原始表的一个子集,由于这个子集中包含了数据处理涉及的所有列,因此操作这个子集就可以满足数据处理需要。一般而言,如果大多数处理都只涉及某个大表的某些列,可以考虑为这些列建立覆盖索引。
覆盖索引的建立方法是将要包含的列中的关键列做为索引键列,将其他列做为索引的包含列(使用索引创建语句中的INCLUDE子句)。
当数据发生变化时,SQL Server会同步维护相关索引中的数据,过多的索引会加影响数据变更的处理效率。因此,只应该在经常使用的列上建立索引。
适量的索引还体现在对索引列的组合方式的控制上。例如,如果有两个列col1和col2,这两个列的组合会产生三种使用情况:单独使用col1、单独使用col2及同时使用col1和col2。如果有为每种情况都建立索引,则需要建立三个索引。但也可以只建立一个复合索引(col1, col2),这样能够依次满足col1+col2、col1、col2这三种方式的查询,其中,col2利用这个查询会比较勉强(还要配合单独的统计),可以视实际情况确定是否需要为col2建立单独的索引。
特别注意:
不要建立重复索引,目前最常见的重复索引是单独为某个列建立主键和聚集索引
与直接从表中提取数据相比,根据索引检索数据,多了一个索引检索的过程,这个过程要求能够尽量缩小数据检索范围,并且使用最少的时间,这样才能真正保证能够通过索引提高数据检索效率。
实现上述目的,对于索引键列的选择,应该遵循如下原则:
选择性是满足条件的记录占总记录数的百分比,这个比率应该尽可能低,这样才能保证通过索引扫描后,只需要从基础表提取很少的数据。
如果这个比率偏高,则不应该考虑在此列上建立索引。
数据密度是指列值唯一的记录占总记录数的百分比,这个比率越高,则说明此列越适合建立索引。
在考虑数据密度的时候,还要注意数据分布的问题,只有经常检索的密度高时,才适合建立索引。例如,如果一张表有10万记录,虽然某个列不重复的记录有9万条,但如果经常检索的2万条记录,其不重复的列值才几十条的话,这个列是不太适合建立索引的。另一种情况是,整体数据密度不大,但经常检索的数据的密度大,例如订单的状态,一般来说,订单的状态就几种,但已经Close的订单往往占整个数据的绝大部分,但数据处理的时候,基本上都是检索未Close的订单,这种情况下,为订单的状态列建立索引还是比较有效的(SQL Server 2008中,可以为这种列建立具有更佳效果的筛选索引)。
一般不宜为超过100Byte的列建立索引。
在索引中,索引的顺序主要由索引中的每一个键列确定,因此,对于复合索引,索引中的列顺序是很重要的,应该优先把数据密度大,选择性列,存储空间小的列放在索引键列的前面。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Analysis of the role and principle of nohup In Unix and Unix-like operating systems, nohup is a commonly used command that is used to run commands in the background. Even if the user exits the current session or closes the terminal window, the command can still continue to be executed. In this article, we will analyze the function and principle of the nohup command in detail. 1. The role of nohup: Running commands in the background: Through the nohup command, we can let long-running commands continue to execute in the background without being affected by the user exiting the terminal session. This needs to be run

MyBatis is a popular Java persistence layer framework that is widely used in various Java projects. Among them, batch insertion is a common operation that can effectively improve the performance of database operations. This article will deeply explore the implementation principle of batch Insert in MyBatis, and analyze it in detail with specific code examples. Batch Insert in MyBatis In MyBatis, batch Insert operations are usually implemented using dynamic SQL. By constructing a line S containing multiple inserted values

Principle analysis and practical exploration of the Struts framework. As a commonly used MVC framework in JavaWeb development, the Struts framework has good design patterns and scalability and is widely used in enterprise-level application development. This article will analyze the principles of the Struts framework and explore it with actual code examples to help readers better understand and apply the framework. 1. Analysis of the principles of the Struts framework 1. MVC architecture The Struts framework is based on MVC (Model-View-Con

The RPM (RedHatPackageManager) tool in Linux systems is a powerful tool for installing, upgrading, uninstalling and managing system software packages. It is a commonly used software package management tool in RedHatLinux systems and is also used by many other Linux distributions. The role of the RPM tool is very important. It allows system administrators and users to easily manage software packages on the system. Through RPM, users can easily install new software packages and upgrade existing software

MyBatis is an excellent persistence layer framework. It supports database operations based on XML and annotations. It is simple and easy to use. It also provides a rich plug-in mechanism. Among them, the paging plug-in is one of the more frequently used plug-ins. This article will delve into the principles of the MyBatis paging plug-in and illustrate it with specific code examples. 1. Paging plug-in principle MyBatis itself does not provide native paging function, but you can use plug-ins to implement paging queries. The principle of paging plug-in is mainly to intercept MyBatis

The chage command in the Linux system is a command used to modify the password expiration date of a user account. It can also be used to modify the longest and shortest usable date of the account. This command plays a very important role in managing user account security. It can effectively control the usage period of user passwords and enhance system security. How to use the chage command: The basic syntax of the chage command is: chage [option] user name. For example, to modify the password expiration date of user "testuser", you can use the following command

Table of Contents Astar Dapp Staking Principle Staking Revenue Dismantling of Potential Airdrop Projects: AlgemNeurolancheHealthreeAstar Degens DAOVeryLongSwap Staking Strategy & Operation "AstarDapp Staking" has been upgraded to the V3 version at the beginning of this year, and many adjustments have been made to the staking revenue rules. At present, the first staking cycle has ended, and the "voting" sub-cycle of the second staking cycle has just begun. To obtain the "extra reward" benefits, you need to grasp this critical stage (expected to last until June 26, with less than 5 days remaining). I will break down the Astar staking income in detail,

The basic principles and implementation methods of Golang inheritance methods In Golang, inheritance is one of the important features of object-oriented programming. Through inheritance, we can use the properties and methods of the parent class to achieve code reuse and extensibility. This article will introduce the basic principles and implementation methods of Golang inheritance methods, and provide specific code examples. The basic principle of inheritance methods In Golang, inheritance is implemented by embedding structures. When a structure is embedded in another structure, the embedded structure has embedded
