简体   繁体   English

索引将改善varchar(max)查询性能,以及如何创建索引

[英]Will indexing improve varchar(max) query performance, and how to create index

Firstly, I should point out I don't have much knowledge on SQL Server indexes. 首先,我应该指出,我对SQL Server索引了解不多。

My situation is that I have an SQL Server 2008 database table that has a varchar(max) column usually filled with a lot of text. 我的情况是我有一个SQL Server 2008数据库表,该表具有通常填充许多文本的varchar(max)列。

My ASP.NET web application has a search facility which queries this column for keyword searches, and depending on the number of keywords searched for their may be one or many LIKE '%keyword%' statements in the SQL query to do the search. 我的ASP.NET Web应用程序具有一个搜索工具,该工具可在此列中查询关键字搜索,并且根据搜索关键字的数量,可以在SQL查询中执行一个或多个LIKE'%keyword%'语句来进行搜索。

My web application also allows searching by various other columns in this table as well, not just that one column. 我的Web应用程序还允许按此表中的其他各个列进行搜索,而不仅限于该列。 There is also a few joins from other tables too. 其他表也有一些联接。

My question is, is it worthwhile creating an index on this column to improve performance of these search queries? 我的问题是,是否值得在此列上创建索引以提高这些搜索查询的性能? And if so, what type of index, and will just indexing the one column be enough or do I need to include other columns such as the primary key and other searchable columns? 如果是这样,什么类型的索引,仅对一个列进行索引就足够了?还是需要包括其他列(例如主键和其他可搜索列)?

The best analogy I've ever seen for why an index won't help '%wildcard%' searches: 对于索引为什么无法帮助'%wildcard%'搜索,我见过的最好的比喻:

Take two people. 带两个人。 Hand each one the same phone book. 递给每个人相同的电话簿。 Say to the person on your left: 对您左边的人说:

Tell me how many people are in this phone book with the last name "Smith." 告诉我此电话簿中有多少人的姓氏为“ Smith”。

Now say to the person on your right: 现在对右边的人说:

Tell me how many people are in this phone book with the first name "Simon." 告诉我这本电话簿中有多少人叫“ Simon”。

An index is like a phone book. 索引就像电话簿。 Very easy to seek for the thing that is at the beginning. 很容易找到开始的东西。 Very difficult to scan for the thing that is in the middle or at the end. 扫描中间或结尾的东西非常困难。

Every time I've repeated this in a session, I see light bulbs go on, so I thought it might be useful to share here. 每次在会话中重复此操作时,都会看到灯泡点亮,因此我认为在此处分享可能会很有用。

you cannot create an index on a varchar(max) field. 您不能在varchar(max)字段上创建索引。 The maximum amount of bytes on a index is 900. If the column is bigger than 900 bytes, you can create the index but any insert with more then 900 bytes will fail. 索引上的最大字节数为900。如果该列大于900字节,则可以创建索引,但是任何大于900字节的插入都会失败。

I suggest you to read about fulltext search. 我建议您阅读全文搜索。 It should suits you in this case 在这种情况下应该适合您

It's not worthwhile creating a regular index if you're doing LIKE '%keyword%' searches. 如果要进行“%keyword%”搜索,则不值得创建常规索引。 The reason is that indexing works like searching a dictionary, where you start in the middle then split the difference until you find the word. 原因是索引的工作方式类似于搜索字典,从中间开始,然后拆分差异,直到找到单词为止。 That wildcard query is like asking you to lookup a word that contains the text "to" or something-- the only way to find matches is to scan the whole dictionary. 通配符查询就像要求您查找包含文本“ to”或某物的单词一样,查找匹配项的唯一方法是扫描整个词典。

You might consider a full-text search, however, which is meant for this kind of scenario ( see here ). 但是,您可能会考虑进行全文搜索,这是针对这种情况的( 请参阅此处 )。

The best way to find out is to create a bunch of test queries that resemble what would happen in real life and try to run them against your DB with and without the index. 找出答案的最佳方法是创建一堆类似于现实生活中的测试查询,并尝试在有或没有索引的情况下针对您的数据库运行它们。 However, in general, if you are doing many SELECT queries, and little UPDATE/DELETE queries, an index might make your queries faster. 但是,通常,如果您执行许多SELECT查询,而很少执行UPDATE / DELETE查询,则索引可能会使您的查询更快。

However, if you do a lot of updates, the index might hurt your performance, so you have to know what kind of queries your DB will have to deal with before you make this decision. 但是,如果进行大量更新,索引可能会损害性能,因此在做出此决定之前,您必须知道数据库将要处理的查询类型。

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

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