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.

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

报告相同问题?

悬赏问题

  • ¥100 c语言,请帮蒟蒻看一个题
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)