SQL SERVER – Fix: Error: Msg 1904, Level 16 The statistics on table has 33 column names in statistics key list. The maximum limit for index or statistics key column list is 32

Earlier I wrote an article where I demonstrated that an index with more than 16 column is not possible. Here is the link to the article. After reading the same article I received email from user suggesting does it mean that statistics can be only created on only 16 columns. Well, answer is NO. One can create statistics on total of 32 columns, where as the limit of creating index is only 16 columns (and 900 bytes).

Here is the quick example where when attempted to create statistics on 33 columns is generating error but when statistics are created on 32 columns it works successfully.

Set up Script:

CREATE DATABASE TestDB
GO
USE TestDB
GO
CREATE TABLE Test1
(ID1 INT,  ID2 INT, ID3 INT,
ID4 INT, ID5 INT, ID6 INT,
ID7 INT, ID8 INT, ID9 INT,
ID10 INT, ID11 INT, ID12 INT,
ID13 INT, ID14 INT, ID15 INT,
ID16 INT, ID17 INT, ID18 INT,
ID19 INT, ID20 INT, ID21 INT,
ID22 INT, ID23 INT, ID24 INT,
ID25 INT, ID26 INT, ID27 INT,
ID28 INT, ID29 INT, ID30 INT,
ID31 INT, ID32 INT, ID33 INT)
GO

Here is the example when index created on 33 columns it gives error.

CREATE STATISTICS [Stats_Test1] ON [dbo].[Test1] ([ID1], [ID2], [ID3], [ID4], [ID5],
[ID6], [ID7], [ID8], [ID9], [ID10],
[ID11], [ID12], [ID13], [ID14], [ID15],
[ID16], [ID17], [ID18], [ID19], [ID20],
[ID21], [ID22], [ID23], [ID24], [ID25],
[ID26], [ID27], [ID28], [ID29], [ID30],
[ID31], [ID32], [ID33] )
GO

Msg 1904, Level 16, State 2, Line 1
The statistics ‘Stats_Test1’ on table ‘dbo.Test1’ has 33 column names in statistics key list. The maximum limit for index or statistics key column list is 32.

Here is the example when index created on 32 columns it works perfectly fine.

CREATE STATISTICS [Stats_Test1] ON [dbo].[Test1] ([ID1], [ID2], [ID3], [ID4], [ID5],
[ID6], [ID7], [ID8], [ID9], [ID10],
[ID11], [ID12], [ID13], [ID14], [ID15],
[ID16], [ID17], [ID18], [ID19], [ID20],
[ID21], [ID22], [ID23], [ID24], [ID25],
[ID26], [ID27], [ID28], [ID29], [ID30],
[ID31], [ID32] )
GO

Well, in simple words, one can create statistics on 32 columns. Please additionally, note, the scope of this blog is not to discuss if that is good or bad. The purpose of this post is notice the Index can have maximum 16 columns but statistics can have 32 columns.

Reference : Pinal Dave (https://blog.sqlauthority.com)

,
Previous Post
SQLAuthority News – SQL Saturday 116 – SQL Saturday in Bangalore, India on January 7, 2012 – 4 Saturdays to Go
Next Post
SQLAuthority News – Virtual Presentation on Practical Tricks and Tips to Speed up Database Queries – December 15, 2011

Related Posts

4 Comments. Leave new

Leave a Reply

Menu