上篇文章粗略的总结了些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'时,查询过程是:
- 在sysindexes表查询INDID值为2,说明有非聚集索引;
- 从根出发,在非叶级节点中定位最接近0101的值(枝节点),查到其位于叶级页面的第n页;
- 在叶级页面的第n页下搜寻0101的RID,其RID显示为N∶i∶j,表示cityid字段中名为0101的记录位于堆的第i页的第j行,N代表文件的ID值。
- 在堆的第 i页第j行将该记录返回给客户端。
第三:访问创建有聚集索引的表。
聚集索引中,数据所在的数据页是叶级,索引数据所在的索引页是非叶级。原理和上述非聚集索引的查询差不多,由于记录是按聚集索引键值进行排序,即聚集索引的索引键值也就是具体的数据页。这种情况比起非聚集索引要简单很多,因为比非聚集索引少了一层节点查询。
上篇文章的username字段上建立了聚集索引,此时执行Select* From student Where username='1'时,查询过程是:
- 在sysindexes表查询INDID值为1,说明表中建立了聚集索;
- 从根出发,在非叶级节点中定位最接近1的值(枝节点),再查到其位于叶级页面的第n页;
- 在叶级页面第n页下搜寻值为1的条目,而这一条目就是数据记录本身;
- 将该记录返回客户端。
下图可做参考:
第四:怎样访问既有聚集索引、又有非聚集索引的数据表:
username字段上建立了聚集索引,cityid上建立了非聚集索引,当执行Select * From student Where cityid='0101'时,查询过程是:
- 在sysindexes表查询INDID值为2,说明有非聚集索引;
- 从根出发,在cityid的非聚集索引的非叶级节点中定位最接近0101的条目;
- 从上面条目下的叶级页面中查到0101的逻辑位置,是聚集索引的指针;
- 根据指针所指示位置,进入位于username的聚集索引中的叶级页面中找到0101数据记录;
- 将该记录返回客户端。
通过上面数据库访问索引的原理,我们就很容易解释聚集索引与非聚集索引的区别了,原理都一样,关键看什么场合应用什么索引了,下一篇我来总结一些不同场合最适合采用什么样的索引,不对之外多多指点。

Stored procedures are precompiled SQL statements in MySQL for improving performance and simplifying complex operations. 1. Improve performance: After the first compilation, subsequent calls do not need to be recompiled. 2. Improve security: Restrict data table access through permission control. 3. Simplify complex operations: combine multiple SQL statements to simplify application layer logic.

The working principle of MySQL query cache is to store the results of SELECT query, and when the same query is executed again, the cached results are directly returned. 1) Query cache improves database reading performance and finds cached results through hash values. 2) Simple configuration, set query_cache_type and query_cache_size in MySQL configuration file. 3) Use the SQL_NO_CACHE keyword to disable the cache of specific queries. 4) In high-frequency update environments, query cache may cause performance bottlenecks and needs to be optimized for use through monitoring and adjustment of parameters.

The reasons why MySQL is widely used in various projects include: 1. High performance and scalability, supporting multiple storage engines; 2. Easy to use and maintain, simple configuration and rich tools; 3. Rich ecosystem, attracting a large number of community and third-party tool support; 4. Cross-platform support, suitable for multiple operating systems.

The steps for upgrading MySQL database include: 1. Backup the database, 2. Stop the current MySQL service, 3. Install the new version of MySQL, 4. Start the new version of MySQL service, 5. Recover the database. Compatibility issues are required during the upgrade process, and advanced tools such as PerconaToolkit can be used for testing and optimization.

MySQL backup policies include logical backup, physical backup, incremental backup, replication-based backup, and cloud backup. 1. Logical backup uses mysqldump to export database structure and data, which is suitable for small databases and version migrations. 2. Physical backups are fast and comprehensive by copying data files, but require database consistency. 3. Incremental backup uses binary logging to record changes, which is suitable for large databases. 4. Replication-based backup reduces the impact on the production system by backing up from the server. 5. Cloud backups such as AmazonRDS provide automation solutions, but costs and control need to be considered. When selecting a policy, database size, downtime tolerance, recovery time, and recovery point goals should be considered.

MySQLclusteringenhancesdatabaserobustnessandscalabilitybydistributingdataacrossmultiplenodes.ItusestheNDBenginefordatareplicationandfaulttolerance,ensuringhighavailability.Setupinvolvesconfiguringmanagement,data,andSQLnodes,withcarefulmonitoringandpe

Optimizing database schema design in MySQL can improve performance through the following steps: 1. Index optimization: Create indexes on common query columns, balancing the overhead of query and inserting updates. 2. Table structure optimization: Reduce data redundancy through normalization or anti-normalization and improve access efficiency. 3. Data type selection: Use appropriate data types, such as INT instead of VARCHAR, to reduce storage space. 4. Partitioning and sub-table: For large data volumes, use partitioning and sub-table to disperse data to improve query and maintenance efficiency.

TooptimizeMySQLperformance,followthesesteps:1)Implementproperindexingtospeedupqueries,2)UseEXPLAINtoanalyzeandoptimizequeryperformance,3)Adjustserverconfigurationsettingslikeinnodb_buffer_pool_sizeandmax_connections,4)Usepartitioningforlargetablestoi


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

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6
Visual web development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
