Home > Database > Mysql Tutorial > How Can I Perform Efficient Full-Text Search on InnoDB Tables Using MyISAM and Stored Procedures?

How Can I Perform Efficient Full-Text Search on InnoDB Tables Using MyISAM and Stored Procedures?

Linda Hamilton
Release: 2024-12-16 12:14:15
Original
765 people have browsed it

How Can I Perform Efficient Full-Text Search on InnoDB Tables Using MyISAM and Stored Procedures?

Fulltext-esque Search on InnoDB Using MyISAM and Stored Procedures

Finding a quick and efficient way to perform fulltext searches on an InnoDB table can be a challenge. While tools like Sphinx provide a solution, this article explores a pure MySQL approach using MyISAM and stored procedures.

The Challenge

A simple query that searches for multiple strings in a row using LIKE statements can be problematic due to its slow performance and limitations.

SELECT ... WHERE row LIKE '%some%' OR row LIKE '%search%' OR  row LIKE '%string%'
Copy after login

The Solution

The proposed solution involves creating a MyISAM fulltext table that indexes back into the InnoDB tables. This allows for indexing a specific column (e.g., subject) that requires searching.

Database Structure

The InnoDB tables are defined for the primary data, such as users and threads.

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

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

create table threads (
...
subject varchar(255) not null, -- column we want to search
...
)
engine=innodb;
Copy after login

The MyISAM fulltext table (e.g., threads_ft) provides the search index.

create table threads_ft (
...
subject varchar(255) not null,
fulltext (subject), -- fulltext index on subject
...
)
engine=myisam;
Copy after login

Stored Procedure

The stored procedure (e.g., ft_search_threads) accepts the search term as input and performs the search using the MyISAM fulltext index. It then joins the results with the InnoDB tables to retrieve the necessary data.

create procedure ft_search_threads (
in p_search varchar(255)
)
begin
...
select
 t.*,
 ...
from
 threads_ft tft
...
match(tft.subject) against (p_search in boolean mode) 
...
order by 
 rank desc
...
end;
Copy after login

Example Usage

The stored procedure can be called from a PHP application or a command-line tool:

call ft_search_threads('+innodb +clustered +index');
Copy after login

This example returns the top 100 results from the threads table that match the search term "innodb clustered index."

By utilizing this approach, you can achieve fulltext-like search functionality on InnoDB tables without compromising performance. It involves creating a dedicated MyISAM table for indexing and writing a stored procedure to perform the search and data retrieval.

The above is the detailed content of How Can I Perform Efficient Full-Text Search on InnoDB Tables Using MyISAM and Stored Procedures?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template