As you do the replace on the actual data in the table, MySQL can't use the index, as it doesn't have any indexed data of the result of the replace which it needs to compare to the needle
.
That said, if your replace settings are static, it might be a good idea to denormalize the data and to add a new column like haystack_search
which contains the data with all the replaces applied. This column could be filled during an INSERT
or UPDATE
. An index on this column can then effectively be used.
Note that you probably want to use %
in your LIKE
query as else it is effectively the same as a normal equal comparison. Now, if you use a searchterm like %needle%
(that is with a variable start), MySQL again can't use the index and falls back to a table scan as it only can use the index if it sees a fixed start of the search term, i.e. something like needle%
.
So in the end, you might end up having to tune your database engine so that it can held the table in memory. Another alternative with MyISAM tables (or with MySQL 5.6 and up also with InnoDB tables) is to use a fulltext index on your data which again allows rather efficient searching.