一尘不染

以任何方式在InnoDB上实现类似全文的搜索

mysql

我有一个非常简单的查询:

SELECT ... WHERE row LIKE '%some%' OR row LIKE '%search%' OR  row LIKE '%string%'

进行搜索some search string,但是正如您所看到的,它会分别搜索每个字符串,而且对性能也不好。

有没有一种方法可以在InnoDB表上使用LIKE重新创建类似全文的搜索。当然,我知道我可以使用Sphinx之类的东西来实现此目的,但是我正在寻找一个纯MySQL解决方案。


阅读 232

收藏
2020-05-17

共1个答案

一尘不染

使用myisam全文表来索引回innodb表,例如:

使用innodb构建系统:

create table users (...) engine=innodb;

create table forums (...) engine=innodb;

create table threads
(
forum_id smallint unsigned not null,
thread_id int unsigned not null default 0,
user_id int unsigned not null,
subject varchar(255) not null, -- gonna want to search this... !!
created_date datetime not null,
next_reply_id int unsigned not null default 0,
view_count int unsigned not null default 0,
primary key (forum_id, thread_id) -- composite clustered PK index
)
engine=innodb;

现在是全文搜索表,我们将使用它来索引回innodb表。您可以使用触发器或每晚进行批更新等来维护此表中的行。

create table threads_ft
(
forum_id smallint unsigned not null,
thread_id int unsigned not null default 0,
subject varchar(255) not null,
fulltext (subject), -- fulltext index on subject
primary key (forum_id, thread_id) -- composite non-clustered index 
)
engine=myisam;

最后,您从php /应用程序调用的搜索存储过程:

drop procedure if exists ft_search_threads;
delimiter #

create procedure ft_search_threads
(
in p_search varchar(255)
)
begin

select
 t.*,
 f.title as forum_title,
 u.username,
 match(tft.subject) against (p_search in boolean mode) as rank
from
 threads_ft tft
inner join threads t on tft.forum_id = t.forum_id and tft.thread_id = t.thread_id
inner join forums f on t.forum_id = f.forum_id
inner join users u on t.user_id = u.user_id
where
 match(tft.subject) against (p_search in boolean mode) 
order by 
 rank desc
limit 100;

end;

call ft_search_threads('+innodb +clustered +index');

希望这可以帮助 :)

2020-05-17