Article directory
1 Introduction
When we usually use msyql and need fuzzy matching fields, our first reaction is to use like query statements for fuzzy matching. When the amount of data is small, we usually don’t feel whether the query efficiency is affected, but when the amount of data reaches millions , when the level is tens of millions, the low efficiency of like query is easy to show. At this time, query efficiency becomes very important.
2. The reason for the low efficiency of directly using the like statement
Next, create a test table as the data for all demonstrations.
Using like directly in mysql will not affect the index used by the query, but the index will become invalid after the wildcard (% or _) is used at the beginning, and it will inevitably cause performance problems when the amount of query data is large.
3. Fuzzy query optimization method
1. Index
SELECT `column` FROM `table` WHERE `field` like 'keyword%';
Without an index, it is a full table search. With an index, the speed will be greatly improved, but it is not applicable to all searches, only applicable to the beginning of the "keywork%" keyword
2.LOCATE('substr',str,pos) method
SELECT `column` FROM `table` WHERE LOCATE('keyword', `field`)>0
keyword is the content to be searched, field is the field to be matched, query all the data with keyword
3.POSITION('substr' IN field) method
SELECT `column` FROM `table` WHERE POSITION('keyword' IN `filed`)
position can be regarded as an alias of locate, the function is the same as locate
4. INSTR(str,'substr') method
SELECT `column` FROM `table` WHERE INSTR(`field`, 'keyword')>0
5. CONTAINS ( column
, str) method
SELECT `column` FROM `table` WHERE CONTAINS(`column`, 'keyword')
4. String exact match
As shown in the data in the test table, if we use like fuzzy query, the %admin
query result will cause the data in the student field not ending with admin to be unable to be found out, and using it %admin%
will cause this kind of data to be found out.admin1
admin12
In order to solve this kind of problem, it is necessary to use the function FIND_IN_SET('str', ) for exact matching column
.
1. Default function
select * from test where find_in_set('admin',student);
search result:
2. Custom functions
Sometimes the delimiter is not necessarily separated by commas, there may be various delimiters, such as |, &, etc., which require custom functions.
select * from test where find_in_set('admin',REPLACE(student,'|',','));
search result:
The above is all about MySQL fuzzy query optimization and exact string matching. I hope it will be helpful to everyone.