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
Install Docker You have to install Docker, no fur...
1. Introduction I have been studying the principl...
I have written such an article before, but I used...
Table of contents 1. What is Promise? 2. Why is t...
Table of contents background example Misconceptio...
In the Linux environment, you want to check wheth...
MySQL multi-condition query with AND keyword. In ...
Prepare war package 1. Prepare the existing Sprin...
Table of contents 1. Sample code 2. See the essen...
Table of contents Install Pagoda Configure Python...
<br />This section introduces how to impleme...
Code Sample Add a line of code in the head tag: XM...
1. Alibaba Cloud selects the appropriate cloud se...
This article uses examples to illustrate the use ...
As shown below: select a1,a2,a1+a2 a,a1*a2 b,a1*1...