MySQL index and query optimization summary

The article "MySQL Query Analysis" describes the method of using MySQL slow query and explain command to locate the MySQL performance bottleneck. After locating the SQL statement with the performance bottleneck, it is necessary to optimize the inefficient SQL statement. This article mainly discusses the principle of MySQL index and the commonly used sql query optimization.

A simple comparison test In

the previous case, the c2c_zwdb.t_file_count table has only one self-incrementing id, and the SQL execution of the unindexed FFileName field is as follows:



In the above figure, type=all, key=null, rows=33777. The sql does not use indexes and is a very inefficient full table scan. If combined query and some other constraints are added, the database will consume memory wildly, and it will affect the execution of front-end programs.

At this time, add an index to the FFileName field:

alter table c2c_zwdb.t_file_count add index index_title(FFileName);

execute the above query statement again, the comparison is obvious:



in this figure, type=ref, key=index name (index_title), rows =1. The sql uses the index index_title, and is a constant scan, only one row is scanned according to the index.

Compared with the unindexed case, after adding the index, the query efficiency comparison is very obvious.

MySQL index

Through the above comparison test, we can see that index is the key to fast search. The establishment of MySQL indexes is very important for the efficient operation of MySQL. For a small amount of data, the impact of not having a suitable index is not very large, but when the amount of data increases, the performance will drop sharply. If multiple columns are indexed (combined index), the order of the columns is very important, and MySQL can only perform efficient searches on the leftmost prefix of the index.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326274320&siteId=291194637