Written in mysql Mysql fuzzy query like efficiency, as well as more efficient

When using msyql fuzzy query, it is natural to use statements like, usually, in a small amount of data, the efficiency of the query is not easy to see, but the amount of data to reach one million, ten million of time, the efficiency of queries easily apparent. The efficiency of this query when it is very important!

Under normal circumstances like the wording is vague queries (field indexed):

SELECT `column` FROM `table` WHERE `field` like '%keyword%';

The above statement with explain explanation of view, SQL statements, did not use the index, but the whole table search, if a large amount of data in the time, we can imagine this would be the last of efficiency

Compare the following wording:

SELECT `column` FROM `table` WHERE `field` like 'keyword%';

Such written explanation see with explain, SQL statements using the index, search efficiency greatly improved!

 

But sometimes, we do keyword fuzzy query when queries are not in order at the beginning, so if not special requirements, "keywork%" is not appropriate for all fuzzy queries

This time Ali Baba asked me, how to solve the above problem?

We can use the following method:

LOCATE(substr,str), LOCATE(substr,str,pos)

The first syntax returns the position of the first occurrence of substr in string str. The second syntax returns sub-string substr character position in the string str, the first time starting from at pos. Str substr If not, then the return value is 0.

Copy the code
SELECT LOCATE('xbar',`foobar`); 
### return 0 

SELECT LOCATE('bar',`foobarbar`); 
### returns 4

SELECT LOCATE('bar',`foobarbar`,5);
### returned 7
Copy the code
SELECT `column` FROM `table` WHERE LOCATE('keyword', `field`)>0

NOTE: keyword is content to be searched, field to field is matched, check out the data of all existing keyword

2.POSITION('substr' IN `field`)方法

locate the position can be seen as an alias, locate function with the same

SELECT `column` FROM `table` WHERE POSITION('keyword' IN `filed`)

3.INSTR ( `str`, 'substr') Method

SELECT `column` FROM `table` WHERE INSTR(`field`, 'keyword' )>0 

In addition to the above-described method, there is a function FIND_IN_SET

FIND_IN_SET(str1,str2):

Returns the location where the index str2 in str1, where str2 must be "" separated.

 

Use like, or use the locate position is a prerequisite for efficient field above query has been established index.

When using msyql fuzzy query, it is natural to use statements like, usually, in a small amount of data, the efficiency of the query is not easy to see, but the amount of data to reach one million, ten million of time, the efficiency of queries easily apparent. The efficiency of this query when it is very important!

Under normal circumstances like the wording is vague queries (field indexed):

SELECT `column` FROM `table` WHERE `field` like '%keyword%';

The above statement with explain explanation of view, SQL statements, did not use the index, but the whole table search, if a large amount of data in the time, we can imagine this would be the last of efficiency

Compare the following wording:

SELECT `column` FROM `table` WHERE `field` like 'keyword%';

Such written explanation see with explain, SQL statements using the index, search efficiency greatly improved!

 

But sometimes, we do keyword fuzzy query when queries are not in order at the beginning, so if not special requirements, "keywork%" is not appropriate for all fuzzy queries

This time Ali Baba asked me, how to solve the above problem?

We can use the following method:

LOCATE(substr,str), LOCATE(substr,str,pos)

The first syntax returns the position of the first occurrence of substr in string str. The second syntax returns sub-string substr character position in the string str, the first time starting from at pos. Str substr If not, then the return value is 0.

Copy the code
SELECT LOCATE('xbar',`foobar`); 
### return 0 

SELECT LOCATE('bar',`foobarbar`); 
### returns 4

SELECT LOCATE('bar',`foobarbar`,5);
### returned 7
Copy the code
SELECT `column` FROM `table` WHERE LOCATE('keyword', `field`)>0

NOTE: keyword is content to be searched, field to field is matched, check out the data of all existing keyword

2.POSITION('substr' IN `field`)方法

locate the position can be seen as an alias, locate function with the same

SELECT `column` FROM `table` WHERE POSITION('keyword' IN `filed`)

3.INSTR ( `str`, 'substr') Method

SELECT `column` FROM `table` WHERE INSTR(`field`, 'keyword' )>0 

In addition to the above-described method, there is a function FIND_IN_SET

FIND_IN_SET(str1,str2):

Returns the location where the index str2 in str1, where str2 must be "" separated.

 

Use like, or use the locate position is a prerequisite for efficient field above query has been established index.

Guess you like

Origin www.cnblogs.com/ysySelf/p/11163487.html