duanjiu4498 2017-08-02 08:34
浏览 113
已采纳

JQuery UI自动完成很慢

This code works fine but the problem is that it is slow, I assume it is because it connects to the database everytime I type a letter. Is there a way to make it faster? I have to assume that the db table will not always be the same so I can't just add the results to a var and be done with it. Thank you in advance.

Also if you believe this api is not good, I can accept alternatives.

HMTL:

<label for="hint">Meds: </label>
<input id="hint">

JQuery:

$(function () {
    $("#hint").autocomplete({
        source: 'getMedicineNames.php'
    });
});

PHP:

require 'connect.inc.php';
$mysql = mysqli_connect("$db_host", "$db_username", "$db_pass", "$db_name");
$name = isset($_GET['term']) ? $_GET['term'] : "";

if ($name!="") {
    $sql = "SELECT MedicineName FROM medicinetypes WHERE MedicineName LIKE '%$name%'";

    $res = mysqli_query($mysql, $sql);
    $res = mysqli_fetch_all($res, MYSQLI_ASSOC);

    foreach ($res as $row) {
        $data[] = $row['MedicineName'];
    }
    echo json_encode($data);
}
  • 写回答

1条回答 默认 最新

  • douji9184 2017-08-02 08:58
    关注

    You can investigate in 3 domains:

    Caching

    You can implement simple caching systems.

    For example, caching all the results of each starting letters in 26 different files or whatever. Find the best repartition for your case. You have many choices with that approach

    Database optimisation

    Does your table have Indexes? If not you WANT to delve into that!

    Most of the databases provide some example code for your use case.

    Search Engines

    When you have to deal with big datasets, you need to consider using a search engine system (like ElasticSearch). They usually have built-in modules for autocompleting and are very performant.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘