根据 SQL 数据库自动完成名称

发布于 2024-08-07 03:56:48 字数 281 浏览 5 评论 0原文

我的网络应用程序中有一个文本字段,我想在其中进行自动完成(例如,用户输入“St”,我可以建议“Steve”)。我要匹配的名称位于 SQL 数据库用户表中。我的问题是,如何才能以扩展到大量用户的方式实现这一点?

  1. 有 DB 全文搜索或 Lucene 之类的东西。这是否适合像这样的“开头为”查询?

  2. 有没有办法为“开头为”类型搜索设置普通数据库索引?

    有没有
  3. 我完全缺少任何其他想法吗?

任何帮助将不胜感激。谢谢。

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?

  1. There's DB full text search or something like Lucene. Would that even be appropriate for a "starts with" query like this?

  2. Is there a way to set up a normal DB index for "starts with" type searches?

  3. Any other ideas that I'm totally missing?

Any help would be appreciated. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

锦上情书 2024-08-14 03:56:49

只要您在名称列上有索引,这些就应该可以完成工作。

SQL Server:

SELECT TOP 10 name FROM names WHERE name LIKE 'St%'

MySQL(根据 Bart J):

SELECT name FROM names WHERE name LIKE 'St%' LIMIT 10

Oracle:

SELECT name FROM names WHERE name LIKE 'St%' AND rownum < 10

These should do the job as long as you have an index on the name column.

SQL Server:

SELECT TOP 10 name FROM names WHERE name LIKE 'St%'

MySQL (according to Bart J):

SELECT name FROM names WHERE name LIKE 'St%' LIMIT 10

Oracle:

SELECT name FROM names WHERE name LIKE 'St%' AND rownum < 10
一杯敬自由 2024-08-14 03:56:49

如果您在想要自动完成的字段上有有序索引,那么它可以在“开头为”样式查询中使用。

If you have an ordered index on the field you want to autocomplete then it can be used in a "starts with" style query.

舂唻埖巳落 2024-08-14 03:56:49

大多数升序索引将用于 LIKE 'xxx%' 类型查询的优化。为了提高性能,我建议您对尝试从数据库返回的结果数量设置限制:

SELECT TOP 10 LastName
FROM tbl
WHERE LastName LIKE @start + '%'
ORDER BY LastName

您想要执行的滚动量是有限制的。

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:

SELECT TOP 10 LastName
FROM tbl
WHERE LastName LIKE @start + '%'
ORDER BY LastName

There's a limit to the amount of scrolling you want to do.

起风了 2024-08-14 03:56:49

不要用 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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文