1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

AJAX autocompletion - Should I fetch data from database for every keystroke?

Discussion in 'JavaScript' started by ready2work, Sep 17, 2008.

  1. #1
    I am trying to use ajax autocompletion for a textbox. Should I fetch data from the database for every keystroke? If I do so, it is getting delayed. What is the technique to make it fast loading?
     
    If someone posts a solution, use the "Best Answer" link in their post to pick it as the best answer.
    ready2work, Sep 17, 2008 IP
  2. artur81_skce

    artur81_skce Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    it could be from every keystroke, but don't call function when user put less than 2-3 sigins.
    How big is your table with words?
     
  3. ready2work

    ready2work Guest

    Best Answers:
    0
    #3
    The table has around 10000 records. I am trying to use autocomplete to get the Names.

    Although I am not calling the function when the characters are less than 3, should I call the function to query the database for every keystroke after that? Will that not delay the result?

    Is there any need/way to pre-fetch the records or any way to make it much faster after 3 characters are keyed in?
     
    ready2work, Sep 17, 2008 Set Best Answer IP
  4. ready2work

    ready2work Guest

    Best Answers:
    0
    #4
    No one here working on AJAX? Thought someone might give atleast some useful links. :(
     
    ready2work, Sep 22, 2008 Set Best Answer IP
  5. sslshopper

    sslshopper Member

    Messages:
    58
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    48
    #5
    It all depends on how much traffic you have. checking it on every keystroke could be a huge resource drain if you have a lot of people using it at once. If you only have a few then it is not so bad. Several sites do it. check out this ExtJS example: http://extjs.com/deploy/dev/examples/form/forum-search.html
     
    sslshopper, Sep 22, 2008 Set Best Answer IP
  6. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #6
    edit: the ext example above uses pre-fetched JSON data: _http://extjs.com/forum/topics-remote.php so it does not actually run any ajax as you type, it already has all the answers availabel to it. something that is not practical with 10k records :(

    i dont think it will be a problem to use ajax, however - if you index your database correctly and earch for 3+ chars, queries ought to be very quick indeed

    do some filtering on keydown, like alpha-numeric only, cache results as some array/object so that if they press backspace, it does not go to ajax lookup again but checks the local array for the results. from that pov, make it so the results from php are json or something similar that you can use as your base data and feed it to a function that does the dropdown by iterating the data. and yes, don't rely on SEmarketing forums for serious help, try jquery/mootools/ext/yui mail lists instead :)

    also, to find the cause of the delay, you need to first make sure that the mysql you have is quick enough on its own (time the queries).
    then, make sure your XHR method is GET and not POST (huge difference performance wise and in overhead). finally: get firefox and install firebug plugin, then enable it for the domain, open it and set it to log XHR requests, then monitor it as you are typing keywords to see the response speeds. anything over 50ms is probably not going to be good enough...

    another thing: fast typers. you are better off waiting for 2 seconds before a lookup - no point in doing a lookup as they type 'mich' and then again as they type 'micha' 100ms later, so use something like varname = setTimeout(keylookup, 2000) and clear the varname and timeout on keypress, then reset it. i hope this helps you somewhat =>
     
  7. ready2work

    ready2work Guest

    Best Answers:
    0
    #7
    Thanks for the info guyz. Will work it out.
     
    ready2work, Sep 22, 2008 Set Best Answer IP