如何使用MySQL的全文索引來提高搜尋效能
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);
SELECT * FROM `table_name` WHERE MATCH(`column_name`) AGAINST ('keyword*' IN BOOLEAN MODE);
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中文網其他相關文章!