Home >Database >Mysql Tutorial >How Can I Optimize Multi-Column LIKE Queries in MySQL?
With frequent queries like SELECT x, y, z FROM table WHERE x LIKE '%text%' OR y LIKE '%text%' OR z LIKE '%text%', optimizing MySQL for such operations becomes imperative. While indexes typically enhance query efficiency for indexed columns, the issue lies with the wildcard usage (%text%) in the LIKE clause.
For textual columns, indexes work by indexing characters starting from the left. However, LIKE queries with leading and trailing wildcards (e.g., %text%) prevent index utilization since the text's starting position is unknown.
Rather than relying on indexes, consider the following approaches:
If the affected table uses the MyISAM storage engine, MySQL provides FTS. This feature facilitates efficient text search by indexing entire words instead of a fixed number of characters.
For non-MyISAM tables, create a separate index table that stores words and their corresponding IDs in the main table. This essentially mimics the FTS functionality, enabling fast lookups on indexed terms.
MySQL versions 5.6 and later extend FTS support to InnoDB tables, providing an efficient alternative for both storage engines.
The above is the detailed content of How Can I Optimize Multi-Column LIKE Queries in MySQL?. For more information, please follow other related articles on the PHP Chinese website!