Home > Database > Mysql Tutorial > Can InnoDB Tables Leverage Full-Text Search Functionality Without External Tools?

Can InnoDB Tables Leverage Full-Text Search Functionality Without External Tools?

Susan Sarandon
Release: 2024-12-17 18:40:12
Original
150 people have browsed it

Can InnoDB Tables Leverage Full-Text Search Functionality Without External Tools?

Can InnoDB Utilize Fulltext Search-Like Functionality?

A simple query with multiple LIKE operators can become inefficient when searching for multiple search strings. To remedy this on an InnoDB table without utilizing external tools like Sphinx, consider adopting a MyISAM fulltext table to index back into your InnoDB tables.

Database Schema

Create your database using InnoDB as the storage engine:

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;
Copy after login

Next, create the fulltext search table using 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;
Copy after login

Search Stored Procedure

Finally, create a stored procedure for your fulltext search:

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');
Copy after login

With these modifications, you can effectively implement fulltext-like search capabilities on your InnoDB tables without resorting to external solutions.

The above is the detailed content of Can InnoDB Tables Leverage Full-Text Search Functionality Without External Tools?. 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