Use of MySQL-optimized indexes

table of Contents

Index usage scenarios

Which columns are suitable for indexing

Joint index

Index optimization SQL method

Considerations when using indexes 


​​​​​​​

Index usage scenarios

 

1. The primary key of the table
2. Automatically establish a unique index
3. The unique constraint of the field of the table 4. The field of
direct conditional query (the field used for conditional constraint in SQL)
5. The field associated with other tables in the query
6. Sorted fields in the query (sorted fields will greatly improve sorting speed if accessed by index)
7. Statistics or grouped statistics fields in the query

8. There are too few records in the table (if there are only 5 records in a table and the index is used to access the records, then the index table needs to be accessed first, and then the data table is accessed through the index table. Generally, the index table and the data table are not in the same data block)
9 , Frequently inserted, deleted, modified tables (for some frequently processed business tables, the index should be minimized if the query allows)
10. Data duplication and evenly distributed table fields (if a table has 100,000 rows of records, there is a Field A has only two values ​​of T and F, and the probability of distribution of each value is about 50%, so indexing the field of this table A generally does not increase the query speed of the database.)
11, often query with the main field but Table fields with many main field index values
12. Matters for indexing tens of millions of MySQL databases and means to improve performance 

 

 

Which columns are suitable for indexing

 

1. In the where clause, group by clause, order by clause, and on clause, add indexes to the columns in the clause
2. The smaller the index field, the better (because the database data storage unit is "page", the more data is stored, IO will also be greater

3. Columns with large dispersion are placed in front of the joint index

select * from payment where staff_id =2 and customer_id =584;

Which column should be placed in front of the above SQL to establish a joint index?

Look at the number of different ids in these two fields separately. The larger the number, the greater the degree of dispersion: so it can be seen from the following figure: customer_id has a large degree of dispersion 

Conclusion: Since the dispersion of customer_id is large, it is good to use index (customer_id, staff_id)

 

 

Joint index

 

Naming convention

1. Fields that need to be indexed, in the where condition,
2. Fields with a small amount of data do not need to be indexed.
3. If the OR condition is in the where condition, the index does not work.
4. Meet the leftmost principle

 

 What is a joint index

1. An index on two or more columns is called a joint index, or a compound index.
2. With the additional columns in the index, you can narrow the search, but using an index with two columns is different from using two separate indexes. When creating a compound index, you should carefully consider the order of the columns. The composite index is very useful when you perform a search on all columns in the index or only on the first few columns; when you only perform a search on any subsequent columns, the composite index is useless.

 

 

Index optimization SQL method

 

1. Index maintenance and optimization (duplicate and redundant indexes)
Increasing the index will be beneficial to query efficiency, but it will reduce the efficiency of insert, update, delete, but in fact it is often not the case, too many indexes will not only affect the use The efficiency will also affect the query efficiency. This is because the database must first choose which index to use for query analysis. If there are too many indexes, the analysis process will be slower, which also reduces the query efficiency, so we want to Know how to increase, sometimes you need to know to maintain and delete unnecessary indexes

 

2. How to find duplicate and redundant indexes

  • Repeat index:

Repeated index refers to the same type of index created by the same column in the same order. The indexes on the primary key and ID columns in the following table are repeated indexes.

create table test(
id int not null primary key,
name varchar(10) not null,
title varchar(50) not null,
unique(id)
)engine=innodb;
  • Redundant index:

Redundant index refers to an index where the prefix columns of multiple indexes are the same or the primary key is included in the joint index. In the following example, key (name, id) is a redundant index

create table test(
id int not null primary key,
name varchar(10) not null,
title varchar(50) not null,
key(name,id)
)engine=innodb;

 

3. Use the pt-duplicate-key-checker tool to find duplicate indexes

pt-duplicate-key-checker -uroot -padmin -h 127.0.0.1

 

 

4. Index maintenance method
Due to business changes, some indexes need not be used afterwards, so they must be deleted. In mysql, you can use slow query log with pt-index-usage tool to analyze the index usage;

pt-index-usage -uroot -padmin /var/lib/mysql/mysql-host-slow.log

 

 

Considerations when using indexes 


1. Create an index
Many times the performance problem is very simple because we forgot to add the index, or that we did not add a more effective index. If no index is added, then a full table scan will be performed to find any even a specific piece of data. If the amount of data in a table is large and the qualified results are few, then no index will cause fatal performance degradation. . However, it is not necessary to build an index in any case. For example, gender may have only two values. Indexing not only has no advantage, but also affects the update speed. This is called excessive indexing. 

2. Compound index For
example, there is a statement like this: select * from users where area = 'beijing' and age = 22;
if we are creating a single index on area and age respectively, because mysql query can only use one at a time Index, so although this has relatively improved the efficiency of a full table scan without indexing, if you create a composite index on the area and age columns, it will bring higher efficiency. If we create a compound index of (area, age, salary), then it is actually equivalent to creating (area, age, salary), (area, age), (area) three indexes, which is called the best left prefix Characteristics
. Therefore, when creating a composite index, we should place the column that is most commonly used as a constraint on the left, and then decrease in order.


3. The index will not contain a column with a NULL value.
As long as the column contains a NULL value, it will not be included in the index. As long as a column in the composite index contains a NULL value, then this column is invalid for this composite index. So we don't let the default value of the field be NULL when designing the database.


4. Use short index
to index the string column, if possible, you should specify a prefix length. For example, if there is a CHAR (255) column, if multiple values ​​are unique within the first 10 or 20 characters, then do not index the entire column. Short indexes can not only improve query speed but also save disk space and I / O operations.


5, sorted index problem
mysql query only uses one index, so if the index has been used in the where clause, then the column in order by will not use the index. Therefore, do not use the sort operation when the default sorting of the database can meet the requirements; try not to include the sorting of multiple columns. It is best to create a composite index for these columns if necessary.


6. Like statement operation
Generally, the use of like operation is discouraged. If it is absolutely necessary, how to use it is also a problem. Like "% aaa%" will not use the index and like "aaa%" can use the index.


7. Do not perform operations on columns
select * from users where YEAR (adddate) ×


8. NOT IN operation
NOT IN operation will not use the index will be a full table scan. NOT IN can be replaced by NOT EXISTS

Published 568 original articles · Like 180 · Visits 180,000+

Guess you like

Origin blog.csdn.net/Delicious_Life/article/details/105610256