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: 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: 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: 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. 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;
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'; It can be seen that after changing to varchar type, query No. 16 becomes a full table scan instead of a range scan. EXPLAIN select * from digitaltwin_meteorological where dataTime > '2021-10-17'; The query on the 17th used index query. We see that the data volume for the 17th is 1749. Summarize Through the above analysis, we can draw the following conclusions: 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:
|
<<: Example code showing common graphic effects in CSS styles
>>: Modify the default color of the input box placeholder text -webkit-input-placeholder method
This article will introduce how to query data in ...
In the MySQL documentation, MySQL variables can b...
<br />Preface: Before reading this tutorial,...
Preface Recently, I have been helping clients con...
When you feel that there is a problem with MySQL ...
Under Linux, if you download and install an appli...
1. Create a new virtual machine from VMware 15.5 ...
In web design, it is very important to use an org...
Before configuration, we need to do the following...
Table of contents Scenario Task idea analyze Conc...
Because I wrote a Python program and intensively ...
#Case: Query employee salary levels SELECT salary...
<iframe src="./ads_top_tian.html" all...
Table of contents First method App.vue Home.vue H...
I have been in contact with PHP for so long, but ...