November 3, 2016 at 11:19 pm
In one of our databases, I just observed an index which is over 900 bytes.
Index is on 3 key fields (no included columns)
Col1 INT, Col2 NVARCHAR(255), Col3 NVARCHAR(255)
Total datalength for above is 1024 bytes (since nvarchar takes 2 bytes per character)
This is the query that I have come up with and gives me the results I need.
Am I overlooking something here by using sys.columns.max_length field?
SELECT ss.name AS SchemaName, so.name AS TableName, si.name AS IndexName, SUM(sc.max_length) AS KeyColumnMaxLength
FROM sys.objects so JOIN sys.columns sc ON so.object_id = sc.object_id
JOIN sys.schemas ss ON so.schema_id = ss.schema_id
JOIN sys.indexes si ON sc.object_id = si.object_id
JOIN sys.index_columns sic ON si.object_id = sic.object_id AND si.index_id = sic.index_id AND sic.column_id = sc.column_id
WHERE so.type = 'U' --AND so.name NOT IN('sysdiagrams')
AND sic.is_included_column = 0--only key column length counts for index max length of 900 bytes
GROUP BY ss.name, so.name, si.name
--HAVING SUM(sc.max_length) > 900--filter only the ones with > 900
ORDER BY 4 DESC
Thanks,
Santhosh
November 4, 2016 at 7:29 am
It seems correct, the only issue would be with xml columns, but I'm sure those would be handled differently.
November 4, 2016 at 7:36 am
Luis Cazares (11/4/2016)
It seems correct, the only issue would be with xml columns, but I'm sure those would be handled differently.
Not a concern here, since XML columns, like the MAX datatypes can't be in the index key.
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
November 4, 2016 at 7:52 am
Thank you...
I was wondering how the max_length field was calculated and tried digging the system view sys.columns
and stuck at sys.syscolpars and thought to simply trust the view !!
Thanks,
Santhosh
November 4, 2016 at 9:02 am
GilaMonster (11/4/2016)
Luis Cazares (11/4/2016)
It seems correct, the only issue would be with xml columns, but I'm sure those would be handled differently.Not a concern here, since XML columns, like the MAX datatypes can't be in the index key.
I mentioned it, because the query returns XML indexes. Which of course use xml columns.
As mentioned, it's not a concern. Especially when using the HAVING clause.
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply