如何使用MySQL的全文索引提高搜索性能
ALTER TABLE
语句添加全文索引即可。以下是一个示例代码:ALTER TABLE
语句添加全文索引即可。以下是一个示例代码:ALTER TABLE `table_name` ADD FULLTEXT(`column_name`);
table_name
是要创建全文索引的表名,column_name
是要添加全文索引的列名。
SELECT * FROM `table_name` WHERE MATCH(`column_name`) AGAINST ('keyword');
可以将table_name
替换成要搜索的表名,column_name
替换成要搜索的列名,keyword
SELECT * FROM `table_name` WHERE MATCH(`column_name`) AGAINST ('keyword' IN BOOLEAN MODE);
table_name
是要创建全文索引的表名,column_name
是要添加全文索引的列名。SELECT * FROM `table_name` WHERE MATCH(`column_name`) AGAINST ('keyword*' IN BOOLEAN MODE);
table_name
替换成要搜索的表名,column_name
替换成要搜索的列名,keyword
替换成要搜索的关键词。精确匹配搜索:
SELECT * FROM `table_name` WHERE MATCH(`column_name`) AGAINST ('keyword1 +keyword2' IN BOOLEAN MODE);
SET ft_min_word_len = 2;
SET ft_stopword_file = 'path/to/stopwords.txt';
设置最小字符长度:
SELECT * FROM `table_name` WHERE MATCH(`column_name`) AGAINST ('+keyword1 +keyword2' IN BOOLEAN MODE);
-- 创建全文索引 ALTER TABLE `articles` ADD FULLTEXT(`title`, `content`); -- 搜索文章 SELECT * FROM `articles` WHERE MATCH(`title`, `content`) AGAINST ('MySQL' IN BOOLEAN MODE);
以上是如何使用MySQL的全文索引提高搜索性能的详细内容。更多信息请关注PHP中文网其他相关文章!