File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Performace Question regarding loading from DB for Ajax Auto-complete

 
deepti bellubbi
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Appreciate your inputs
Thanks
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say do not recreate the wheel, use something like http://jqueryui.com/demos/autocomplete/ or http://developer.yahoo.com/yui/autocomplete/

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
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64205
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hopr that does not mean that you are trying to do DB access in a JSP!
 
deepti bellubbi
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No I don't intend to do db access in 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?

Thanks

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33723
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
deepti bellubbi
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33723
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
deepti bellubbi wrote:

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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic