Q: I have a question about using LIKE in SQL. Is it true that, because LIKE cannot take advantage of indices, it has to resort to a database scan? If it is true, is there no way to speed up a query that uses LIKE?
A: No, that is not true. If the LIKE pattern starts with a character string (such as LIKE 'J*f'), the optimizer can use index scan-key positioning to limit the number of rows (records) that need to be scanned. In my example, the optimizer would use an index to find rows with values that begin with J.
If the LIKE pattern starts with % or _, index scan-key positioning cannot be used. However, even in this case, indices can be used for index scan-key selection, in which the entire index (not the table) is scanned and selection is performed on it. With the results of the index scan, random I/O is performed to the table.
Just do it!
Got a tip that makes you a star?
Send it to