Meaningless Drivel is fun!*
The moose likes HTML, CSS and JavaScript and the fly likes Performace Question regarding loading from DB for Ajax Auto-complete Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Performace Question regarding loading from DB for Ajax Auto-complete" Watch "Performace Question regarding loading from DB for Ajax Auto-complete" New topic
Author

Performace Question regarding loading from DB for Ajax Auto-complete

deepti bellubbi
Greenhorn

Joined: May 06, 2008
Posts: 27
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

Joined: Nov 08, 2001
Posts: 15376
    
    6
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

Joined: May 06, 2008
Posts: 27
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

Joined: Jan 10, 2002
Posts: 60780
    
  65

I hopr that does not mean that you are trying to do DB access in a JSP!


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
deepti bellubbi
Greenhorn

Joined: May 06, 2008
Posts: 27
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

Joined: Sep 29, 2008
Posts: 12617

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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30116
    
150

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
deepti bellubbi
Greenhorn

Joined: May 06, 2008
Posts: 27

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

Joined: Sep 29, 2008
Posts: 12617

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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30116
    
150

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.
 
 
subject: Performace Question regarding loading from DB for Ajax Auto-complete
 
Similar Threads
editable dropdown in struts
Type Ahead in Drop Down
I need something to start with in using AJAX
show /hide data based on autocomplete results.
how to use autocomplete in jsf