当前位置:网站首页>Do like and in indexes in MySQL go
Do like and in indexes in MySQL go
2022-07-29 00:15:00 【Unpaid mage】
Today, let's practice
First, we create a user table to test
Like
stay email Add an index to the field to test Like keyword 
Let's review Like Several ways of writing sentences
Say... In the big direction Like Statements are written in two ways , When we parted % and _.
%: Used to match the appearance of several characters ( It can also be 0 individual )
_: Used to match the appearance of a single character
Our main fuzzy query in work is also %, Let's focus on one Like Several ways of writing
select * from t_user where email like ‘ Characters you know %’
select * from t_user where email like ‘% Characters you know ’
select * from t_user where email like ‘% Characters you know %’
Let's take a look at these three cases of indexing
Let's look at the first way of writingselect * from t_user where email like ‘ Characters you know %’
Use explain Look at the 
Obviously, this statement is indexed .
Is there a problem here ? Let's modify the data in the database. Will this line of statement still go through the index ?
We modify the data to duplicate , Reduce the index discrimination and try again .
Re pass explain Analyze the results 
adopt type Column ALL It is obvious that this statement has not gone through the index , Because at this time, in the database email Field discrimination is no longer supported 123% This way of writing is indexed , So use the full table scan .
Next, let's look at the second way
select * from t_user where email like '% Characters you know '
First look at the test data 
Let's carry out this line sql
SELECT * from t_user where email LIKE '%[email protected]'
You can stop and think about whether you will go to the index , Pay attention to what I write sql And database data . This article sql The discrimination of the target data queried in the database is no problem , and email Fields are also indexed , Logically speaking, this statement will definitely follow the index .
Let's see explain Analysis results of 
Judging from the results, there is no index , In fact this with mysql The index organization structure of .
% Add to the front mysql Cannot use index query , because mysql I do not know! % How many characters does it represent , So we can only scan the whole table .
The third way of writing is actually the same as the second way , Also belong to % The one in front , Also won't go index .
In
in Whether to go or not depends on the amount of data behind the condition , When the amount of data is small, the index will be used , And when in When the amount of data followed is large, the full table scan will be performed .mysql The bottom layer may have repeated data processing for the following conditions ; Refer to others' blogs to explain that the range of conditions accounts for% of the total data 30% The whole table will be scanned around , Abandon the way index is executed ; It may also involve the determination of the proportion of the results after de duplication of the following data !
You can see this blog in detail , It's still very detailed .
边栏推荐
- 1-6 state与绑定事件
- 2022网络安全学习路线 非常详细 推荐学习
- JS advanced ES6 ~ es13 new features
- How can Plato obtain premium income through elephant swap in a bear market?
- Linux之yum安装MySQL
- Application of Devops in Internet of things solutions
- Leetcode63. 不同路径 II
- EN 1873 assembly accessories for roofing - plastic single roof lamps - CE certification
- Oracle super full SQL, details crazy
- Intelligent trash can (VII) -- Introduction and use of sg90 steering gear (Pico implementation of raspberry pie)
猜你喜欢

Detailed principle explanation and verification results of digital clock based on FPGA

With the help of rpa+lcap, the enterprise treasurer management can be upgraded digitally

【TA-霜狼_may-《百人计划》】美术2.2 模型基础

Powercl batch creates and manages virtual switches

Classification and determination method of Worthington stemxyme

Principle of meter skipping

以JSP为视图解析器搭建SSM项目

SQL implementation merges multiple rows of records into one row

Virtual lab basic experiment tutorial -8. Fourier transform (1)

Servlet operation principle_ API details_ Advanced path of request response construction (servlet_2)
随机推荐
Android studio connects to MySQL and completes simple login and registration functions
1-6 state and binding events
JS高级 之 ES6~ES13 新特性
【MySQL 8】Generated Invisible Primary Keys(GIPK)
【C】 Reverse string (two recursive ideas)
MySQL安装配置教程(超级详细、保姆级)
【小程序项目开发 -- 京东商城】uni-app 商品分类页面(上)
Leetcode64. Minimum path sum
【MySQL系列】MySQL数据库基础
Summary of wrong questions of software designers
VS2005 accesses the setting method "recommended collection" of vss2005 through sourceoffsite
PMP Exam countdown, look at 3A pass bag!
Js判断数据类型的4种⽅式
Using recursion and chain header interpolation to realize the group turnover of linked lists -- leetcode25 K group turnover linked lists
Everything you have learned will come in handy at some point in your life (turn)
JS advanced ES6 ~ es13 new features
Real time data warehouse: Netease strictly selects the practice of real-time data warehouse based on Flink
AutoCAD -- import excel tables into CAD and merge CAD
NAT如何配置地址转换
Detailed explanation of the usage of exists in MySQL