Performace Question regarding loading from DB for Ajax Auto-complete
deepti bellubbi
Greenhorn
Joined: May 06, 2008
Posts: 27
posted
0
Hi
I need to show a search page with one textfield which shows the User name. Instead of making him type the entire thing,I want to get the username from the database. The total number of records will be around 2000-3000.
I was thinking of having AJAX autocomplete feature here so that the user doesn't have to type in the entire username and hence avoids spelling mistakes,
also it would look cool :-)
Question is, is it a good idea to implement this..performance wise?
I was checking the examples of Ajax autocomplete and understand that at first we need to get the entire data on server side and then as the user starts
typing the username, we can pass this value to a method which will iterate over the dataset and find matches.
The speed is really based on how long it takes for a round trip to the server, how long it takes to look up the results, and how long to display it.
The part that will be the biggest problem will be your serverside code.
2000-3000 records is not a lot, I have autocompletes that handle about 100X more records.
Eric
deepti bellubbi
Greenhorn
Joined: May 06, 2008
Posts: 27
posted
0
Thanks Eric.
Could you please point me to some examples where the data comes from database. I tried to find some myself however fins most examples with local arrays.
I am trying to implement this using JSP.
What I have in mind is.. fetch all usernames from the db , it will be cached. Then every time the the user enters something in the username textfield..I will invoke another method which will iterate over the cached result and find usernames that start with those alphabets (simple string operations).
I can periodically refresh the cache..say every 3-4 hours or so.
Do you think its a good approach or would you suggest some changes?
deepti bellubbi wrote:Could you please point me to some examples where the data comes from database. I tried to find some myself however fins most examples with local arrays.
The only difference is how that array is populated--if you already know how to get the data from the database you're already done--what do you need an example of?
deepti bellubbi wrote:What I have in mind is.. fetch all usernames from the db , it will be cached. Then every time the the user enters something in the username textfield..I will invoke another method which will iterate over the cached result and find usernames that start with those alphabets (simple string operations).
I can periodically refresh the cache..say every 3-4 hours or so.
If you are writing your own implementation for learning purposes, consider the data structure. A map would let you segment the list so you don't have to loop through the whole thing each time.
If you are writing your own implementation for learning purposes, consider the data structure. A map would let you segment the list so you don't have to loop through the whole thing each time.
Could you please elaborate, I did not quite understand that.
You can either go through the entire list looking for "startsWith", or you can do something to improve performance. Sorting might help. Creating a map could help segment the results (key "a" is something w/ all users that start with "a"--could be another map following the same pattern, could be a list, etc.)
If you are writing your own implementation for learning purposes, consider the data structure. A map would let you segment the list so you don't have to loop through the whole thing each time.
Could you please elaborate, I did not quite understand that.
Thanks
As noted above, there is existing code that does auto-complete. If you are working on a "real" project, you should use one of those; not write your own. If you are just doing it to learn, it makes sense to write your own.
A map or sorted list is faster than just a list because you don't have to loop through the whole list to find your data. For a map, you only have to look through the data beginning with the relevant letter. In a list, you just have to do a binary search. Better is a map of sorted lists.