简体   繁体   English

键入和搜索,如何提高性能?

[英]Type and search, how to improve the performance?

I want to implement somethings like searching on wiki, when I want to search "apple" , I type "a" it shows the words start from "a"... I know that it can implement when I type, I submit a SQL query to search the article start from "a", but when more and more query request, it become slow... ... Is there any performance turning technique on doing this kind of things? 我想实现类似在Wiki上搜索的功能,当我要搜索“ apple”时,我键入“ a”,它显示的单词是从“ a”开始...我知道它可以在键入时实现,我提交了一个SQL查询从“ a”开始搜索文章,但是当查询请求越来越多时,它变得很慢……在做这种事情上是否有任何性能转换技术? Thank you. 谢谢。

On the server-side you should cache the results, ideally in memory (eg: Memcached). 在服务器端,您应该将结果缓存,最好是缓存在内存中(例如:Memcached)。 So if 10 people hit "a" it will be only one query to the database and 9 super fast data access from the memory. 因此,如果有10个人按“ a”,则只会对数据库进行一次查询,而从内存中进行9次超快速数据访问。

As for the bandwidth optimization, you should send your data in JSON, or alternatively some custom data format. 至于带宽优化,您应该以JSON或其他自定义数据格式发送数据。 See this awesome article: Building Fast Client-side Searches 看到这篇很棒的文章: 建立快速的客户端搜索

Not sure what version of SQL you are using, but a few assumptions: 不确定您使用的是哪个版本的SQL,但有一些假设:

  • You have an index on the field you are searching 您正在搜索的字段上有一个索引
  • You are only returning the needed field (like title), not SELECT * 您只返回所需的字段(如标题),而不是SELECT *

What I would recommend considering is reducing the number of rows returned depending on the size of the search query, something like: (pseudo-code, don't know your SQL dialect) 我建议考虑的是根据搜索查询的大小减少返回的行数,例如:(伪代码,不知道您的SQL方言)

if len(searchString)<=3
   select top 50 title from table order by title
else
   select title from table order by title

The smaller searches are likely to return many more rows, but most users are likely to type in several letters before then stop typing, so for the initial queries, don't return all of the rows. 较小的搜索可能返回更多的行,但是大多数用户可能在停止输入之前先键入几个字母,因此对于初始查询,请不要返回所有行。

Not a technical answer an maybe obvious, but for client side I think you can launch your search event not on the key press but after X millisecond after the key press and with the condition that nothing is changed. 不是一个技术上的答案可能很明显,但是对于客户端,我认为您可以在按键后X毫秒后且不改变任何条件的情况下,不在按键上启动搜索事件。

So if I want search a "word" and i type "w" "o" "r" "d" in a speedy way you only make une request and obviously you can optimize that request SQL way 因此,如果我想搜索一个“单词”并且以快速的方式键入“ w”,“ o”,“ r”,“ d”,则只发出une请求,显然可以优化该请求的SQL方式。

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

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