jQuery UI 自动完成功能到 MySQL 与本地存储?
有兴趣使用 jQuery UI 自动完成功能连接大型 MySQL 事件数据库。这会显着减慢每个页面的速度吗?(搜索栏将出现在所有页面上)
我是否应该将查询结果存储到本地存储并将脚本放在页面底部?或者SESSION或者Cookie?
这是没有任何缓存功能的代码。
<?php
require_once("../../connect.php");
$day_events = "SELECT * FROM tbl_events";
$events_result = mysql_query($day_events);
?>
<script>
$(function() {
var availableTags = [
<?php
while($event_row=mysql_fetch_array($events_result)) {
echo "\"".$event_row['event']."\",\n";
}
?>
];
$( "#tags" ).autocomplete({
source: availableTags
});
});
</script>
**编辑:澄清我的问题是使用自动完成功能会显着减慢页面速度吗?如果可以的话可以用什么方法来改善呢?
Interested in using the jQuery UI autocomplete feature to connect with a large MySQL database of events. Will this slow down each page significantly?(Search bar will be on all pages)
Should I store the results from the Query to Local Storage put the script at the bottom of the page? Or SESSION or Cookie?
Here is the code without any cache features.
<?php
require_once("../../connect.php");
$day_events = "SELECT * FROM tbl_events";
$events_result = mysql_query($day_events);
?>
<script>
$(function() {
var availableTags = [
<?php
while($event_row=mysql_fetch_array($events_result)) {
echo "\"".$event_row['event']."\",\n";
}
?>
];
$( "#tags" ).autocomplete({
source: availableTags
});
});
</script>
**EDIT: To clarify my question is will using the autocomplete slow down pages significantly? If so what methods can be used to improve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您最好使用 AJAX 来检索包含用户键入的字符串的事件,这将过滤您的结果集,并且意味着您的页面也不会花费很长时间来加载。
Your better off using AJAX to retrieve events containing the string the user types, that will filter down your result set and means your page won't take ages to load either.
根据我的经验,本地和远程自动完成都相当快。我根据将要加载到源中的数据量以及访问数据的概率来使用其中之一。如果您的自动完成功能将搜索大型数据集,则最好使用远程选项(http://jqueryui.com/demos/autocomplete/#remote)。这可能会增加轻微的延迟,具体取决于 mySQL/Web 服务器返回结果的时间以及通过连接发送的数据量。
另一种选择是将源数据保存在外部 JavaScript 文件中,以便浏览器只需要下载一次(它应该自动缓存它)。同样,这取决于有多少数据。如果有相当多的数据,那么使用远程/AJAX 路线可能更有意义。
尽管缓存结果是节省数据库命中的好主意,但不需要将结果放入会话或 cookie 中。自动完成组件有一个缓存选项(http://jqueryui.com/demos/autocomplete/#remote-with-cache),但还有其他方法可以缓存 ajax 请求和 javascript,以便为所有访问者缓存。
In my experience, both local and remote autocomplete has been reasonably fast. I use one or the other depending on how much data is going to be loaded into the source, and what the probability is that the data will be accessed. If there is a large dataset your autocomplete will be searching against, it may be best to use the remote option (http://jqueryui.com/demos/autocomplete/#remote). That may add a slight delay depending on how long the results are returned by mySQL/web server and how much data is sent over the connection.
Another option is to keep the source data in an external javascript file so that the browser only needs to download it once (it should automatically cache it). Again, this depends on how much data there is. If there's quite a lot of data, it may make more sense to go with the remote/AJAX route.
Placing the results in a session or cookie shouldn't be needed, although caching the results is a good idea to save on database hits. The autocomplete component has a caching option (http://jqueryui.com/demos/autocomplete/#remote-with-cache), but there are other ways to cache ajax requests and javascript so that it's cached for all visitors.