Application of B-Tree Index in sqlserver and mysql

When talking about database performance optimization, the "index" is usually mentioned, but many people actually don't really understand the index, and they haven't figured out why the index can speed up the retrieval speed, so that the index cannot be used well in practice.

In fact, the index can be said to be the cheapest and very effective optimization method. Generally speaking, a well-designed index can indeed have an immediate effect on query performance optimization.


I believe that many readers have known and used the index, and may have read or heard the popular descriptions of "Xinhua Dictionary" and "library", but they are still quite confused about the storage structure and nature of the index.

Readers with basic data structure and algorithm should have heard or practiced several common search algorithms such as "sequential search, binary search (half) search, and binary tree search". Among them, the sequential search efficiency is the lowest, and its algorithm complexity is O(n), while the binary search algorithm complexity is O(logn) but the data must be ordered, and it is usually widely used in linked lists. The complexity of binary tree search is only O(log2n), but the data structure is required to be a "tree".



Among the mainstream relational databases, the most widely used and supported index is the B-Tree index. Considering that most readers have limited knowledge of data structure, in order to facilitate understanding, readers can use B-Tree (or its variant B+Tree)

Understand as a common binary tree. Although this is not accurate, I believe that after reading it, readers have roughly understood why looking up data through an index is much faster than a normal table scan.


Clustered index in sqlserver


The leaf node (the bottom node) of the clustered index directly contains the data page.


Non-clustered index in sqlserver


In a table with a clustered index, the leaf node of a non-clustered index contains the key value of the clustered index (can be understood as a pointer to the clustered index).

In a heap table without a clustered index, the non-clustered index contains RID (can be understood as a pointer to the data row).


In mysql, there are usually "clustered index" (for InnoDB engine) and "non-clustered index" (for MyIsam engine), "primary key index" and "secondary index".

Index structure in mysql InnoDB engine


In the primary key index, the leaf node contains the data row (data page), and the leaf interface of the secondary index stores the key value of the primary key index (pointing to the primary key index)


Index structure in mysql MyIsam engine



There is not much difference between the primary key index and the secondary index structure. The data row information (such as row number, etc.) stored in the leaf nodes can directly point to and locate the data row


I believe readers can easily see that the structure, storage method and principle of B-Tree index in sqlserver and mysql are roughly the same. Of course, there are also many details and internal implementation differences.


Due to the author's level and limited understanding, all the text and descriptions in the article are written out of the author's memory. It is inevitable that mistakes will occur. Please enthusiastic readers to criticize and correct them in time.

Due to the limited time, most of the pictures are drawn rough by the author, and readers should also understand.


This article is from http://blog.csdn.net/dinglang_2009 , please indicate the source for reprinting.





Guess you like

Origin blog.csdn.net/dinglang_2009/article/details/41414529
Recommended