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

The pitfalls encountered when learning Vue.js

Table of contents Class void pointing ES6 Arrow F...

Introduction to the use of alt and title attributes of HTML img tags

When browser vendors bend the standards and take i...

Nginx one domain name to access multiple projects method example

Background Recently, I encountered such a problem...

HTML markup language - table tag

Click here to return to the 123WORDPRESS.COM HTML ...

Detailed explanation of the wonderful uses of SUID, SGID and SBIT in Linux

Preface Linux's file permission management is...

Implementation of React virtual list

Table of contents 1. Background 2. What is a virt...

Nodejs module system source code analysis

Table of contents Overview CommonJS Specification...

Why is the MySQL auto-increment primary key not continuous?

Table of contents 1. Introduction 2. Self-increme...

Why should css be placed in the head tag

Think about it: Why should css be placed in the h...

Implementation and optimization of MySql subquery IN

Table of contents Why is IN slow? Which is faster...

Implementation of CSS3 button border animation

First look at the effect: html <a href="#...

Use of Docker UI, a Docker visualization management tool

1. Introduction to DockerUI DockerUI is based on ...

How to use CocosCreator to create a shooting game

Analyze the production steps: 1. Prepare resource...