Index transactions in MySQL (1) Index----"Principle knowledge of database operation + interview questions~

This article mainly talks about MySQL index transactions

The so-called index index refers to: directory~~

The significance of the existence of the index: speed up the search (omitting the traversal process), but pay a certain price ~

The price paid is as follows:

1.需要付出额外的空间代价来保存索引数据

2.索引可能会拖慢新增,删除,修改数据的速度

However, in general, I still think that the advantages of indexing outweigh the disadvantages~~

In actual development, query scenarios are generally much more frequent than additions, deletions, and modifications

View index:

show index from 表名;

Create an index:

create index 索引名 on 表名(列名);

索引名称可任意取

For example: create a directory (index) based on the name of the content in the table ~

create index index_student_name on student(name);

The operation of creating an index may be very dangerous. If the data in the table is large, the cost of creating the index will be very high... In fact, the best way is to set the index at the beginning of the table creation. If the table There is already a lot of data in it, so don’t move the index~

Delete the index:

drop index 索引名 on 表名;

Deleting an index is similar to creating an index, and deleting an index is also risky~~

The index can speed up the query. In fact, the data structure (query) behind the index is a B+ tree (a data structure tailored for the database index) ~~ B+ tree we do not make special requirements, but the characteristics and advantages of the B+ tree Do you think we should know something? ?

B+ tree features:

  1. A node can store N keys, and N keys divide N intervals (instead of N+1 intervals)
  2. The value of the key in each node will exist in the child node (at the same time the key is the maximum value of the child node)
  3. The leaf nodes of the B+ tree are connected end to end, similar to a linked list
  4. Since the leaf node is a complete data collection, only the leaf node stores each row of data in the data table, instead of the leaf node, only the key value itself can be saved

All nodes of the entire tree are included in leaf nodes (keys in all non-leaf nodes will eventually appear in leaf nodes~~)
 

IO:input  output------>输入输出

显示数据到显示器上,从键盘输入数据

把数据写到硬盘上,从硬盘上读数据

把数据写到网卡里,从网卡读数据。

Advantages of B+ tree:

  1. The current node saves more keys, and the final height of the tree is relatively shorter, which reduces the number of IO accesses during query (same as B-tree) (IO refers to input and output)
  2. All queries will eventually fall on the leaf nodes (query any data, the number of IO accesses is the same)
  3. All the leaf nodes of the B+ tree form a linked list, which is convenient for range query at this time.
  4. Since the data is all on the leaf nodes, the non-leaf nodes only store the key, resulting in the non-leaf nodes occupying a relatively small space. These non-leaf nodes can be cached in memory (or a part of the cache), which further reduces the The number of IOs.

The way data is organized in MySQL:

When I saw a "table" back then, in fact, the table was not necessarily organized on the hard disk according to the data structure of the "table", but it might also be organized in the form of this tree (B+ tree) (specifically which The structure depends on whether there is an index in your table, and which index is used in the first-level database~)

In a table: If id is the primary key of the table, what if there are multiple indexes in the table? ? (Two indexes of id and name) The data of the table is still based on the id as the primary key, and a B+ tree is built, and all data rows are organized through the leaf nodes... Secondly, another B+ tree will be created for the name column, but this B+ tree The leaf node of the row no longer stores the complete data of this row, but stores the primary key id! ! ! At this time, if you query based on the name, you only get the primary key id if you find the leaf node, and you need to search once in the B+ tree of the primary key through the primary key id (a total of two B+ trees are searched) (the above process is called "back Table", this process is automatically completed by MySQL, and the user cannot perceive it~)

Guess you like

Origin blog.csdn.net/weixin_64308540/article/details/132645268