简体   繁体   English

如何加快搜索建议

[英]How to Speed Up Search Suggestions

I want to make a search suggestion like Google that's fast. 我想提出像Google这样的快速搜索建议。 I tried it with AJAX/jQuery, but its speed was very slow. 我用AJAX / jQuery尝试过,但是速度非常慢。 I also tried with XML instead of MySQL, but it was slow too. 我也尝试使用XML而不是MySQL,但是它也很慢。 How can I speed it up? 我如何加快速度? my jquery code is: 我的jQuery代码是:

function lookup(inputString,fname,sbox,asbox) {
    if(inputString.length == 0) {
        // Hide the suggestion box.
        $('#'+sbox).hide();
    } else {
        // post data to our php processing page and if there is a return greater than zero
        // show the suggestions box
        $.post(fname, {mysearchString: ""+inputString+""}, function(data){
            if(data.length >0) {
                $('#'+sbox).show();
                $('#'+asbox).html(data);
            }
        });
    }
}

and php code is 和PHP代码是

query : SELECT maincatid,category FROM maincategory WHERE category LIKE '%$mysearchString%' LIMIT 10" 查询: SELECT maincatid,category FROM maincategory WHERE category LIKE '%$mysearchString%' LIMIT 10"

        if($rs) {
            while ($result = @mysql_fetch_object($rs)) 
            {
                echo '<li onClick="fill(\''.$result->maincatid.'\',\''.$result->category.'\');">'.str_ireplace($mysearchString,'<span style="background-color:#C1E0FF;">'.$mysearchString.'</span>',$result->category).'</li>';
                $c++;
            }
        } 
        else {
            echo 'ERROR: There was a problem with the query.';
        }
    }
    } else {
    } 
} else {
    echo 'Access denied.';
}

I would suggest that you profile to see where your greatest time is spent. 我建议您进行配置,以了解您在哪里花费了最多的时间。 I like to watch packets go from Wireshark to the their destination, although that seems like overkill when there are plugins for Firefox that will trace your requests. 我喜欢看从Wireshark到目的地的数据包,尽管当有Firefox插件可以跟踪您的请求时,这似乎有些过分。

However, looking at your code, I would say that most likely, it is the server-side code, specifically the WHERE x LIKE %y% clause. 但是,查看您的代码,我想最有可能的是服务器端代码,特别是WHERE x LIKE%y%子句。 Most DBs don't do as well with a left wildcard because they use a b-tree for their indices, so I would get rid of that first wildcard. 大多数DB在使用左通配符时效果不佳,因为它们使用b树作为索引,因此我将摆脱第一个通配符。 Beyond that, pre-compile a list of suggestions based on past misspellings and their corrections. 除此之外,根据过去的拼写错误和更正内容,预先编译建议列表。 If you did this and properly indexed your table, your requests could be quite snappy. 如果执行此操作并正确索引了表,则您的请求可能会非常快。

There are lots of reasons google's search results will be faster than yours. 谷歌的搜索结果会比您的搜索结果更快的原因很多。

Off the top of my head, 从我头顶上

  1. They would have some sort of optimized data warehouse with indexes and full text search instead of the free version of MySQL 他们将拥有某种具有索引和全文搜索的优化数据仓库,而不是MySQL的免费版本

  2. They don't have just one server that serves their request, they'd have a CDN and server clusters and you'd hit the server nearest you for low latency 他们没有一台服务器来满足他们的请求,它们有一个CDN和服务器集群,而您会碰到离您最近的服务器以降低延迟

  3. They'd have the current trending queries cached so they wouldn't have to look it up everytime you hit it. 他们会缓存当前的趋势查询,因此不必在每次点击时都进行查询。

  4. They'd be running on their own dedicated servers that do nothing but serve your search results every day instead of a shared server with a bunch of other people on it 他们将在自己的专用服务器上运行,这些服务器什么也不做,但每天都会提供搜索结果,而不是上面有很多人的共享服务器

Forget about trying to match google. 忘记尝试匹配Google了。 Find out where your search is slow and fix that. 找出搜索缓慢的地方并解决。 Install firebug and see how long your server takes to respond. 安装firebug,然后查看服务器响应需要多长时间。 Profile your code to see what parts are slow. 分析您的代码以查看哪些部分运行缓慢。 Most of the time it's badly designed tables and slow servers that are the cause 多数情况下,表设计不良和服务器运行缓慢是造成这种情况的原因

Most likely you want to use an elite data structure like a binary tree or a trie, or a radix-tree, or patricia-tree or crit-bit tree. 您最有可能想要使用精英数据结构,例如二叉树或trie,基数树,patricia树或crit-bit树。 I did myself an implementation of a kart-trie. 我自己做了一个卡丁车尝试。 You can grab it at phpclasses.org and search for kart-trie. 您可以在phpclasses.org上获取它并搜索kart-trie。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM