Fast string binding to MySQL query
I have a MySQL database (InnoDB) with a table users
with a column username
. On the Ruby on Rails website, I have a feature that allows users to search for someone by username. When typing on the website, it prompts who the user is looking for (typeahead w / bootstrap). I am currently using a query in MySQL,SELECT `username` FROM users WHERE `username` LIKE 'the_query%' LIMIT 15;
The problem that exists is speed. The table has approximately 500,000 rows, and such a query seems to take about a second, which is too slow when you make guesses on the fly. What can I do to improve performance? Anything less than 100ms would be appropriate. Perhaps there is a better way to do this than using MySQL to handle the search?
source to share
Try to limit the number of matches you get. If it is a search bar, you cannot display thousands of entries in the search list. But from the above query, the server is sending all the results to the frontend as I'm guessing. And then you can filter the entries from that. Also, try this query.
Limit the number of entries to 10 ...
SELECT username
FROM users WHERE username
LIKE 'the_query%' LIMIT 10;
source to share