jQuery UI - 加速自动完成
我的自动完成工作正常,但我正在尝试找出使其更快的方法。这是我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
自动完成假设如果您返回它,它就是一个有效的答案。所以:不。
是的,从平面文件中提取数据比每次击键都敲击数据库要快得多。但是,如果您在数据库前面放置一些缓存(memcache),它可能会和平面文件一样快。
但实际上,因为您(可能)正在为您的术语搜索(相对)较小的集合,所以无论如何它都会非常快。
从扩展的角度来看,将它们粘贴在平面文件中或使用缓存。
作为旁注:查看 mysql_escape_string() 以使用 $country 和 $term。否则,您将面临 SQL 注入攻击。
Autocomplete assumes that if you return it, it's a valid answer. So: no.
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.