Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql compare using left(X,2)="AB" or X like "AB%" for speed?

Tags:

mysql

Which will perform better when searching for a key with a specific prefix in MySQL? ;-

i) where left(X,2)="AB"

or

ii) where X like "AB%"

like image 395
jammie podger Avatar asked Mar 17 '10 10:03

jammie podger


1 Answers

Assuming you have an index on the column, the second is faster. The database can use an index when you use LIKE but it cannot when you use LEFT. In technical terms, LIKE is sargable but LEFT is not. You can find more information here.

like image 197
Mark Byers Avatar answered Sep 27 '22 18:09

Mark Byers