简体   繁体   English

MS SQL Server 2005上的索引varchar

[英]Index varchar on MS SQL Server 2005

I need to index a varchar field on my table in MS SQL Server 2005, but it's not clear to me how to do so. 我需要在MS SQL Server 2005中索引我的表上的varchar字段,但我不清楚如何这样做。 If I try to add a non-clustered index on the field, it says "Column 'xxxx' in table 'mytable' is of a type that is invalid for use as a key column in an index" 如果我尝试在字段上添加非聚集索引,则表示“mytable”表中的“列'xxxx'的类型无效,无法用作索引中的键列”

My table has an auto-increment int ID that is set as the primary key on the table. 我的表有一个自动增加的int ID,它被设置为表中的主键。 If I set this property as the index, and then add my varchar column as an "included column", the index goes through. 如果我将此属性设置为索引,然后将我的varchar列添加为“包含列”,则索引将完成。 But I'm not sure that's what I want - I want to be able to search the table based on the varchar field alone, and my understanding of indexes was that all indexed elements had to be provided to actually see a speedup in the query, but I don't want to have to included the int ID (because I don't know what it is, at the time of this given query). 但我不确定这是我想要的 - 我希望能够仅基于varchar字段搜索表,而我对索引的理解是必须提供所有索引元素才能真正看到查询中的加速,但我不想要包含int ID(因为我不知道它是什么,在给定的查询时)。

Am I trying to do this incorrectly? 我试图做错了吗? Would the ID + my varchar as an included column accomplish what I am looking for? ID +我的varchar作为一个包含的列能完成我想要的吗?

Is your varchar(max) ? 你的varchar(max) I think those aren't allowed to be used in an index. 我认为不允许在索引中使用它们。

Otherwise, post your CREATE TABLE statement, normally there is no problem adding a varchar to an index. 否则,发布您的CREATE TABLE语句,通常将varchar添加到索引没有问题。

I assume yours is a VARCHAR(MAX) column which, as the error says, is an invalida data type for an index. 我假设你的是一个VARCHAR(MAX)列,正如错误所说,它是索引的invalida数据类型。 Suggestion: create a calculated column that is the hash value of the VARCHAR(MAX) column (eg using the HashBytes function) then create an index on the calculated column only. 建议:创建一个计算列,该列是VARCHAR(MAX)列的哈希值(例如,使用HashBytes函数),然后仅在计算列上创建索引。 Then, in the search condition (eg WHERE clause) of your SQL DML you would use both the VARCHAR(MAX) search value itself plus a hash of your VARCHAR(MAX) search value on the respective columns in your table. 然后,在SQL DML的搜索条件(例如WHERE子句)中,您将同时使用VARCHAR(MAX)搜索值本身以及表中各列上的VARCHAR(MAX)搜索值的哈希值。 It may be a good idea to encapsulate the hashing of search values in a 'helper' stored procedure. 将搜索值的散列封装在“帮助程序”存储过程中可能是个好主意。

No, the ID + varchar column would not work. 不,ID + varchar列不起作用。 That would work great for queries where you do a lookup on the ID and only select the ID or/and the varchar column - then you'd have a covering index and everything could be retrieved only by looking at the index. 这对于在ID上进行查找并且只选择ID或/和varchar列的查询非常有用 - 然后您将拥有覆盖索引,并且只能通过查看索引来检索所有内容。

I'm guessing you have a clustered index on your ID column as that's the primary key. 我猜你的ID列上有一个聚簇索引,因为它是主键。 Then you'd need to create a nonclustered index on the varchar column - which should be possible. 然后,您需要在varchar列上创建一个非聚集索引 - 这应该是可能的。 The nonclustered index will automatically include the ID as well. 非聚集索引也将自动包含ID。

Also remember that the index will only be good for queryes like WHERE VarcharColumn = 'xyz' and WHERE VarcharColumn LIKE 'xyz%'. 还要记住,索引只适用于WHERE VarcharColumn ='xyz'和WHERE VarcharColumn LIKE'xyz%'等查询。

It won't help for LIKE '%xyz%' and '%xyz' queries. 这对LIKE'%xyz%'和'%xyz'查询没有帮助。

Setting a column as primary key per default creates a clustered index, so you don't have to create another INT+VARCHAR index. 将列设置为默认的主键会创建聚簇索引,因此您不必创建另一个INT + VARCHAR索引。

What you're looking for is an index on your VARCHAR alone - without +INT, since your primary key is implicitly included - after all SQL Server should be able to locate the actual row when performing index lookups. 您正在寻找的是单独的VARCHAR上的索引 - 没有+ INT,因为您的主键是隐式包含的 - 所有SQL Server应该能够在执行索引查找时找到实际的行。 There is a restriction though, I believe the overall size of index columns should be < 900 byte (at least it was with SQL Server 2000). 虽然有一个限制,我相信索引列的总体大小应该<900字节(至少是SQL Server 2000)。 How long is your VARCHAR? 你的VARCHAR有多长?

You don't need to include the varchar field in the primary key for it to be indexed. 您无需在主键中包含varchar字段即可将其编入索引。 To create an index just modify the table in Management Studio click on the Manage Indexes and Keys button and click Add to add a new index. 要创建索引,只需在Management Studio中修改表,单击Manage Indexes and Keys按钮,然后单击Add以添加新索引。 Then select the VARCHAR field. 然后选择VARCHAR字段。 There should be no problem. 应该没问题。

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

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