Analysis of the reasons why the index does not take effect when searching in the MySql range

Analysis of the reasons why the index does not take effect when searching in the MySql range

1 Problem Description

This article sorts the established composite index and takes the non-index fields in the record. It is found that the index is not effective. For example, there is the following table, and the DDL statement is:

CREATE TABLE `employees` (
 `emp_no` int(11) NOT NULL,
 `birth_date` date NOT NULL,
 `first_name` varchar(14) NOT NULL,
 `last_name` varchar(16) NOT NULL,
 `gender` enum('M','F') NOT NULL,
 `hire_date` date NOT NULL,
 `age` int(11) NOT NULL,
 PRIMARY KEY (`emp_no`),
 KEY `unique_birth_name` (`first_name`,`last_name`) USING BTREE
 )ENGINE=InnoDB DEFAULT CHARSET=utf8;

The composite index is unique_birth_name (first_name,last_name) . Use the following statement:

EXPLAIN SELECT
 gender
FROM
 employees
ORDER BY
 first_name,
 last_name 

這里寫圖片描述

According to the above figure: type:all and Extra:Using filesort, the index is not effective.

Continue the experiment and further rewrite the query statement to add a range search:

EXPLAIN SELECT
 gender
FROM
 employees
WHERE first_name > 'Leah'
ORDER BY
 first_name,
 last_name

The execution plan is shown in the following figure:

這里寫圖片描述

The results here are no different from the first SQL analysis. Keep experimenting.

Rewrite the sql statement:

EXPLAIN SELECT
 gender
FROM
 employees
WHERE first_name > 'Tzvetan'
ORDER BY
 first_name,
 last_name 

這里寫圖片描述

At this point, surprisingly, the index works.

2 Problem Analysis

At this point, we make a bold guess:

When performing SQL analysis for the first time, because after the first order by, the data of the entire table is still obtained. If each gender is searched and spliced ​​according to the primary key carried in the composite index, it will naturally be very resource-consuming and time-consuming. MySQL will not do such a stupid thing. It is better to directly scan the entire table, and concatenate each scanned data with the temporary data obtained by order by to obtain the required data.

In order to verify the correctness of the above idea, we analyze three SQL statements.

The amount of data obtained by the first SQL based on the composite index is: 300024 , which is the data of the entire table

SELECT
 COUNT(first_name)
FROM
 employees
ORDER BY
 first_name,
 last_name 

這里寫圖片描述

The amount of data obtained by the second rewritten SQL based on the composite index is: 159149 , which is 1/2 of the total table data volume.

SELECT
 COUNT(first_name)
FROM
 employees
WHERE first_name > 'Leah'
ORDER BY
 first_name,
 last_name 

這里寫圖片描述

The amount of data obtained by the third rewritten SQL based on the composite index is: 36731 , which is 1/10 of the total table data volume.

SELECT
  COUNT(first_name)
FROM
  employees
WHERE first_name > 'Tzvetan'
ORDER BY
  first_name,
  last_name 

這里寫圖片描述

By comparison, it was found that the amount of data obtained by the second rewritten SQL based on the composite index was 1/2 of the total table data amount. At this point, MySQL has not yet reached the level of using indexes for secondary searches. The amount of data obtained by the third rewritten SQL based on the composite index is 1/10 of the total table data volume, reaching the level of MySQL using the index for secondary search. Therefore, it can be seen from the execution plan that the third rewritten SQL used the index.

3 Conclusion

Whether MySQL performs a secondary search based on the primary key queried from the first index condition also depends on the amount of data queried. If the amount of data is close to the amount of data in the entire table, a full table scan will be performed. Otherwise, a secondary search will be performed based on the primary key queried from the first time.

This concludes this article on the analysis of the causes of the problem of index not taking effect during MySql range search. For more relevant content on the problem of index not taking effect during MySql range search, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • What are the benefits of using B+ tree as index structure in MySQL?
  • Why does MySQL database index choose to use B+ tree?
  • The principles and defects of MySQL full-text indexing
  • Mysql 5.6 "implicit conversion" causes index failure and inaccurate data
  • Detailed analysis of several situations in which MySQL indexes fail
  • Descending Index in MySQL 8.0
  • Index Skip Scan in MySQL 8.0
  • MySQL performance optimization index optimization
  • What are the advantages of using B+ tree index in MySQL?

<<:  Detailed explanation of the front-end framework for low-threshold development of iOS, Android, and mini-program applications

>>:  About the location of the H1 tag in XHTML

Recommend

Comprehensive summary of Vue3.0's various listening methods

Table of contents Listener 1.watchEffect 2.watch ...

Detailed analysis of matching rules when Nginx processes requests

When nginx receives a request, it will first matc...

Summary of MySQL database usage specifications

Introduction: Regarding MySQL database specificat...

Docker implements re-tagging and deleting the image of the original tag

The docker image id is unique and can physically ...

Share 8 CSS tools to improve web design

When one needs to edit or modify the website desi...

How to install kibana tokenizer inside docker container

step: 1. Create a new docker-compose.yml file in ...

Example of compiling LNMP in Docker container

Table of contents 1. Project Description 2. Nginx...

CSS uses calc() to obtain the current visible screen height

First, let's take a look at the relative leng...

Three ways to forward linux ssh port

ssh is one of the two command line tools I use mo...

How to configure the Runner container in Docker

1. Create a runner container mk@mk-pc:~/Desktop$ ...

JS implements multiple tab switching carousel

Carousel animation can improve the appearance and...

How to write DROP TABLE in different databases

How to write DROP TABLE in different databases 1....

How to implement load balancing in MySQL

Preface MySQL is a high-speed, high-performance, ...