What is a MySQL index (Index)? Why use an index?

An index is a special database structure composed of one or more columns in a data table, which can be used to quickly query records with a certain value in the data table. The meaning, function, advantages and disadvantages of the index are explained in detail below.

Through the index, you don't need to read all the information of the record when querying the data, but only query the index column. Otherwise, the database system will read all the information from each record to match.

The index can be compared to the phonetic table of the Xinhua dictionary. For example, if you want to look up the word "ku", if you don't use the phonetic sequence, you need to search page by page from the 400 pages of the dictionary. However, if the pinyin is extracted to form a sequence table, you only need to search directly from the sequence table on more than 10 pages. This saves a lot of time.

Therefore, the use of indexes can greatly improve the query speed of the database, and effectively improve the performance of the database system.

Why use an index

An index is a table of correspondence between column values ​​and record rows established in a certain order based on one or several columns in the table. In essence, it is a one-to-one correspondence between the column values ​​describing the index column and the record rows in the original table. ordered list of .

Index is a very important database object in MySQL, it is the basis of database performance tuning technology, and it is often used to achieve fast data retrieval.

In MySQL, there are usually two ways to access the row data of the database table:

1) Sequential access

Sequential access is to perform a full table scan in the table, traversing row by row from the beginning to the end until the target data that meets the conditions is found in the unordered row data.

The implementation of sequential access is relatively simple, but when there is a large amount of data in the table, the efficiency is very low. For example, when searching for a small amount of data among tens of millions of data, using sequential access will traverse all the data, which will take a lot of time and obviously affect the processing performance of the database.

2) Index access

Index access is the method of directly accessing the rows in the table by traversing the index.

The premise of using this method is to create an index on the table. After creating the index on the column, when searching for data, you can directly find the position of the corresponding record row according to the index on the column, so as to quickly find the data. The index stores pointers to the data values ​​of the specified column, and sorts these pointers according to the specified sort order.

For example, in the student basic information table tb_students, if an index is established based on student_id, the system creates a mapping table from the index column to the actual record. When the user needs to search for the data whose student_id is 12022, the system first finds the record on the student_id index, then directly finds the data row through the mapping table, and returns the data of the row. Because the speed of scanning the index is generally much faster than the speed of scanning the actual data row, so the way of using the index can greatly improve the working efficiency of the database.

In short, without an index, MySQL would have to read the entire table starting with the first record until it finds the relevant row. The larger the table, the more time it takes to query the data. If the queried column in the table has an index, MySQL can quickly get to a location to search the data file without having to look at all the data, which will save a lot of time.

Advantages and disadvantages of indexing

Indexes have their obvious advantages, as well as their inevitable disadvantages.

advantage

The advantages of indexes are as follows:

  • The uniqueness of each row of data in the database table can be guaranteed by creating a unique index.
  • Indexes can be set on all MySQL column types.
  • It can greatly speed up the query speed of data, which is the main reason for using indexes.
  • The connection between tables can be accelerated in realizing the referential integrity of data.
  • It can also significantly reduce the time of grouping and sorting in the query when using the grouping and sorting clauses for data query

shortcoming

Adding indexes also has many disadvantages, mainly as follows:

  • Creating and maintaining index groups is time-consuming and increases with the amount of data.
  • Indexes need to occupy disk space. In addition to data tables occupying data space, each index also occupies a certain amount of physical space. If there are a large number of indexes, index files may reach their maximum file size sooner than data files.
  • When the data in the table is added, deleted, and modified, the index must also be dynamically maintained, which reduces the speed of data maintenance.

When using indexes, you need to consider the advantages and disadvantages of indexes comprehensively.

Indexes can improve query speed, but will affect the speed of inserting records. Because, when inserting records into a table with an index, the database system will sort according to the index, which reduces the speed of inserting records, and the speed impact when inserting a large number of records will be more obvious. In this case, the best way is to delete the index in the table first, then insert the data, and then create the index after the insertion is complete.

Dark horse programmer MySQL database entry to proficiency, from mysql installation to mysql advanced, mysql optimization all covered

Guess you like

Origin blog.csdn.net/Itmastergo/article/details/130483636