1. Introduction:I built a "Student Management System" in which there is a student table and four tables (group table, class table, tag table, city table) for joint fuzzy query, the efficiency is very low, so I thought about how to improve the efficiency of like fuzzy query Note: Before reading this blog, please check: How to view the execution time of SQL statements in MySQL 2. The first idea is to build an index1. The like %keyword index is invalid, so a full table scan is used. 2. Like keyword% index is valid. 3. Like %keyword% index is invalid, use full table scan. Tested using explain: Original table (Note: the case uses the student table as an example) -- User table create table t_users( id int primary key auto_increment, -- Username varchar(20), -- Password password varchar(20), -- Real name real_name varchar(50), -- Gender 1 means male 0 means female sex int, --birth date, -- Mobile phone number mobile varchar(11), -- The uploaded avatar path head_pic varchar(200) ); Create an index #create index index name on table name (column name); create index username on t_users(username); Like %keyword% index is invalid, use full table scan explain select id,username,password,real_name,sex,birth,mobile,head_pic from t_users where username like '%h%'; like keyword% index is valid. explain select id,username,password,real_name,sex,birth,mobile,head_pic from t_users where username like 'wh%'; Like %keyword index is invalid, and full table scan is used. 3. INSTRI had never heard of this at first, but after looking up some information today, I learned about this precious thing. #instr(str,substr) method select id,username,password,real_name,sex,birth,mobile,head_pic from t_users where instr(username,'wh')>0 #0.00081900 #Fuzzy query select id,username,password,real_name,sex,birth,mobile,head_pic from t_users where username like 'whj'; # 0.00094650 The main reason why the efficiency difference between the two is not big is that the data is small. It is best to prepare more original data for testing for the best effect. P.S. Does Like use index?1. The like %keyword index is invalid, so a full table scan is used. But you can use flip function + fuzzy query before like + create flip function index = use flip function index instead of full table scan. 2. Like keyword% index is valid. 3. The like %keyword% index is invalid and the reverse index cannot be used. SummarizeThis is the end of this article about how to optimize the slow Like fuzzy query in MySQL. For more information about optimizing the slow Like fuzzy query in MySQL, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Install .NET 6.0 in CentOS system using cloud server
>>: Quick understanding of Vue routing navigation guard
Software Download Download software link: https:/...
This article uses an example to describe how to s...
binlog is a binary log file that records all DML ...
Rendering Example Code Today we are going to use ...
1.Tomcat Optimization Configuration (1) Change To...
SQL fuzzy query statement The general fuzzy state...
How to use the code in NetEase Blog: First log in...
<br />"There are no ugly women in the w...
In the vertical direction, you can set the cell a...
Table of contents 1. JavaScript is single-threade...
Table of contents Vue2 Writing Vue3 plugin versio...
This article aims to clarify the relationship bet...
This article shares the MySQL precompilation func...
When using HTML tables, we sometimes need to chan...
Here is a brief introduction to indexes: The purp...