MySQL database - index (6) - index usage (covering index and table return query, prefix index, single column index and joint index), index design principles, index summary

Table of contents

Index usage (Part 2)

Covering index and table return query

Thinking questions

prefix index

grammar

Example

prefix length

Query process of prefix index

Single column index and joint index 

Index design principles

Index summary

1. Index Overview

2. Index structure 

3. Index classification 

4. Index syntax

5.SQL performance analysis 

6. Index usage 

7. Index design principles 


Index usage (Part 2)

Covering index and table return query

A covering index means that the query uses an index and all the columns that need to be returned can be found in the index.

Let’s first look at the execution process of the following set of SQL to gain a deeper understanding of covering index and table return queries.

Table structure and index diagram: 

id is the primary key and is a clustered index. A common index is established on the name field, which is a secondary index (auxiliary index).

Execute SQL:

select * from tb_user where id = 2;

According to the id query, we directly use the clustered index query. An index scan matches the id as 2. Because row data is placed under the leaf nodes in the clustered index, we can get the data we want and return the data directly, with high performance.

Look again,

Execute SQL:

selet id,name from tb_user where name = 'Arm';

Although the query is based on the name field and the secondary index is queried, since the fields returned by the query are id and name, in the secondary index of name, these two values ​​​​can be obtained directly. This achieves a covering index. Therefore, there is no need to query back the table, and the performance is high.

Last look,

Execute SQL:

selet id,name,gender from tb_user where name = 'Arm';

Since gender is not included in the secondary index of name, two index scans are required, that is, a table query is required, and the performance is relatively poor.

We can look at the execution plan of MySQL. All the indicators in the execution plan of some SQL statements are the same, and no difference can be seen. However, we want to pay attention to the Extra behind it:

Extra meaning
Using where; Using Index The search uses an index, but the required data can be found in the index columns, so there is no need to go back to the table to query the data.
Using index condition The search uses an index, but needs to return the table to query the data.

Therefore, in the execution plan, if Extra is 'Using index condition', then it may need to be modified and optimized.

Thinking questions

A table has four fields (id, username, password, status). Due to the large amount of data, the
following SQL statement needs to be optimized. How to proceed is the best solution:

select id,username,password from tb_user where username = 'itcast';


Answer:

Create a joint index for username and password,

The sql is: create index idx_user_name_pass on tb_user(username,password);
This can avoid the above SQL statement and table return query during the query process.

prefix index

When the field type is a string (varchar, text, longtext, etc.), sometimes a very long string needs to be indexed, which will make the
index very large and waste a lot of disk IO during query, affecting query efficiency.

At this time, you can only prefix part of the string to create an index, which can greatly save index space and improve indexing efficiency.

grammar

create index idx_xxxx on table_name( column(n) );

Example

Create a prefix index of length 5 for the email field of the tb_user table.

create index idx_email_5 on tb_user ( email(5) );

prefix length

It can be determined based on the selectivity of the index , which refers to the ratio of unique index values ​​(cardinality) to the total number of records in the data table . The higher the index selectivity, the higher the query efficiency. The selectivity of the unique index is 1. This is the best index selectivity and has the best performance.

select count(distinct email) / count(*) from tb_user ;

select count(distinct substring(email,1,5)) / count(*) from tb_user ;

Query process of prefix index

Single column index and joint index 

Single column index : An index contains only a single column.
Union index : An index contains multiple columns.

In the execution plan, if the two fields phone and name are single-column indexes, but in the end MySQL will only select one index, that is, only one field's index can be used. At this time, the table will be returned for query.

In a business scenario, if there are multiple query conditions, when considering creating an index on the query field, it is recommended to create a joint index
instead of a single-column index.

Index design principles

  1. Create indexes for tables with large amounts of data and frequent queries.
  2. Create indexes for fields that are often used as query conditions (where), sorting (order by), and grouping (group by) operations.
  3. Try to select columns with high discrimination as indexes and try to build unique indexes. The higher the discrimination, the more efficient the use of indexes.
  4. If it is a string type field and the length of the field is long, a prefix index can be established based on the characteristics of the field.
  5. Try to use joint indexes and reduce single-column indexes. When querying, joint indexes can often cover indexes, save storage space, avoid table backs, and improve query efficiency.
  6. To control the number of indexes, the more indexes the better. The more indexes, the greater the cost of maintaining the index structure, which will affect the efficiency of additions, deletions and modifications.
  7. If an indexed column cannot store NULL values, constrain it using NOT NULL when creating the table. When the optimizer knows whether each column contains NULL values, it can better determine which index to use most efficiently for the query.

Index summary

1. Index Overview

Indexes are data structures for efficient data retrieval.

2. Index structure 

B+Tree

Hash

3. Index classification 

Primary key index, unique index, regular index, full-text index, clustered index, secondary index.

4. Index syntax

create [unique] index xxx on xxx(xxx);

show index from xxxx;

drop index xxx on xxxx; 

5.SQL performance analysis 

Execution frequency, slow query log, profile, explain.

6. Index usage 

Union index, index invalidation, SQL prompt, covering index, prefix index, single column/joint index.

7. Index design principles 

Tables, fields, indexes.


END 


Learn from: Dark Horse Programmer - MySQL Database Course

Guess you like

Origin blog.csdn.net/li13437542099/article/details/133280459