A summary of the reasons why Mysql does not use date field index

A summary of the reasons why Mysql does not use date field index

background

In a table, the dataTime field is set to varchar type, the data stored is in date format, and an index is set for the field. But in the log record, there is a slow query about this table. The query statement is:
select * from digitaltwin_meteorological where dataTime > '2021-10-15';
EXPLAIN analyzes the SQL statement and finds that the SQL statement performs a full table scan. Why is the dataTime index column used in SQL and why is the full table scan performed?

explore

1: At first, it was thought that the dataTime field type was varchar, so MySQL sorted the index in string order instead of date size order, so when performing range queries, the index range partitioning could not be performed in date order. So I changed dataTime to datatime type and analyzed the statement, and found that it was still a full table scan.

2: Change the value of the query condition,

 select count(*) from digitaltwin_meteorological where dataTime > '2021-10-15';

The execution result is 3910.

EXPLAIN select * from digitaltwin_meteorological where dataTime > '2021-10-15';

The sql statement analysis result is a full table scan:

insert image description here

We change the query condition to 16 to see how many records there are:

 select count(*) from digitaltwin_meteorological where dataTime > '2021-10-16';

The query result is 2525. Let's analyze the query statement for number 16:

EXPLAIN select * from digitaltwin_meteorological where dataTime > '2021-10-16';

The execution result is a range query, which uses the index:

insert image description here

It can be seen from this that when the number of records queried is large, MySQL will perform a full table scan, believing that a full table scan is more efficient. When there are few records in the query, MySQL will use index query.
The total amount of data in the table is 19714 records, which means that when 2525/19714=13%, MySQL uses index query. When 3910/19714=20%, MySQL performs a full table scan.

3: We change dataTime to datetime data type, then do we need to add quotation marks to the query condition? We remove the quotation marks of the dataTime query condition and see the result:

EXPLAIN select * from digitaltwin_meteorological where dataTime > 2021-10-16;

insert image description here
It can be seen that after removing the quotation marks, it becomes a full table scan again. Therefore, regardless of whether the field type is varchar or datetime, the value of the query condition needs to be quoted. Without quotes, MySQL will perform some calculations on this value. In fact, without quotes, 2021-10-16 is no longer the date of the 16th. Let's look at the following sql:

 select count(*) from digitaltwin_meteorological where dataTime > 2021-10-16;

The calculation result is 19714, which is the data of the entire table. Therefore, the datetime query condition also needs to be quoted.

4: The above analysis is all about dataTime in the case of datetime type. The original field type is varchar, so if we change it to varchar type, does the above conclusion still exist? We change the type and execute SQL again:

EXPLAIN select * from digitaltwin_meteorological where dataTime > '2021-10-16';

insert image description here

It can be seen that after changing to varchar type, query No. 16 becomes a full table scan instead of a range scan.
Change the condition to number 17 and see the execution result:

EXPLAIN select * from digitaltwin_meteorological where dataTime > '2021-10-17';

insert image description here

The query on the 17th used index query. We see that the data volume for the 17th is 1749.
Therefore, when the field type is varchar, in 1749/19714=9% of cases, the index will be used, and in 2525/19714=13% of cases, the full table will be scanned.
That is to say, when the data type is datetime, if the query result accounts for 13%, the index query will be performed, and when the data type is varchar, if the query result accounts for 13% of the data in the whole table, the whole table scan will be performed. This is one of the reasons why we set the date type to datetime instead of varchar.

Summarize

Through the above analysis, we can draw the following conclusions:
1. In range queries, when the amount of data queried reaches a certain range, MySQL believes that a full table scan is more efficient and will perform a full table scan instead of an index.
2. The value of the datetime field type must also be quoted when querying, otherwise MySQL will not process it by date.
3. For data in date format, set to varchar type, the critical value for range query to use index query or full table scan is lower than the critical value for datetime type query to use index query or full table scan, so if date type data is set to datetime type, there is a higher probability of using index query.

This is the end of this article about why MySQL does not use the date field index. For more relevant MySQL date field index content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • A brief discussion on which fields in Mysql are suitable for indexing
  • Solution to index failure in MySQL due to different field character sets
  • How to add and delete unique indexes for fields in MySQL
  • Mysql determines whether a table field or index exists

<<:  Example code showing common graphic effects in CSS styles

>>:  Modify the default color of the input box placeholder text -webkit-input-placeholder method

Recommend

MySQL query example explanation through instantiated object parameters

This article will introduce how to query data in ...

MySQL variable principles and application examples

In the MySQL documentation, MySQL variables can b...

Summary of the application of decorative elements in web design

<br />Preface: Before reading this tutorial,...

Introduction to Nginx regular expression related parameters and rules

Preface Recently, I have been helping clients con...

Viewing and analyzing MySQL execution status

When you feel that there is a problem with MySQL ...

VMware 15.5 version installation Windows_Server_2008_R2 system tutorial diagram

1. Create a new virtual machine from VMware 15.5 ...

How to use CSS counters to beautify ordered lists of numbers

In web design, it is very important to use an org...

Win10 configuration tomcat environment variables tutorial diagram

Before configuration, we need to do the following...

Reasons and solutions for slow MySQL query stuck in sending data

Because I wrote a Python program and intensively ...

Detailed explanation of MySQL sql99 syntax inner join and non-equivalent join

#Case: Query employee salary levels SELECT salary...

Parameters to make iframe transparent

<iframe src="./ads_top_tian.html" all...

Detailed explanation of non-parent-child component communication in Vue3

Table of contents First method App.vue Home.vue H...

Solutions to MySql crash and service failure to start

I have been in contact with PHP for so long, but ...