jQuery UI - 加速自动完成

发布于 2024-11-03 14:14:58 字数 1366 浏览 0 评论 0原文

我的自动完成工作正常,但我正在尝试找出使其更快的方法。这是我的 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>

PHP/mySQL 查询非常基本:

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

当然,这意味着每次击键都会产生一个新查询。添加一些缓存会有所帮助,但它仍然没有我想象的那么快。

问题1 我的第一个想法是在选择国家/地区时在一次数据库调用中获取所有城市。有没有办法将返回的国家/地区所有城市的 JSON 分配给自动完成可以使用的变量?我的初始测试只会在您键入时弹出所有城市:

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

问题 2 由于城市和国家/地区不会发生很大变化(通常),因此仅拥有包含所有值的平面文件会更快吗?

或者有没有其他方法可以更快地自动完成?

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ˉ厌 2024-11-10 14:14:58
  1. 自动完成假设如果您返回它,它就是一个有效的答案。所以:

  2. 是的,从平面文件中提取数据比每次击键都敲击数据库要快得多。但是,如果您在数据库前面放置一些缓存(memcache),它可能会和平面文件一样快。

但实际上,因为您(可能)正在为您的术语搜索(相对)较小的集合,所以无论如何它都会非常快。

从扩展的角度来看,将它们粘贴在平面文件中或使用缓存。

作为旁注:查看 mysql_escape_string() 以使用 $country 和 $term。否则,您将面临 SQL 注入攻击

  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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文