Searching MySQL Data with Fulltext-Like Capabilities on InnoDB
The conventional approach of using "LIKE" for string search on an InnoDB table suffers from performance limitations and the need to check each search term individually. To enhance search functionality, a solution that mimics fulltext search on InnoDB tables is desirable.
Solution: Utilizing a Separate MyISAM Table for Indexing
This approach involves creating a separate MyISAM table that mirrors the InnoDB table. The MyISAM table contains an additional column indexed with the "FULLTEXT" keyword. By populating this MyISAM table with row data, we effectively create an index that supports fast, fulltext-like search capabilities.
Implementation
Consider the following example:
-- InnoDB tables CREATE TABLE users (id INT, name VARCHAR(255), PRIMARY KEY (id)); CREATE TABLE forums (id INT, name VARCHAR(255), PRIMARY KEY (id)); CREATE TABLE threads (id INT, subject VARCHAR(255), user_id INT, forum_id INT, PRIMARY KEY (id)); -- MyISAM fulltext table CREATE TABLE threads_ft (id INT, subject VARCHAR(255), FULLTEXT(subject), PRIMARY KEY (id));
Synchronizing Data
To keep the MyISAM fulltext table up to date, you can use triggers, batch updates, or any other suitable mechanism.
Performing Search Queries
Now, you can perform fulltext-like search queries:
-- Stored procedure for searching CREATE PROCEDURE ft_search_threads(IN p_search VARCHAR(255)) BEGIN SELECT * FROM threads_ft WHERE MATCH(subject) AGAINST (p_search IN BOOLEAN MODE) ORDER BY RANK() DESC; END; -- Example query CALL ft_search_threads('keyword1 keyword2 keyword3');
Benefits
This approach offers the following advantages:
The above is the detailed content of How Can I Achieve Fulltext-Like Search on InnoDB Tables in MySQL?. For more information, please follow other related articles on the PHP Chinese website!