Can indexes definitely improve performance?

In the previous article, we introduced the concept and importance of database transactions, the ACID properties of transactions, and the control and isolation levels of concurrent transactions.

Today we discuss an important object related to performance: Index. You must have heard: Indexes can improve query performance. So, what is the principle of indexing? Will it be possible to query faster with an index? Is the index just to optimize query speed? Next, we will answer them one by one.

Principle of Index

The following is a simple query to find the employee whose job number is 5:

SELECT *
  FROM employee
 WHERE emp_id = 5;

How does the database find the data we need? If there is no index, you can only scan the entire employee table, and then use the job number to determine and return the data that meets the conditions. One of the biggest problems with this approach is that when the amount of data gradually increases, the performance of the full table scan also decreases significantly.

In order to solve the query performance problem, the database introduced a new data structure: index . The index is like the keyword index at the back of a book. It is sorted by keywords and provides page numbers that point to specific content. If we create an index (such as a B-tree index) on the email field, the database lookup process is roughly as shown in the figure below:

btree

The B-tree (Balanced Tree) index is like an upside-down tree, where the nodes are organized in order

Guess you like

Origin blog.csdn.net/horses/article/details/108729085