Home Database Mysql Tutorial 你了解SQL的索引原理吗

你了解SQL的索引原理吗

Jun 07, 2016 pm 04:24 PM
sql learn principle article index

上篇文章粗略的总结了些SQL聚集索引与非聚集索引的区别,但看起来好像不太清晰,这篇我通过索引原理来再一次分析下。 索引是为检索而存在的,就是说索引并不是一个表必须的。表索引由多个页面组成,这些页面一起组成了一个树形结构,即我们通常说的B树,首先

上篇文章粗略的总结了些SQL聚集索引与非聚集索引的区别,但看起来好像不太清晰,这篇我通过索引原理来再一次分析下。

索引是为检索而存在的,就是说索引并不是一个表必须的。表索引由多个页面组成,这些页面一起组成了一个树形结构,即我们通常说的B树,首先来看下表索引的组成部分:

根极节点,root,它指向另外两个页,把一个表的记录从逻辑上分成非叶级节点Non-Leaf Level(枝),它指向了更加小的叶级节点Leaf Level(叶)。 根节点、非叶级节点和叶级节点都位于索引页中,统称为索引叶节点,属于索引页的范筹。这些"枝"、"叶"最终指向数据页Page。根级节点和叶级节点之间的叶又叫数据中间页。根节点对应了sysindexes表的Root字段,记载了非叶级节点的物理位置(即指针);非叶级节点位于根节点和叶节点之间,记载了指向叶级节点的指针;而叶级节点则最终指向数据页,这就是最后的B树。

数据库是怎样访问表数据的:

第一:没有创建任何索引的表。

这种表我们称为堆表,因为所有的数据页都是无序的,杂乱无章的,在查询数据时,需要一条一条记录查询,有时第一条记录就能找到,最坏的情况是在最后一条记录中查找到,但是千万不要认为SQL此时查找到数据后会当成结果立即返回,SQL即使查找到了记录,也会将所有数据遍历一次,这能从最终的执行计划中得知,就是平时说的表扫描,对于没有索引的表也能查询,就是效率会特别低,如果数据量稍大的话。

问题:SQL是如何得知表没有索引呢?

SQL在接到查询请求的时候,会分析sysindexes表中索引标志符(INDID: Index ID)的字段的值,如果该值为0,表示这是一张数据表而不是索引表,SQL就会使用sysindexes表的另一个字段FirstIAM值中找到该表的IAM 页链也就是所有数据页集合。至于什么是IAM,大家可以网上搜索下。

第二:访问创建有非聚集索引的表。

非聚集索引可以建多个,形成B树结构,叶级节点不包含数据页,只包含索引行。如果表中只有非聚集索引,则每个索引行包含了非聚集索引键值以及行定位符(ROW ID,RID),他们指向具有该键值的数据行。RID由文件ID、页编号和在页中行的编号组成。当 INDID的值在2-250之间时,说明表中存在非聚集索引页。SQL调用ROOT字段的值指向非聚集索引B树的ROOT,查找与被查询最相近的值,根据这个值找到在非叶级节点中的页号,在叶级节点相应的页面中找到该值的RID,最后根据这个RID在Heap中定位所在的页和行并返回到查询端。

上篇文章的cityid上建立了非聚集索引,执行Select * From student Where cityid='0101'时,查询过程是:

  1. 在sysindexes表查询INDID值为2,说明有非聚集索引;
  2. 从根出发,在非叶级节点中定位最接近0101的值(枝节点),查到其位于叶级页面的第n页;
  3. 在叶级页面的第n页下搜寻0101的RID,其RID显示为N∶i∶j,表示cityid字段中名为0101的记录位于堆的第i页的第j行,N代表文件的ID值。
  4. 在堆的第 i页第j行将该记录返回给客户端。

你了解SQL的索引原理吗

第三:访问创建有聚集索引的表。

聚集索引中,数据所在的数据页是叶级,索引数据所在的索引页是非叶级。原理和上述非聚集索引的查询差不多,由于记录是按聚集索引键值进行排序,即聚集索引的索引键值也就是具体的数据页。这种情况比起非聚集索引要简单很多,因为比非聚集索引少了一层节点查询。

上篇文章的username字段上建立了聚集索引,此时执行Select* From student Where username='1'时,查询过程是:

  1. 在sysindexes表查询INDID值为1,说明表中建立了聚集索;
  2. 从根出发,在非叶级节点中定位最接近1的值(枝节点),再查到其位于叶级页面的第n页;
  3. 在叶级页面第n页下搜寻值为1的条目,而这一条目就是数据记录本身;
  4. 将该记录返回客户端。

下图可做参考:

你了解SQL的索引原理吗

第四:怎样访问既有聚集索引、又有非聚集索引的数据表:

username字段上建立了聚集索引,cityid上建立了非聚集索引,当执行Select * From student Where cityid='0101'时,查询过程是:

  1. 在sysindexes表查询INDID值为2,说明有非聚集索引;
  2. 从根出发,在cityid的非聚集索引的非叶级节点中定位最接近0101的条目;
  3. 从上面条目下的叶级页面中查到0101的逻辑位置,是聚集索引的指针;
  4. 根据指针所指示位置,进入位于username的聚集索引中的叶级页面中找到0101数据记录;
  5. 将该记录返回客户端。

通过上面数据库访问索引的原理,我们就很容易解释聚集索引与非聚集索引的区别了,原理都一样,关键看什么场合应用什么索引了,下一篇我来总结一些不同场合最适合采用什么样的索引,不对之外多多指点。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1658
14
PHP Tutorial
1257
29
C# Tutorial
1231
24
What is the difference between HQL and SQL in Hibernate framework? What is the difference between HQL and SQL in Hibernate framework? Apr 17, 2024 pm 02:57 PM

HQL and SQL are compared in the Hibernate framework: HQL (1. Object-oriented syntax, 2. Database-independent queries, 3. Type safety), while SQL directly operates the database (1. Database-independent standards, 2. Complex executable queries and data manipulation).

Comparison and differences of SQL syntax between Oracle and DB2 Comparison and differences of SQL syntax between Oracle and DB2 Mar 11, 2024 pm 12:09 PM

Oracle and DB2 are two commonly used relational database management systems, each of which has its own unique SQL syntax and characteristics. This article will compare and differ between the SQL syntax of Oracle and DB2, and provide specific code examples. Database connection In Oracle, use the following statement to connect to the database: CONNECTusername/password@database. In DB2, the statement to connect to the database is as follows: CONNECTTOdataba

Usage of division operation in Oracle SQL Usage of division operation in Oracle SQL Mar 10, 2024 pm 03:06 PM

"Usage of Division Operation in OracleSQL" In OracleSQL, division operation is one of the common mathematical operations. During data query and processing, division operations can help us calculate the ratio between fields or derive the logical relationship between specific values. This article will introduce the usage of division operation in OracleSQL and provide specific code examples. 1. Two ways of division operations in OracleSQL In OracleSQL, division operations can be performed in two different ways.

Detailed explanation of the Set tag function in MyBatis dynamic SQL tags Detailed explanation of the Set tag function in MyBatis dynamic SQL tags Feb 26, 2024 pm 07:48 PM

Interpretation of MyBatis dynamic SQL tags: Detailed explanation of Set tag usage MyBatis is an excellent persistence layer framework. It provides a wealth of dynamic SQL tags and can flexibly construct database operation statements. Among them, the Set tag is used to generate the SET clause in the UPDATE statement, which is very commonly used in update operations. This article will explain in detail the usage of the Set tag in MyBatis and demonstrate its functionality through specific code examples. What is Set tag Set tag is used in MyBati

Analysis of the function and principle of nohup Analysis of the function and principle of nohup Mar 25, 2024 pm 03:24 PM

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

How can I make money by publishing articles on Toutiao today? How to earn more income by publishing articles on Toutiao today! How can I make money by publishing articles on Toutiao today? How to earn more income by publishing articles on Toutiao today! Mar 15, 2024 pm 04:13 PM

1. How can you make money by publishing articles on Toutiao today? How to earn more income by publishing articles on Toutiao today! 1. Activate basic rights and interests: original articles can earn profits by advertising, and videos must be original in horizontal screen mode to earn profits. 2. Activate the rights of 100 fans: if the number of fans reaches 100 fans or above, you can get profits from micro headlines, original Q&A creation and Q&A. 3. Insist on original works: Original works include articles, micro headlines, questions, etc., and are required to be more than 300 words. Please note that if illegally plagiarized works are published as original works, credit points will be deducted, and even any profits will be deducted. 4. Verticality: When writing articles in professional fields, you cannot write articles across fields at will. You will not get appropriate recommendations, you will not be able to achieve the professionalism and refinement of your work, and it will be difficult to attract fans and readers. 5. Activity: high activity,

How to solve the 5120 error in SQL How to solve the 5120 error in SQL Mar 06, 2024 pm 04:33 PM

Solution: 1. Check whether the logged-in user has sufficient permissions to access or operate the database, and ensure that the user has the correct permissions; 2. Check whether the account of the SQL Server service has permission to access the specified file or folder, and ensure that the account Have sufficient permissions to read and write the file or folder; 3. Check whether the specified database file has been opened or locked by other processes, try to close or release the file, and rerun the query; 4. Try as administrator Run Management Studio as etc.

Database technology competition: What are the differences between Oracle and SQL? Database technology competition: What are the differences between Oracle and SQL? Mar 09, 2024 am 08:30 AM

Database technology competition: What are the differences between Oracle and SQL? In the database field, Oracle and SQL Server are two highly respected relational database management systems. Although they both belong to the category of relational databases, there are many differences between them. In this article, we will delve into the differences between Oracle and SQL Server, as well as their features and advantages in practical applications. First of all, there are differences in syntax between Oracle and SQL Server.

See all articles