August 23, 2011 at 3:12 am
hi,
can we create an index on an a coloumn on datatype varchar/nvarchar having maximum size?
ex :
create table example
(
id int primary key identity(1,1),
name varchar(max)
)
create nonclustered index XL_example on example(name)
August 23, 2011 at 3:38 am
August 23, 2011 at 3:41 am
Not on it's own, no.
IF OBJECT_ID('tempdb..#example') IS NOT NULL
BEGIN
DROP TABLE #example
END
CREATE TABLE #example (id INT PRIMARY KEY IDENTITY(1,1), name VARCHAR(MAX))
CREATE NONCLUSTERED INDEX IDX_NC_temp_example_name ON #example(name)
But you could use it in an "INCLUDE"
IF OBJECT_ID('tempdb..#example') IS NOT NULL
BEGIN
DROP TABLE #example
END
CREATE TABLE #example (id INT PRIMARY KEY IDENTITY(1,1), name VARCHAR(MAX))
CREATE NONCLUSTERED INDEX IDX_NC_temp_example_name ON #example(id) INCLUDE(name)
August 23, 2011 at 3:50 am
However if you do, the entire varchar(max) is duplicated.
There's a more basic issue here. Why is a name column up to 2 billion characters long? Surely a name is on the order of 50 at most?
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
August 23, 2011 at 3:53 am
You may consider to use "CHECKSUM" method. It's inteded for buidling hash indexes, especially to improve indexing speed for indexing long character columns (as you have).
You can read more and find examples: http://msdn.microsoft.com/en-us/library/ms189788.aspx
August 23, 2011 at 4:42 am
The maximum key length is 900 bytes and if you create the nonclustered index that is having > 900 bytes. Index will create (expect non-key columns i.e., Columns that are of the ntext, text, image, varchar(max), nvarchar(max), and varbinary(max) data types cannot be specified as index key columns) and any DML opertaion against that column will fail.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply