Optimizing Multi-Column LIKE Queries in MySQL
Slow SELECT queries using LIKE clauses on multiple columns can be a performance bottleneck. In this article, we'll explore whether indexing can enhance query speed and address concerns about disk usage and INSERT/DELETE performance.
Can an Index Improve LIKE Performance?
Unfortunately, indexes are ineffective for LIKE queries in MySQL because they work by indexing a fixed number of characters starting from the left. With LIKE '%text%', there can be an arbitrary amount of text before the target string, preventing index usage.
Alternative Approaches
Instead of using LIKE, consider FTS (Full Text Search). FTS is supported for MyISAM tables and enables efficient searching for partial string matches. For non-MyISAM tables, implementing your own indexing system using a separate index table to map words to IDs can replicate FTS functionality.
Disk Usage and INSERT/DELETE Performance
FTS indexing can increase disk usage. However, the impact on INSERT/DELETE operations is minimal. This is because inserts only add new words to the index, which is done as part of the transaction, while deletes mark words for later removal.
Update
MySQL 5.6 introduced FTS support for InnoDB tables, offering a robust option for improving LIKE performance on various table types.
The above is the detailed content of Can Indexing Improve MySQL's Multi-Column LIKE Query Performance?. For more information, please follow other related articles on the PHP Chinese website!