InnoDB 可以利用类似全文搜索的功能吗?
在搜索多个搜索字符串时,使用多个 LIKE 运算符的简单查询可能会变得低效。要在不使用 Sphinx 等外部工具的情况下在 InnoDB 表上解决此问题,请考虑采用 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, -- Desired search field 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;
接下来,使用以下命令创建全文搜索表MyISAM:
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;
搜索存储过程
最后,为全文搜索创建一个存储过程:
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');
通过这些修改,您可以在 InnoDB 表上有效地实现类似全文的搜索功能,而无需求助于外部解决方案。
以上是InnoDB 表可以在没有外部工具的情况下利用全文搜索功能吗?的详细内容。更多信息请关注PHP中文网其他相关文章!