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?
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?
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?
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
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 =>