Which query is faster and why for a database with 1M records in a table ? How can I optimize the query ?
First one:
SELECT id,column1,column2 FROM `table1` WHERE MATCH(column1)
AGAINST('string1 string2 string3 ' in boolean mode) ORDER BY column2 ASC LIMIT 10
Second one:
SELECT id,column1,column2 FROM `table1` WHERE column1 LIKE %string1%
OR column1 LIKE %string2% OR column1 LIKE %string3% OR column2 LIKE %string1%
OR column2 LIKE %string2% OR column2 LIKE %string3% OR column3 LIKE %string1% OR
column3 LIKE %string2% OR column3 LIKE %string3% ORDER BY column2 ASC LIMIT 10
Match against is a fulltext search. If you created a fulltext search index for MySQL it will be much faster than the LIKE ‘%…%’, because in this case there will be no index.
The reason why there cannot be any index on ‘%…%’, is because it is simply not fast to create an index on every letter of your strings.
Also those two query’s have a different function, the fulltext index scans for words and is only usable with words. You cannot search a string “emil” with “match against(‘mi’ in boolean mode). That will not work.
Of course with like this works.