I am developing my first Flask application (with sqlite as a database). It takes a single name from user as a query, and displays information about this name as a response.
All working well, but I want to implement typeahead.js to make better user experience. Typeahead.js sends requests to server as user types, and suggests possible names in dropdown. Right now I'm searching the database with select * from table_name where name like 'QUERY%'. But this of course is not so fast as I would like it to be - it works, but with noticable input lag (less or around a second I suppose).
In order to speed up things I looked at some memory caching options (like Redis or memcached), but they are key-value store, therefore I think do not fit my needs. I think possible option would be to make list of names (["Jane", "John", "Jack"], around 200k names total), load it into ram and do searches there. But how do I load something in memory in Flask?
Anyway, my question is: What is the best way to make such search (by first few letters) faster (in Python/Flask)?
You are looking for "partial matches". I would load all possible names into an array, and sort them. Then I would separately create a (26x26) lookup array that shows the index of the first element in the list of names that corresponds to a combination of the first two letters; you might also have a dict (rather than an exhaustive list) of all possible three letter combinations, which would speed up your search (because it limits it to a much smaller slice of the array).
In other words - you would not really be searching at all (for the two and three letter combo's); you would be returning a slice of the array. Once you have a match of more than three, you probably can search the slice (not worth creating tables beyond three characters).
Fastest text search in Python