开发者

How to Speed Up Search Suggestions

I want to make a search suggestion like Google that's fast. I tried it with AJAX/jQuery, but its speed was very slow. I also tried with XML instead of MySQL, but it was slow too. How can I speed it up? my jquery code is:

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

query : 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.';
        }
    }
  开发者_如何学Go  } 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.

However, looking at your code, I would say that most likely, it is the server-side code, specifically the WHERE x LIKE %y% clause. 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. 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

  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

  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. Find out where your search is slow and fix that. Install firebug and see how long your server takes to respond. 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. I did myself an implementation of a kart-trie. You can grab it at phpclasses.org and search for kart-trie.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜