How to optimize the slow Like fuzzy query in MySQL

How to optimize the slow Like fuzzy query in MySQL

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 index

1. 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. INSTR

I had never heard of this at first, but after looking up some information today, I learned about this precious thing.

instr(str,substr) : Returns the position of the first occurrence of substr in string str. If the string is not found, it returns 0, otherwise it returns the position (starting from 1)

#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.

Summarize

This 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:
  • Detailed introduction to the use of MySql like fuzzy query wildcards
  • How to solve the slow speed of MySQL Like fuzzy query
  • Implementation of fuzzy query like%% in MySQL
  • Some summary of MySQL's fuzzy query like

<<:  Install .NET 6.0 in CentOS system using cloud server

>>:  Quick understanding of Vue routing navigation guard

Recommend

Windows 10 installation vmware14 tutorial diagram

Software Download Download software link: https:/...

Analysis of the method of setting up scheduled tasks in mysql

This article uses an example to describe how to s...

Detailed explanation of MySQL binlog usage

binlog is a binary log file that records all DML ...

Detailed process of implementing the 2048 mini game in WeChat applet

Rendering Example Code Today we are going to use ...

Tomcat+Mysql high concurrency configuration optimization explanation

1.Tomcat Optimization Configuration (1) Change To...

MySQL fuzzy query statement collection

SQL fuzzy query statement The general fuzzy state...

Simple web page code used in NetEase blog

How to use the code in NetEase Blog: First log in...

Web page text design should be like smart girls wearing clothes

<br />"There are no ugly women in the w...

HTML table tag tutorial (33): cell vertical alignment attribute VALIGN

In the vertical direction, you can set the cell a...

Vue implements Dialog encapsulation

Table of contents Vue2 Writing Vue3 plugin versio...

Introduction to MySQL isolation level, lock and MVCC

This article aims to clarify the relationship bet...

Detailed explanation of MySQL precompilation function

This article shares the MySQL precompilation func...

Example of how to change the line spacing of HTML table

When using HTML tables, we sometimes need to chan...

How to add indexes to MySQL

Here is a brief introduction to indexes: The purp...