jQuery用户界面 - 加快自动完成 [英] jQuery UI - speeding up Autocomplete

查看:102
本文介绍了jQuery用户界面 - 加快自动完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经有了自动完成工作正常,但我试图找出办法让它更快。这里是我的HTML:

 国家:LT;选择名称='国家'ID ='国家'的onchange =renewAutocomplete();>
             <期权价值='美国'>美国< /选项>
             <期权价值='UK'>英国< /选项>
         < /选择>< BR />
城市:其中;输入类型=文本名称=城市ID =城市/>
<脚本>
   功能renewAutocomplete(){
      $(#city).autocomplete({
            来源:searchCities /+ $('#国)VAL()。
            的minLength:2
        });
   }
< / SCRIPT>

在PHP / MySQL的查询是pretty基本的:

  $ =一词$ _GET ['术语'];
$的SQL =选择从市城市里country_ code ='{$国家}和类似城市的'{$}一词%';

当然,这意味着每一个按键一个新的查询。它有助于有点添加一些缓存,但它仍然是没有那么快,因为我认为它应该是。

问题1
我首先想到的是在选择国家时,让所有的城市在一个DB调用。是否有全国各城市返回的JSON指定到变量,自动完成可以使用的方法吗?我最初的测试将只是弹出所有的城市作为您键入:

  $获得(searchCities /+ $('#国)。VAL()函数(数据){
                    $(#city).autocomplete({
                        来源:资料,
                        的minLength:2
                    });
        });
PHP:
    $的SQL =选择从市城市里country_ code ='{$}国家';

问题2
由于城市和国家不会发生很大的变化(通常情况下),这将是更快,只是与所有值的平面文件?

或者是有任何其他方式做出更快的自动完成?

感谢。


解决方案

  1. 自动完成假设,如果你回吧,这是一个有效的答案。所以:


  2. 是的,从平面文件拉动会比打的每次击键您的分贝快了很多。但是,如果你把在DB的前面部分缓存(内存缓存),它很可能是一样快的平面文件。


不过说真的,因为你(可能)搜索您的任期(相对)小套,这将是非常快反正。

从比例的角度来看,他们坚持在一个平面文件或使用缓存。

作为一个方面说明:考虑函数mysql_escape_string()使用$国家和$任期。否则,你打开 SQL注入攻击。

I've got autocomplete working fine, but I'm trying to figure out way to make it faster. Here's my HTML:

Country: <select name='country' id='country' onchange="renewAutocomplete();">
             <option value='US'>USA</option>
             <option value='UK'>United Kingdom</option>
         </select><br/>
City: <input type='text' name='city' id='city' />
<script>
   function renewAutocomplete() {
      $( "#city" ).autocomplete({
            source: "searchCities/" + $('#country').val(),
            minLength: 2
        });
   }
</script>

The PHP/mySQL query is pretty basic:

$term = $_GET['term'];
$sql = "SELECT city FROM cities WHERE country_code = '{$country}' AND city LIKE '{$term}%'";

OF course, this means a new query for every keystroke. It helps a bit to add some caching, but it still isn't as fast as I think it should be.

Question 1 My first thought was to get ALL the cities in one db call when the Country is selected. Is there a way to assign the returned JSON of all the cities of country into a variable that autocomplete can use? My initial tests would just popup all the cities as you typed:

      $.get("searchCities/" + $('#country').val(), function(data) {
                    $( "#city" ).autocomplete({
                        source: data,
                        minLength: 2
                    });
        });
PHP: 
    $sql = "SELECT city FROM cities WHERE country_code = '{$country}'";

Question 2 Since cities and countries don't change a lot (usually), would it be faster to just have a flat file with all the values?

Or is there any other way to make a faster autocomplete?

Thanks.

解决方案

  1. Autocomplete assumes that if you return it, it's a valid answer. So: no.

  2. Yes, pulling from a flat file would be a lot faster than hitting your db for every key stroke. However, if you threw some caching (memcache) in front of the db, it would probably be just as fast as a flat file.

But really, because you're (likely) searching a (relatively) small set for your term, it's going to be really fast anyway.

From a scaling perspective, stick them in a flat file or use caching.

As a side note: look into mysql_escape_string() for using $country and $term. Otherwise you're open to SQL Injection attacks.

这篇关于jQuery用户界面 - 加快自动完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆