根据 SQL 数据库自动完成名称
我的网络应用程序中有一个文本字段,我想在其中进行自动完成(例如,用户输入“St”,我可以建议“Steve”)。我要匹配的名称位于 SQL 数据库用户表中。我的问题是,如何才能以扩展到大量用户的方式实现这一点?
有 DB 全文搜索或 Lucene 之类的东西。这是否适合像这样的“开头为”查询?
有没有办法为“开头为”类型搜索设置普通数据库索引?
有没有我完全缺少任何其他想法吗?
任何帮助将不胜感激。谢谢。
I have a text field in my web app where I want to do auto-completion (e.g. the user types "St" and I can suggest "Steve"). The names I'm matching against are in a SQL database table of users. My question is, how can I make this happen in a way that will scale to massive amounts of users?
There's DB full text search or something like Lucene. Would that even be appropriate for a "starts with" query like this?
Is there a way to set up a normal DB index for "starts with" type searches?
Any other ideas that I'm totally missing?
Any help would be appreciated. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只要您在名称列上有索引,这些就应该可以完成工作。
SQL Server:
MySQL(根据 Bart J):
Oracle:
These should do the job as long as you have an index on the name column.
SQL Server:
MySQL (according to Bart J):
Oracle:
如果您在想要自动完成的字段上有有序索引,那么它可以在“开头为”样式查询中使用。
If you have an ordered index on the field you want to autocomplete then it can be used in a "starts with" style query.
大多数升序索引将用于 LIKE 'xxx%' 类型查询的优化。为了提高性能,我建议您对尝试从数据库返回的结果数量设置限制:
您想要执行的滚动量是有限制的。
Most ascending indexes will be used for optimization with a LIKE 'xxx%'-type query. For performance, I would recommend you set a limit to the number of results you try to return from the database:
There's a limit to the amount of scrolling you want to do.
不要用 SQL 来做吗...?
发送列表并在浏览器中自动完成。这就是我们所做的。
这样,我们就避免了类似香港客户的情况 ->瑞士网络服务器 ->瑞士 SQL 服务器每次用户按键往返。我们只进行一次加载页面,然后一切都在客户端。
我们最大的数据集中有大约 9,000 个项目可以驱动自动建议控制(大多数都少得多)
评论后编辑:
就像我说的,我们的大多数列表都少得多。然而,即使加载额外的 200k,也比每次按键时往返数据库要好。
事实上,您可以往返意味着“内部网”,因此页面加载时间也无关紧要。无论如何,与往返延迟相比,200k 根本不算什么……
Don't do it in SQL...?
Send the list and autocomplete in the browser. This is what we do.
This way, we avoid something like a Hong Kong client -> Switzerland web server -> Switzerland SQL server round-trip per user key press. We do one trip to load the page, then it's all client side.
We have something like 9,000 items in our biggest data set that drives Auto Suggest control (most are far less)
Edit, after comment:
Like I said, most of our lists are far less. However, even loading an extra 200k is better then round-tripping to a database every key press.
The fact that you can round trip implies "intranet", so page load time is also irrelevant. 200k is nothing anyway compared to round trip latency...