I am implementing an autocomplete field in a website, and my endpoint queries a mysql to get the data. This particular field can only be retrieved from a database ‘list’ which has a about 600,000 entries, where only 4000 of them are unique. Once a day or so, a new unique list item will be added.
I’m using a call like this, where query is whatever the user has started typing in the autocomplete box:
SELECT DISTINCT item FROM list WHERE item like '%query%' LIMIT 10;
This query takes about .8 milliseconds, and the majority of the time is due to the SELECT DISTINCT, I believe. Is there any way to increase the performance of this task?
I’m fine to create new tables/views, but I’m not sure what will help.
When you use the % sign at the beginning in a query with LIKE no index will be used, and every single row of your table will be scanned, no matter how your indexes look like.
Think about an address book with letter tabs, if I’m looking for
'Mads%', I can point my finger to the letterMand read from there, but If I look for'%dse%', I’ve to read every entry.itemwill be used (be sure to add it, you can also index a part of the field in case it’s a long field).