数据库索引的工作原理及其种类

数据库索引,是数据库管理系统中一个排序的数据结构,以协助快速查询、更新数据库表中数据。索引的实现通常使用B树及其变种B+树。

在数据之外,数据库系统还维护着满足特定查找算法的数据结构,这些数据结构以某种方式引用(指向)数据,这样就可以在这些数据结构上实现高级查找算法。这种数据结构,就是索引。

为表设置索引要付出代价的:一是增加了数据库的存储空间,二是在插入和修改数据时要花费较多的时间(因为索引也要随之变动)。

图展示了一种可能的索引方式。左边是数据表,一共有两列七条记录,最左边的是数据记录的物理地址(注意逻辑上相邻的记录在磁盘上也并不是一定物理相邻的)。为了加快Col2的查找,可以维护一个右边所示的二叉查找树,每个节点分别包含索引键值和一个指向对应数据记录物理地址的指针,这样就可以运用二叉查找在O(log2n)的复杂度内获取到相应数据。

创建索引可以大大提高系统的性能。

第一,通过创建唯一性索引,可以保证数据库表中每一行数据的唯一性。

第二,可以大大加快数据的检索速度,这也是创建索引的最主要的原因。

第三,可以加速表和表之间的连接,特别是在实现数据的参考完整性方面特别有意义。

第四,在使用分组和排序子句进行数据检索时,同样可以显著减少查询中分组和排序的时间。

第五,通过使用索引,可以在查询的过程中,使用优化隐藏器,提高系统的性能。

也许会有人要问:增加索引有如此多的优点,为什么不对表中的每一个列创建一个索引呢?因为,增加索引也有许多不利的方面。

第一,创建索引和维护索引要耗费时间,这种时间随着数据量的增加而增加。

第二,索引需要占物理空间,除了数据表占数据空间之外,每一个索引还要占一定的物理空间,如果要建立聚簇索引,那么需要的空间就会更大。

第三,当对表中的数据进行增加、删除和修改的时候,索引也要动态的维护,这样就降低了数据的维护速度。

索引是建立在数据库表中的某些列的上面。在创建索引的时候,应该考虑在哪些列上可以创建索引,在哪些列上不能创建索引。一般来说,应该在这些列上创建索引:在经常需要搜索的列上,可以加快搜索的速度;在作为主键的列上,强制该列的唯一性和组织表中数据的排列结构;在经常用在连接的列上,这些列主要是一些外键,可以加快连接的速度;在经常需要根据范围进行搜索的列上创建索引,因为索引已经排序,其指定的范围是连续的;在经常需要排序的列上创建索引,因为索引已经排序,这样查询可以利用索引的排序,加快排序查询时间;在经常使用在WHERE子句中的列上面创建索引,加快条件的判断速度。

同样,对于有些列不应该创建索引。一般来说,不应该创建索引的的这些列具有下列特点:

第一,对于那些在查询中很少使用或者参考的列不应该创建索引。这是因为,既然这些列很少使用到,因此有索引或者无索引,并不能提高查询速度。相反,由于增加了索引,反而降低了系统的维护速度和增大了空间需求。

第二,对于那些只有很少数据值的列也不应该增加索引。这是因为,由于这些列的取值很少,例如人事表的性别列,在查询的结果中,结果集的数据行占了表中数据行的很大比例,即需要在表中搜索的数据行的比例很大。增加索引,并不能明显加快检索速度。

第三,对于那些定义为text, image和bit数据类型的列不应该增加索引。这是因为,这些列的数据量要么相当大,要么取值很少。

第四,当修改性能远远大于检索性能时,不应该创建索引。这是因为,修改性能和检索性能是互相矛盾的。当增加索引时,会提高检索性能,但是会降低修改性能。当减少索引时,会提高修改性能,降低检索性能。因此,当修改性能远远大于检索性能时,不应该创建索引。

根据数据库的功能,可以在数据库设计器中创建三种索引:唯一索引、主键索引和聚集索引。

唯一索引

唯一索引是不允许其中任何两行具有相同索引值的索引。

当现有数据中存在重复的键值时,大多数数据库不允许将新创建的唯一索引与表一起保存。数据库还可能防止添加将在表中创建重复键值的新数据。例如,如果在employee表中职员的姓(lname)上创建了唯一索引,则任何两个员工都不能同姓。 主键索引 数据库表经常有一列或列组合,其值唯一标识表中的每一行。该列称为表的主键。 在数据库关系图中为表定义主键将自动创建主键索引,主键索引是唯一索引的特定类型。该索引要求主键中的每个值都唯一。当在查询中使用主键索引时,它还允许对数据的快速访问。 聚集索引 在聚集索引中,表中行的物理顺序与键值的逻辑(索引)顺序相同。一个表只能包含一个聚集索引。

如果某索引不是聚集索引,则表中行的物理顺序与键值的逻辑顺序不匹配。与非聚集索引相比,聚集索引通常提供更快的数据访问速度。

局部性原理与磁盘预读 由于存储介质的特性,磁盘本身存取就比主存慢很多,再加上机械运动耗费,磁盘的存取速度往往是主存的几百分分之一,因此为了提高效率,要尽量减少磁盘I/O。为了达到这个目的,磁盘往往不是严格按需读取,而是每次都会预读,即使只需要一个字节,磁盘也会从这个位置开始,顺序向后读取一定长度的数据放入内存。这样做的理论依据是计算机科学中著名的局部性原理:当一个数据被用到时,其附近的数据也通常会马上被使用。程序运行期间所需要的数据通常比较集中。

由于磁盘顺序读取的效率很高(不需要寻道时间,只需很少的旋转时间),因此对于具有局部性的程序来说,预读可以提高I/O效率。

预读的长度一般为页(page)的整倍数。页是计算机管理存储器的逻辑块,硬件及操作系统往往将主存和磁盘存储区分割为连续的大小相等的块,每个存储块称为一页(在许多操作系统中,页得大小通常为4k),主存和磁盘以页为单位交换数据。当程序要读取的数据不在主存中时,会触发一个缺页异常,此时系统会向磁盘发出读盘信号,磁盘会找到数据的起始位置并向后连续读取一页或几页载入内存中,然后异常返回,程序继续运行。

B-/+Tree索引的性能分析 到这里终于可以分析B-/+Tree索引的性能了。

上文说过一般使用磁盘I/O次数评价索引结构的优劣。先从B-Tree分析,根据B-Tree的定义,可知检索一次最多需要访问h个节点。数据库系统的设计者巧妙利用了磁盘预读原理,将一个节点的大小设为等于一个页,这样每个节点只需要一次I/O就可以完全载入。为了达到这个目的,在实际实现B-Tree还需要使用如下技巧:

每次新建节点时,直接申请一个页的空间,这样就保证一个节点物理上也存储在一个页里,加之计算机存储分配都是按页对齐的,就实现了一个node只需一次I/O。

B-Tree中一次检索最多需要h-1次I/O(根节点常驻内存),渐进复杂度为O(h)=O(logdN)。一般实际应用中,出度d是非常大的数字,通常超过100,因此h非常小(通常不超过3)。

而红黑树这种结构,h明显要深的多。由于逻辑上很近的节点(父子)物理上可能很远,无法利用局部性,所以红黑树的I/O渐进复杂度也为O(h),效率明显比B-Tree差很多。

综上所述,用B-Tree作为索引结构效率是非常高的。


A database index is a data structure that improves the speed of data retrieval operations on a database table at the cost of additional writes and storage space to maintain the index data structure. Indexes are used to quickly locate data without having to search every row in a database table every time a database table is accessed. Indexes can be created using one or more columns of a database table, providing the basis for both rapid random lookups and efficient access of ordered records.

What is Indexing?

Indexing is a data structure technique which allows you to quickly retrieve records from a database file. An Index is a small table having only two columns. The first column comprises a copy of the primary or candidate key of a table. Its second column contains a set of pointers for holding the address of the disk block where that specific key value stored.

An index -

  • Takes a search key as input
  • Efficiently returns a collection of matching records.

In this DBMS Indexing tutorial, you will learn:

  • Types of Indexing
  • Primary Index
  • Secondary Index
  • Clustering Index
  • What is Multilevel Index?
  • B-Tree Index
  • Advantages of Indexing
  • Disadvantages of Indexing

Types of Indexing

Type of Indexes in Database

Indexing in Database is defined based on its indexing attributes. Two main types of indexing methods are:

  • Primary Indexing
  • Secondary Indexing

Primary Index

Primary Index is an ordered file which is fixed length size with two fields. The first field is the same a primary key and second, filed is pointed to that specific data block. In the primary Index, there is always one to one relationship between the entries in the index table.

The primary Indexing in DBMS is also further divided into two types.

  • Dense Index
  • Sparse Index

Dense Index

In a dense index, a record is created for every search key valued in the database. This helps you to search faster but needs more space to store index records. In this Indexing, method records contain search key value and points to the real record on the disk.

Sparse Index

It is an index record that appears for only some of the values in the file. Sparse Index helps you to resolve the issues of dense Indexing in DBMS. In this method of indexing technique, a range of index columns stores the same data block address, and when data needs to be retrieved, the block address will be fetched.

However, sparse Index stores index records for only some search-key values. It needs less space, less maintenance overhead for insertion, and deletions but It is slower compared to the dense Index for locating records.

Below is an database index Example of Sparse Index

Secondary Index

The secondary Index in DBMS can be generated by a field which has a unique value for each record, and it should be a candidate key. It is also known as a non-clustering index.

This two-level database indexing technique is used to reduce the mapping size of the first level. For the first level, a large range of numbers is selected because of this; the mapping size always remains small.

Example of secondary Indexing

Let's understand secondary indexing with a database index example:

In a bank account database, data is stored sequentially by acc_no; you may want to find all accounts in of a specific branch of ABC bank.

Here, you can have a secondary index in DBMS for every search-key. Index record is a record point to a bucket that contains pointers to all the records with their specific search-key value.

Clustering Index

In a clustered index, records themselves are stored in the Index and not pointers. Sometimes the Index is created on non-primary key columns which might not be unique for each record. In such a situation, you can group two or more columns to get the unique values and create an index which is called clustered Index. This also helps you to identify the record faster.

Example:

Let's assume that a company recruited many employees in various departments. In this case, clustering indexing in DBMS should be created for all employees who belong to the same dept.

It is considered in a single cluster, and index points point to the cluster as a whole. Here, Department _no is a non-unique key.

What is Multilevel Index?

Multilevel Indexing in Database is created when a primary index does not fit in memory. In this type of indexing method, you can reduce the number of disk accesses to short any record and kept on a disk as a sequential file and create a sparse base on that file.

B-Tree Index

B-tree index is the widely used data structures for tree based indexing in DBMS. It is a multilevel format of tree based indexing in DBMS technique which has balanced binary search trees. All leaf nodes of the B tree signify actual data pointers.

Moreover, all leaf nodes are interlinked with a link list, which allows a B tree to support both random and sequential access.

  • Lead nodes must have between 2 and 4 values.
  • Every path from the root to leaf are mostly on an equal length.
  • Non-leaf nodes apart from the root node have between 3 and 5 children nodes.
  • Every node which is not a root or a leaf has between n/2] and n children.

Advantages of Indexing

Important pros/ advantage of Indexing are:

  • It helps you to reduce the total number of I/O operations needed to retrieve that data, so you don't need to access a row in the database from an index structure.
  • Offers Faster search and retrieval of data to users.
  • Indexing also helps you to reduce tablespace as you don't need to link to a row in a table, as there is no need to store the ROWID in the Index. Thus you will able to reduce the tablespace.
  • You can't sort data in the lead nodes as the value of the primary key classifies it.

Disadvantages of Indexing

Important drawbacks/cons of Indexing are:

  • To perform the indexing database management system, you need a primary key on the table with a unique value.
  • You can't perform any other indexes in Database on the Indexed data.
  • You are not allowed to partition an index-organized table.
  • SQL Indexing Decrease performance in INSERT, DELETE, and UPDATE query.

Summary:

  • Indexing is a small table which is consist of two columns.
  • Two main types of indexing methods are 1)Primary Indexing 2) Secondary Indexing.
  • Primary Index is an ordered file which is fixed length size with two fields.
  • The primary Indexing is also further divided into two types 1)Dense Index 2)Sparse Index.
  • In a dense index, a record is created for every search key valued in the database.
  • A sparse indexing method helps you to resolve the issues of dense Indexing.
  • The secondary Index in DBMS is an indexing method whose search key specifies an order different from the sequential order of the file.
  • Clustering index is defined as an order data file.
  • Multilevel Indexing is created when a primary index does not fit in memory.
  • The biggest benefit of Indexing is that it helps you to reduce the total number of I/O operations needed to retrieve that data.
  • The biggest drawback to performing the indexing database management system, you need a primary key on the table with a unique value.

参考:Indexing in DBMS: What is, Types of Indexes with EXAMPLES

参考:索引的工作原理及其种类

数据库索引的工作原理及其种类相关推荐

  1. 数据库索引如何工作? [关闭]

    鉴于索引在数据集大小增加时非常重要,有人可以解释索引在数据库无关的级别上的工作原理吗? 有关索引字段的查询的信息,请查看如何索引数据库列 . #1楼 为什么需要它? 当数据存储在基于磁盘的存储设备上时 ...

  2. 什么是m叉树_不懂数据库索引的底层原理?那是因为你心里没点b树

    点击上方"后端技术精选",选择"置顶公众号" 技术文章第一时间送达! 作者:苏苏喂 cnblogs.com/sujing/p/11110292.html 题外话 ...

  3. oracle树子类遍历父类_不懂数据库索引的底层原理?那是因为你心里没点b树

    前几天下班回到家后正在处理一个白天没解决的bug,厕所突然传来对象的声音: 对象:xx,你有<时间简史>吗? 我:我去!妹子,你这啥癖好啊,我有时间也不会去捡屎啊! 对象:...人家说的是 ...

  4. 不懂数据库索引的底层原理?那是因为你心里没点b树

    点击上方"后端技术精选",选择"置顶公众号" 技术文章第一时间送达! 作者:苏苏喂 cnblogs.com/sujing/p/11110292.html 题外话 ...

  5. 数据库索引如何工作?原理

    原文地址:https://blog.csdn.net/weiliangliang111/article/details/51333169 数据库索引到底是什么,是怎样工作的? 我们通过一个简单的例子来 ...

  6. 数据库索引的实现原理及查询优化

    MySQL官方对索引的定义为:索引(Index)是帮助MySQL高效获取数据的数据结构. 使用索引的目的在于提高查询效率,这篇文章梳理一下索引的实现原理和应用. 不同的存储引擎索引实现的数据结构不同 ...

  7. 一篇读懂聚集索引、非聚集索引、覆盖索引的工作原理!

    作者:PostTruth 「数据库」和「数据库索引」这两个东西是在服务器端开发领域应用最为广泛的两个概念,熟练使用数据库和数据库索引是开发人员在行业内生存的必备技能. 使用索引很简单,只要能写创建表的 ...

  8. 数据库索引实现底层原理2

    强烈建议参阅链接:http://www.linezing.com/blog/?p=798#nav-1 说白了,索引问题就是一个查找问题... 数据库索引,是数据库管理系统中一个排序的数据结构,以协助快 ...

  9. 数据库索引的实现原理

    强烈建议参阅链接:http://www.linezing.com/blog/?p=798#nav-1 说白了,索引问题就是一个查找问题... 数据库索引,是数据库管理系统中一个排序的数据结构,以协助快 ...

最新文章

  1. tinyxml 读取文本节点_TinyXml 读写XML文件
  2. 解决Virtualbox无法安装Windows 8.1
  3. C语言求二个数的最大公约数gcd和最小公倍数lcm(附完整源码)
  4. 程序员面试100题之九:求子数组的最大和
  5. 读书笔记之Unix命令
  6. Quaruts II 增量编译
  7. NanShan即时通讯 ie6 ie7 ie8 不支持json 终极解放方案
  8. dbcp连接池配置参数
  9. Java网络编程之服务端中的Socket
  10. 过去的七天我是这样过的
  11. 安装win10系统以及升级win10 home至enterprise版本
  12. 手游无限多开器安卓版_无限多开版!使用叶子猪手游模拟器玩大话西游手游教程...
  13. IDEA使用Maven构建Spring+SpringMVC+MyBatis整合项目demo成功执行但控制台Tomcat Locahost log输出No Spring WebApplicationIn
  14. win10需要修复计算机,超好用!Win10自带修复系统 隐藏太深
  15. 程序员必须 知道的英语单词
  16. tensorflow代码翻译成pytorch代码 -详细教程+案例
  17. PX4 FMU启动流程 1.nsh
  18. 串口DCB定义,配置例程
  19. 光模块COB工艺之耦合激光焊
  20. OpenCV绘制透明底的图片,简单易懂讲解alpha通道怎么用

热门文章

  1. python3网络编程
  2. cesium 渲染分析(以太阳为例),实现卫星任务规划中地球赤道平面绘制
  3. 自动驾驶中的9种传感器融合算法
  4. AutoShape:实时单目3D检测网络(ICCV2021)
  5. 300米远程深度估计:港科大重磅开源自动驾驶深度感知新技术,远超现有雷达|CVPR2020...
  6. C盘满了怎么办?如何清理
  7. moment 当前日期
  8. linux 后台运行jar SpringBoot
  9. Machine Learning | (10) 回归算法-岭回归
  10. RDKit | 删除方差低的描述符