I have a table with nvarchar(max)
datatype column. Max length of data in this column is 37000
, then I can't use nvarchar(4000)
. How can I create index for this column? My data is Unicode text in Persian.
Asked
Active
Viewed 5.4k times
16

Arialdo Martini
- 4,427
- 3
- 31
- 42

Ali Ahmadi
- 2,387
- 4
- 31
- 48
-
4indexing a column of that length would seem largely pointless....suggest you look at Full Text Indexinhg – Mitch Wheat Sep 09 '12 at 05:45
-
1See also http://stackoverflow.com/a/21624526/194717 – Tony Apr 16 '16 at 18:17
2 Answers
20
1- 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)
2-or 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

jozi
- 2,833
- 6
- 28
- 41
-
1yeah, but what's the point of indexing a long text column? Other than a covering index (i.e. part of include, and even then its doubtful optimiser would use it) – Mitch Wheat Sep 09 '12 at 05:54
-
1
-
3
Best is using DROP_EXISTING = ON
, which does a rebuild using existing index.
Here is an example
CREATE NONCLUSTERED INDEX IDX_NC_temp_example_name ON #example(id) INCLUDE(name)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = ON, SORT_IN_TEMPDB = ON, DROP_EXISTING = ON, ONLINE = ON, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY]

Tim Penner
- 3,551
- 21
- 36

CoachJames
- 31
- 1