Mysql full-text index usage and limitations

Project scenario:

Recently, I am doing performance optimization, using full-text index, and making a record.


Problem Description

When we do list queries, most of the time we will encounter queries like '%%', and this kind of query will invalidate the index.
When the amount of data is large, it will make the query very slow. Of course, we can use es to optimize, the introduction of es will also make the code more complicated than not to use, so we just use mysql directly when it is not necessary.



solution:

1. The limitation of mysql full-text index requires mysql version 5.7 and above to support it. If you don’t know the version, use the command to check the version number.

select version();

Second, create an index sql as follows

CREATE FULLTEXT INDEX idx_fullname ON t_course(name) WITH PARSER ngram;
CREATE FULLTEXT INDEX idx_fullname ON t_course_type(name) WITH PARSER ngram;

Here you need to use WITH PARSER ngram is suitable for Chinese word segmentation, if the content to be searched is in English, you don't need to add the following paragraph.
3. Modify the mysql configuration as follows.
In your mysql installation directory, find the configuration file, D:\javaTools\mysql-8.0.12-winx64\my.ini, if not, create a new one yourself. Add the following configuration

innodb_ft_min_token_size = 1 #配置分词最小单位
ft_min_word_len = 1 #配置分词最小单位

A reboot is required after modification to take effect.
The reason for adding these two configurations is to divide the granularity to the minimum. If not, you may search for "Hello", and the content contains these two words, but you cannot search them. The default value of these two values ​​is 4. So modify it.
After restarting, use the command to check whether the configuration takes effect

show variables like '%ft%';

insert image description here

Fourth, use the full-text index search.

select
	tc.id,
	tc.name
from
	t_course tc 
where
match(name) against ('测试') limit 5

insert image description here
You can see that the content related to the test is found out, explain to see if it is indexed.
insert image description here

You can see that the full-text index just left.
5. Multi-field full-text index

CREATE FULLTEXT INDEX idx_fullname ON t_course(name,字段名) WITH PARSER ngram;
match(name,字段名) against ('测试')

Just build a composite index like above.
6. Join to query the full-text index of two tables.
There is a case of join query, where both tables have fields that require full-text indexing. After testing, this situation does not work. Similarly, there is also a full-text index for the name field of the coursetype table.

explain select
	tc.id,
	tc.name,
	tct.name,
	tct.id 
from
	t_course tc left join t_course_type tct  on tc.course_type =tct.id 
where
1=1 
and (
match(tc.name) against ('测试') 
or match(tct.name) against ('测试') 
)

insert image description here
In this way, the index will not be searched, and the name and type name of the course will be fuzzy searched at the same time. This kind of situation can only be solved by another way. You can consider using
(a left join b where match(a.name) ) union (a right join b math(b.name))
this method to achieve it, which increases the complexity of SQL a lot. In fact, it is not recommended to use it like this. It is better to just put es directly.

Summarize

This article introduces how to use the full-text index. The points to be noted have already been written above. I will summarize the practical usage plan for everyone. If you need it, you can refer to it.

Guess you like

Origin blog.csdn.net/qq_34526237/article/details/127580600