mysql - jQuery 自动完成 : pre-cache query to increase performance?

标签 mysql json jquery-ui

我有一个 jQuery 自动完成字段,当用户开始在该字段中输入城市名称时,它会查找城市名称和相应的 ZIP(美国邮政编码)。

MySQL 数据库包含大约 44,000 行美国邮政编码。选择所有城市名称和邮政编码时,查询本身运行速度很快(0.0002 秒)。

“预缓存”所有结果是否有任何好处 (SELECT place_name, postal_code FROM postal_codes WHERE country_code = 'US') 以便用户更快地加载自动完成功能?

编辑:整个 JSON 列表为 600KB,并在浏览器中加载 2-3 秒(根据 Google Chrome 控制台)

当前代码:

<script type="text/javascript">
$(function() {

$('#from_country').change(function(){
  var val = this.value;
  $("#from_zip").autocomplete({
    source: "json-list-live-production.php?country=" + val,
    minLength: 4,
  });
}).change()


$('#del_country').change(function(){
  var val = this.value;
  $("#del_zip").autocomplete({
    source: "json-list-live-production.php?country=" + val,
    minLength: 4,
  });
}).change()


});
</script>

<input type="text" class="input_text" name="from_zip" id="from_zip"  />

<input type="text" name="del_zip" id="del_zip"  />

最佳答案

看起来使用 GeoNames.org 网络服务 + jQuery 自动完成查找邮政编码是最快的方法。

$( "#from_zip" ).autocomplete({
  source: function( request, response ) {
    $.ajax({
      url: "http://ws.geonames.org/postalCodeSearchJSON",
      dataType: "jsonp",
      data: {
        featureClass: "P",
        style: "full",
        maxRows: 50,
        placename_startsWith: request.term,
        country: "US"
      },
      success: function( data ) {
        response( $.map( data.postalCodes, function( item ) {
          return {
            label: item.placeName + (item.adminCode1 ? ", " + item.adminCode1 : "") + " (" + item.postalCode + ")",
            value: item.postalCode
          }
        }));
      }
    });
  },
  minLength: 2,
    messages: {
        noResults: null,
        results: function() {}
    },

});

关于mysql - jQuery 自动完成 : pre-cache query to increase performance?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15791518/

相关文章:

php - Doctrine2 Symfony2 innerJoin QueryException 预期 Doctrine\ORM\Query\Lexer::T_WITH,得到 'ON'

.net - 在 nhibernate-mapping 中设置默认值

json - Grails中将JSON作为渲染标准的问题!在一个 Restful 服务中找到一个字段并进行渲染

javascript - jQuery 可排序图像可放置在单独的容器上

PHP - mysqli_fetch_assoc,2 个结果然后放入数组中

MySQL 从 PHP 查询时出错 : IF EXISTS

javascript - 将字符串格式化为 JSON 对象以在 HTML 中显示在带有/[(ngModel)] 的文本区域中

jQuery/JSON 错误 "SyntaxError: JSON.parse: unexpected character"

jquery - 开源 jquery 自动完成标记

javascript - jQuery UI 如何将损坏的图像链接指向正确的文件?