Detailed explanation of MySQL combined index method

Detailed explanation of MySQL combined index method

For any DBMS, indexes are the most important factor for optimization. For a small amount of data, not having a suitable index does not have a big impact, but as the amount of data increases, performance will drop sharply.

If you index multiple columns (composite index), the order of the columns is very important. MySQL can only effectively search for a prefix of the leftmost column of the index. For example:

Assume that there is a composite index (c1, c2), the query select * from t1 where c1=1 and c2=2 can use this index. The query select * from t1 where c1=1 can also use this index. However, the query statement select * from t1 where c2=2 cannot use this index because there is no leading column for the composite index. That is, in order to use the c2 column for search, c1 must be equal to a certain value.

For example:
Create two tables book (book table) and bookclass (book classification table)

select b.ISBN FROM book b where b.CATEGORY_ID = 1; 

這里寫圖片描述

Execution time: 0.053s

Use explain to analyze the SQL:

這里寫圖片描述

type = ALL Extra=Using where, the full table query does not use the index.

EXPLAIN shows how MySQL uses indexes to process select statements and join tables. It can help choose better indexes and write more optimized query statements.

ALL For each combination of rows from the previous tables, a full table scan is performed. This is usually bad if the table is the first table not marked const, and is usually very bad in other cases. Often you can add more indexes instead of using ALL so that rows can be retrieved based on constant values ​​or column values ​​in the previous table.

Create a composite index:
create index index_isbn on book (CATEGORY_ID,ISBN);

Execute the SQL again and find that the time is shortened to 0.009s

這里寫圖片描述

Use explain to analyze the SQL:

這里寫圖片描述

type = ref, Extra = Using index Index query is used.

ref For each combination of rows from the previous tables, all rows with matching index values ​​will be read from this table. If the join uses only a leftmost prefix of the key, or if the key is not UNIQUE or PRIMARY KEY (in other words, if the join cannot select a single row based on the key), then ref is used. This join type is good if the keys used match only a small number of rows.

This is the end of this article about the detailed explanation of the combined index method of MySQL. For more relevant MySQL combined index content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • MySql index improves query speed common methods code examples
  • Summary of several situations in which MySQL indexes fail
  • Detailed explanation of MySQL clustered index and non-clustered index
  • Solutions to Mysql index performance optimization problems
  • Summary of some common writing methods that cause MySQL index failure
  • Various types of MySQL indexes
  • MySQL performance optimization: how to use indexes efficiently and correctly
  • Basic syntax of MySQL index
  • Reasons and solutions for MySQL selecting the wrong index

<<:  Linux uses suid vim.basic file to achieve privilege escalation

>>:  Vue uses OSS to upload pictures or attachments

Recommend

Several methods of implementing two fixed columns and one adaptive column in CSS

This article introduces several methods of implem...

Pure CSS to achieve cool neon light effect (with demo)

I have recently been following the CSS Animation ...

XHTML three document type declarations

XHTML defines three document type declarations. T...

Chrome monitors cookie changes and assigns values

The following code introduces Chrome's monito...

VMware Workstation 12 Pro Linux installation tutorial

This article records the VMware Workstation 12 Pr...

Vite introduces the implementation of virtual files

Table of contents background Importing virtual fi...

Extract specific file paths in folders based on Linux commands

Recently, there is a need to automatically search...

Detailed explanation of the use of this.$set in Vue

Table of contents Use of this.$set in Vue use Why...

Analysis of the differences between Iframe and FRAME

1. Use of Iframe tag <br />When it comes to ...

How to clear mysql registry

Specific method: 1. Press [ win+r ] to open the r...

How to configure Linux firewall and open ports 80 and 3306

Port 80 is also configured. First enter the firew...

Example code for css3 to achieve scroll bar beautification effect

The specific code is as follows: /*Scroll bar wid...

Complete steps for deploying confluence with docker

Confluence is paid, but it can be cracked for use...

CSS code to distinguish ie8/ie9/ie10/ie11 chrome firefox

Website compatibility debugging is really annoyin...