当前位置:网站首页>Several cases of index failure
Several cases of index failure
2022-07-01 11:06:00 【Rookie cat meow meow】
This article is just personal notes , If there is any error, please remind me
The best left prefix rule
The right side of the range query is invalid
like Index invalidation principle
Index invalidation caused by implicit conversion
Look at this watch
CREATE TABLE `test1` (
`id` int(11) NOT NULL,
`num1` int(11) NOT NULL DEFAULT '0',
`num2` varchar(11) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
KEY `num1` (`num1`),
KEY `num2` (`num2`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
SQL test
Take a look at this group first SQL, There are four , Our test data sheet num1 yes int type ,num2 yes varchar type , But the stored data is related to the primary key id The same sequence of numbers , Both fields are indexed .
1: SELECT * FROM `test1` WHERE num1 = 10000;
2: SELECT * FROM `test1` WHERE num1 = '10000';
3: SELECT * FROM `test1` WHERE num2 = 10000;
4: SELECT * FROM `test1` WHERE num2 = '10000';
among 124 Three SQL Basically, the results are instantaneous , In about 0.001 ~ 0.005 second , With tens of millions of data, such a result can determine these three SQL The performance is basically the same . But third SQL, Many tests are basically time-consuming 4.5~4.8 Between seconds .
Why? 34 Two article SQL The efficiency difference is so big , But the same comparison 12 Two article SQL But it doesn't make any difference ? Take a look at the implementation plan , Bottom respectively 1234 strip SQL Execution plan data : You can see ,124 Three SQL Can use the index , The connection types are ref, The number of scanning lines is 1, So it's very efficient . Look at the third SQL, No index , So scan for the whole table ,rows Directly to 1000 All the , So the performance difference is so big .
Look up MySQL The discovery of related documents is caused by implicit transformation , Look at the official description :
Official documents : 12.2 Type Conversion in Expression Evaluation
When operators are used with different types of operands , Type conversion occurs to make operands compatible . Some transformations occur implicitly . for example ,MySQL It will automatically convert strings into numbers as needed , vice versa . The following rules describe how the comparison operation is converted :
- At least one of the two parameters is NULL when , The result of the comparison is NULL, The special case is to use <=> The two one. NULL When making a comparison, it will return 1, There is no need for type conversion in either case
- Both parameters are strings , Compare by string , No type conversion
- Both arguments are integers , Compare... By integers , No type conversion
- When comparing hexadecimal values with non numeric values , It will be treated as a binary string
- One parameter is TIMESTAMP or DATETIME, And the other parameter is constant , Constants are converted to timestamp
- One parameter is decimal type , If the other parameter is decimal Or an integer , Will convert integers to decimal Then compare , If the other parameter is a floating-point number , It will put decimal Convert to floating point for comparison
- In all other cases , Both parameters are converted to floating-point numbers for comparison
According to official documents , Our first 23 Two article SQL Implicit conversion has taken place , The first 2 strip SQL Query criteria for num1 = ‘10000’, On the left is int To the right of the type is the string , The first 3 strip SQL contrary , Then according to the official conversion rule No 7 strip , Both left and right sides will be converted to floating-point numbers for comparison .
First look at the 2 strip SQL:SELECT * FROMtest1WHERE num1 = ‘10000’; On the left for int type 10000, Convert to floating point number or 10000, Right string type ’10000’, Converting to floating point numbers is also 10000. The conversion results of both sides are unique , So it does not affect the use of indexes .
The first 3 strip SQL:SELECT * FROMtest1WHERE num2 = 10000; On the left is the string type ’10000’, Convert floating point number to 10000 Is the only one. , On the right int type 10000 The conversion result is also unique . however , Because on the left is the search condition ,‘10000’ go to 10000 Although it is the only , But other strings can also be converted to 10000, such as ’10000a’,‘010000’,'10000’ And so on can be converted to floating point numbers 10000, In this case , You can't use indexes .
The rules found by consulting relevant materials are as follows :
- Strings that do not begin with a number will be converted to 0. Such as ’abc’、‘a123bc’、'abc123’ Will be transformed into 0;
- String conversion starting with a number will be intercepted , Intercept from the first character to the first non numeric content . such as ’123abc’ Will be converted to 123,'012abc’ Will be converted to 012 That is to say 12,'5.3a66b78c’ Will be converted to 5.3, Others in the same way .
Analyze and summarize
Through the above test, we found that MySQL Use some features of operators :
- When the data types on the left and right sides of the operator are inconsistent , There will be implicit conversions .
- When where Implicit conversion occurs when the left side of the query operator is a numeric type , Then it has little effect on efficiency , But it is still not recommended .
- When where Implicit conversion occurs when the left side of the query operator is a character type , Then the index will be invalidated , The scanning efficiency of the whole meter is extremely low .
- When a string is converted to a numeric type , Strings that do not begin with numbers are converted to 0, A string beginning with a number will intercept the value from the first character to the first non numeric content as the conversion result .
therefore , We're writing SQL We must form good habits when we are young , What type of fields are queried , The condition to the right of the equal sign is written as the corresponding type . Especially when the query field is a string , The condition to the right of the equal sign must be caused by quotation marks to indicate that this is a string , Otherwise, it will cause index failure and trigger full table scan .
Different character sets and sorting rules lead to index invalidation
Other :
- It's best to match all values
- Don't do anything on the index ( Calculation 、 function 、 Automatically / Manual type conversion ), Otherwise, it will lead to index invalidation and turn to full table scan
explain select * from ti where b+1=5; -- The field operation cannot be indexed
- Try to use overlay index ( Query only indexed columns ( The index column is consistent with the query column )), Reduce select *
- Use... On index fields (!= perhaps < >) When judging , It will cause index invalidation and turn to full table scan
- Use... On index fields is null / is not null When judging , It will cause index invalidation and turn to full table scan
- Index fields use or when , It will cause index invalidation and turn to full table scan
边栏推荐
- Leetcode 181 Employees exceeding the manager's income (June 29, 2022)
- 商城小程序源码开源版-可二开
- Matplotlib数据可视化基础
- 京东与腾讯续签合作:向腾讯发行A类股 价值最高达2.2亿美元
- Can I choose to open an account on CICC securities? Is it safe?
- 网站源码整站下载 网站模板源代码下载
- Rising Stars in Plant Sciences (RSPS2022) Finalist科学演讲会(6.30晚9点)
- node版本管理器nvm安装及切换
- 银行卡借给别人是否构成犯罪
- flutter path_ Provider: ^2.0.10 can get temporary directory
猜你喜欢
《数据安全法》出台一周年,看哪四大变化来袭?
12款大家都在用的产品管理平台
移动硬盘驱动器读到,但不显示盘符
关于Keil编译程序出现“File has been changed outside the editor,reload?”的解决方法
Mall applet source code open source version - two open
2022年6月编程语言排行,第一名居然是它?!
数据库实验报告(二)
Neurips 2022 | cell image segmentation competition officially launched!
Yoda unified data application -- Exploration and practice of fusion computing in ant risk scenarios
NC | intestinal cells and lactic acid bacteria work together to prevent Candida infection
随机推荐
CANN算子:利用迭代器高效实现Tensor数据切割分块处理
Huawei equipment is configured with large network WLAN basic services
Half of 2022 has passed, isn't it sudden?
Guys, how to export iceberg data to MySQL? What tools are there? Neither sqoop nor dataX
【AI资讯月刊】350+资源大盘点!6月不容错过的资料和动态,都都都在这里啦!<附下载>
MIT's latest paper, "the need for interpretable features: motivation and classification": building interpretability in the constituent elements of machine learning models
The exclusive collection of China lunar exploration project is limited to sale!
Mutual conversion of pictures in fluent uint8list format and pictures in file format
flutter path_provider: ^2.0.10可以获取临时目录
Submission lottery - light application server essay solicitation activity (may) award announcement
Cvpr22 | CMT: efficient combination of CNN and transformer (open source)
How does MySQL copy table data from one database to another (two databases are not linked to the same database)
名创拟7月13日上市:最高发行价22.1港元 单季净利下降19%
Handling distributed transactions with powerful dbpack (PHP tutorial)
Detailed explanation of linear regression in machine learning
CPI教程-异步接口创建及使用
Paxos 入门
获取键代码
Technology sharing | introduction to linkis parameters
kubernetes之ingress探索实践