[MySQL Index] Improve query speed and efficiency

1. Know the index

Suppose now that you want to find the content of the index in the MySQL book, you should not take the MySQL book one by one to find it, but you will read the catalog of the MySQL book, and then find the page number corresponding to the index through the catalog, and then go to the corresponding View the content of the index in the page number of

Advantages of indexes:

  • The index is equivalent to the catalog of the book, using the index can speed up the search speed

Disadvantages of indexes:

  • Although the index can speed up the search, the index also increases the cost of addition, deletion, and modification, because the created index needs to be adjusted when adding, deleting, and modifying

  • The index also increases the overhead of space, and the construction of the index requires additional hard disk space to save

index means index , whether it is to view the index or create an index,..., only the operations about the index generally have the word index

2. Index operation

2.1 View index

Generally, when creating primary key constraints, unique constraints, and foreign key constraints, indexes for corresponding columns are automatically created

Next, we will create a student table to view its index

student table:

create table student(id int primary key,name varchar(20));

The id in the student table created above is the primary key, so the id will automatically generate an index. When we query the index on the student table, we can find the id index

View index: show index from table name;

2.2 Create an index

When creating an index on a column or columns in a data table, the following points need to be considered:

  • The amount of data is large, and conditional queries are often performed on these columns

  • The insertion, modification, and deletion operations of the data table are less frequent for these columns

  • The index will take up additional disk space, consider whether the disk space is sufficient

Ordinary indexes can be created for fields corresponding to non-primary keys, non-unique constraints, and non-foreign keys

Create a student table:

//如果有student表,请先删除在创建
create table student(id int primary key,name varchar(20));

Create an index: create index index name no table name (field name);

create index index_name on student(name);

Note: To create an index, it is best to create the index at the beginning of the table creation. Otherwise, if it is for a table that already has many records in a table, creating an index is a dangerous operation, and it will eat a lot of data at this time. IO, it takes a long time (it may be tens of minutes or several hours, mainly depending on the amount of data), then during this time, the database cannot be used normally

2.3 Delete index

Next, we will delete the index added in 1.3

Delete index: drop index index name on table name;

drop index index_name on student;

Note: Deleting the index may also eat a lot of disk IO

3. Index data structure in MySQL

1. Is the data structure of the index in MySQL a hash table?

Answer: The time complexity of finding elements in the hash table is O(1), but the hash table is not suitable for database indexing, because the hash table can only be compared for equality, and range queries cannot be performed.

2. Is the data structure of the index in MySQL a binary search tree?

Answer: The time complexity of finding elements in a binary search tree is O(n). Binary means that when the number of elements is large, the height of the tree will be relatively high. The height of the tree also determines the number of comparisons of elements when querying

3. Is the data structure of the index in MySQL an N-ary search tree?

Answer: There are multiple values ​​on each node of the N-ary search tree, and there are multiple forks at the same time, so the height of the tree is reduced. One of the typical implementations is called B-tree. Although the number of comparisons has not been reduced, one node may need to compare multiple times, but the number of reads and writes to the hard disk has been reduced, and each node is on the hard disk.

B-tree:

B-tree is a typical implementation of N-ary tree. Its specificity is that there are multiple values ​​on each node, and the values ​​in child nodes are all smaller than the values ​​in parent nodes.

B-tree is already more suitable for database index than binary search tree, but it is not enough. For this, B+ tree is introduced, which is a further improvement of B tree. The B+ tree is a tailor-made data structure for indexing this scene.

Specific to B+ tree:

  • The B+ tree is also an N-fork search tree. Each node may contain N keys, and N keys divide N intervals. The last key is equivalent to the maximum value.

  • The key of the parent element will appear repeatedly in the child element, and it will appear as the maximum value. This repeated appearance will allow the leaf nodes to contain the complete set of all data, and all values ​​​​in non-leaf nodes will be reflected in the leaf nodes. come out

  • The leaf nodes will be connected end to end in a way similar to a linked list

B+ tree:

Advantages of B+ tree:

  • As an N-fork search tree, the height is reduced, and the number of hard disk IOs is relatively small when compared (same as B-tree)

  • Better for range queries

  • All queries fall on the leaf nodes, no matter which element is queried, the number of comparisons in the middle is almost the same. For B-trees, the speed of each query may be different, but for B+ trees, the speed of each query is the same

  • Since all keys will be reflected in the leaf nodes, you only need to put all the data rows in the leaf nodes. Non-leaf nodes only need to save a simple id instead of a whole row, which means that the space occupied by non-leaf nodes is greatly reduced

有的表不仅有主键索引,还有别的非主键列也可能有索引,此时如何构造B+树?

答:构造一个主键列的B+树,然后再构造一个非主键列的 B+ 树。非主键列有索引的 B+树非叶子节点里面存的都是一些key(比如:一些学生姓名),到了叶子节点这一层,存的并不是完整的数据行,而是存的主键值。如果使用主键列进行查询,只需要查询一次主键列 B+树即可。如果采用非主键列来查询,则需要先查一遍非主键列的B+树,然后再查一遍主键列的B+树

当前B+树这个数据结构,只是针对 MySQL 的 innoDB 这个数据库引擎里面所典型使用的数据结构。不同的数据库,不同的引擎,里面存储数据的结构可能也不同

Guess you like

Origin blog.csdn.net/m0_66488562/article/details/129475242