SQL Server:普通索引与全文索引

发布于 2024-07-29 03:18:10 字数 156 浏览 5 评论 0原文

varchar 列上的全文索引和常规索引到底有什么区别(以及优点/缺点)? 我什么时候使用哪个索引?

我有大量 varchar 列(地址 - 城市名称、街道名称等),我需要以最高效的方式对其进行搜索,并且我正在尝试找出要使用的索引类型以及原因。

谢谢你!

what exactly is the difference (and advantages/disadvantages) between a fulltext index and a regular index on a varchar column? When would I use which index?

I have a multitude of varchar columns (addresses - city name, street name etc.) which I need to be searchable in the most performant way and I'm trying to figure out which index type to use and why.

Thank you!

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

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

发布评论

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

评论(3

话少情深 2024-08-05 03:18:10

这取决于您想要执行的搜索类型。 例如,您不能在此查询中使用普通索引:

SELECT * FROM [MyTable] WHERE [MyColumn] LIKE '%' + @SearchText + '%'

它不是sargable。 这是可控制的,但选择性可能不是很好:

SELECT * FROM [MyTable] WHERE [MyColumn] LIKE @SearchText + '%'

您使用全文索引的方式完全不同:

SELECT * FROM [MyTable] WHERE CONTAINS([MyColumn], @SearchText)

It depends on the kind of search you want to do. For example, you can't use a normal index with this query:

SELECT * FROM [MyTable] WHERE [MyColumn] LIKE '%' + @SearchText + '%'

It's not sargable. This is sargable, but the selectivity might not be very good:

SELECT * FROM [MyTable] WHERE [MyColumn] LIKE @SearchText + '%'

You use a full-text index completely differently:

SELECT * FROM [MyTable] WHERE CONTAINS([MyColumn], @SearchText)
他不在意 2024-08-05 03:18:10

通常,当使用普通索引搜索时,您只能在单个字段中搜索,例如“查找以 A 开头的所有城市”或类似的内容。

全文索引允许您跨多个列进行搜索,例如在街道、城市、省份等中同时搜索。如果您想做类似 Google 式搜索的操作,这可能是一个优势 - 只需输入搜索词即可找到所有内容在任何 varchar 列中的任意位置具有该搜索词的行。

此外,通过常规搜索,您可以做的事情相当有限 - 您可以搜索完全匹配或只是喜欢 - 仅此而已。

使用全文索引,您可以搜索单词形式(ran、run 等),也可以通过指定您自己的同义词库来搜索类似单词。 如果存在问题,您可以根据多种语言进行搜索。 您可以搜索包含两个或多个彼此“接近”的术语的条目。

马克

Usually, when searching with a normal index, you can search only in a single field, e.g. "find all cities that begin with A" or something like that.

Fulltext index allows you to search across multiple columns, e.g. search at once in street, city, province, etc. That might be an advantage if you want to do something like a Google-style search - just punch in a search term and find all rows that have that search term anywhere in any of the varchar columns.

Additionally, with a regular search, you are fairly limited in what you can do - you can search for an exact match or just LIKE - that's about it.

With fulltext index, you can search for word forms (ran, run, etc.) and also for similar words by specifying your own thesaurus. You can search based on several languages if that's an issue. You can search for entries that have two or more terms that are "NEAR" one another.

Marc

子栖 2024-08-05 03:18:10

来自 MSDN

与全文搜索相反,LIKE Transact-SQL 谓词仅适用于字符模式。 此外,您不能使用 LIKE 谓词来查询格式化的二进制数据。 此外,针对大量非结构化文本数据的 LIKE 查询比针对相同数据的等效全文查询要慢得多。

针对数百万行文本数据的 LIKE 查询可能需要几分钟才能返回; 而针对相同数据的全文查询只需几秒或更短的时间,具体取决于返回的行数。

From the MSDN:

In contrast to full-text search, the LIKE Transact-SQL predicate works on character patterns only. Also, you cannot use the LIKE predicate to query formatted binary data. Furthermore, a LIKE query against a large amount of unstructured text data is much slower than an equivalent full-text query against the same data.

A LIKE query against millions of rows of text data can take minutes to return; whereas a full-text query can take only seconds or less against the same data, depending on the number of rows that are returned.

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