Home > Database > Mysql Tutorial > ## How Can I Make MySQL LIKE \'%string%\' Queries Efficient in InnoDB?

## How Can I Make MySQL LIKE \'%string%\' Queries Efficient in InnoDB?

Mary-Kate Olsen
Release: 2024-10-25 07:23:29
Original
787 people have browsed it

## How Can I Make MySQL LIKE '%string%' Queries Efficient in InnoDB?

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!

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