Optimizing MySQL Queries for LIKE '%string%' in InnoDB
Problem Description
When using the LIKE '%string%' clause in a query on an InnoDB table, it is observed that the index on the 'keywords' column becomes ineffective, resulting in full table scans.
The Solution
InnoDB indexes are optimized for string comparisons that start at the beginning of the indexed column. However, the LIKE '%string%' clause allows the search term to appear anywhere in the column. This type of query cannot use start-based indexes because the search term is not anchored at the start of the string.
Fulltext Index as the Optimal Solution
To optimize queries that involve floating search terms in InnoDB, fulltext indexes should be used. Fulltext indexes are specifically designed to handle such queries, as they allow for flexible string matching.
Upgrading to MySQL 5.6.4 or Later
InnoDB support for fulltext indexes was introduced in MySQL 5.6.4. If your MySQL version is below 5.6.4, it is recommended to upgrade to utilize this feature. By upgrading to MySQL 5.6.4 or later, you will be able to use fulltext indexes to optimize LIKE '%string%' queries in InnoDB.
The above is the detailed content of ## How Can I Make MySQL LIKE \'%string%\' Queries Efficient in InnoDB?. For more information, please follow other related articles on the PHP Chinese website!