2017-08-02 08:34
浏览 75

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.


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


$(function () {
        source: 'getMedicineNames.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);

图片转代码服务由CSDN问答提供 功能建议

此代码工作正常但问题是它很慢,我认为这是因为它每次都连接到数据库 我打了一封信。 有没有办法让它更快? 我必须假设db表并不总是相同,所以我不能只是将结果添加到var并完成它。 提前谢谢你。



 &lt; label for =“hint”&gt; Meds:&lt; / label&gt; 
&lt; input id =“ 提示“&gt; 



< strong> 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:


    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.

    点赞 打赏 评论

相关推荐 更多相似问题