by Kalen Delaney via Kalen Delaney on 1/18/2009 8:37:00 PM
I wrote about "Too Many Columns" last September, and along with changes in SQL Server 2008 that allow lots of columns, there is also the ability to create more than the old maximum of 249 nonclustered indexes on a table. I knew this fact, but somehow I overlooked it when updating the chapter on table structures in my new book. My omission also got past all the editors, and I just realized my mistake when reviewing the Indexes chapter, which was being updated for SQL 2008 by Kimberly Tripp.
I found that Books Online has not been completely updated (and I have filed a doc bug on this already). This page still says the limit is 249 nonclustered indexes per table:
http://msdn.microsoft.com/en-us/library/ms190197.aspx
However, the page for CREATE TABLE at http://msdn.microsoft.com/en-us/library/ms174979.aspx does say:
Each table can contain a maximum of 999 nonclustered indexes, and 1 clustered index. These include the indexes generated to support any PRIMARY KEY and UNIQUE constraints defined for the table.
You can take my script to create a table with lots of columns, and edit it to create a UNIQUE constraint on every column.
DECLARE @create varchar(max);DECLARE @tabname sysname;DECLARE @numcols int; DECLARE @col int;SELECT @numcols = 900;SELECT @tabname = 'wide' + CONVERT(varchar, @numcols);
SELECT @create = 'CREATE TABLE ' + @tabname + ' (ID int IDENTITY, ';
SELECT @col = 1;WHILE @col < @numcols BEGIN IF (@col % 3) = 0 SELECT @create = @create + 'col' + CONVERT(varchar, @col) + ' int UNIQUE,'; IF (@col % 3) = 1 SELECT @create = @create + 'col' + CONVERT(varchar, @col) + ' char(5) UNIQUE,'; IF (@col % 3) = 2 SELECT @create = @create + 'col' + CONVERT(varchar, @col) + ' varchar(25) UNIQUE,';SELECT @col = @col + 1;END;SELECT @create = @create + 'col' + CONVERT(varchar, @col) + ' int default 0);'PRINT @create EXECUTE (@create)
After the table is created, you can examine sys.indexes:
SELECT name, index_id, type_descFROM sys.indexesWHERE object_id = object_id('wide900');
You'll definitely see more than 249 indexes! In addition, you might note that there is a gap in the index_id values. The values 251 - 255 are not used, and this is for backward compatibility. In much older versions, index_id (or indid) 255 was reserved for the LOB data belonging to a table, and the values from 251 - 254 were reserved. So that range is still not used. But you can see that I have index_id values from 256 - 905 in this table.
So, thanks Kim~
One more place that Books Online could use a little tweaking is in the section on Programmability Enhancements:
http://msdn.microsoft.com/en-us/library/cc645577.aspx
The section on "Wide Tables" seems to imply that you need to have an XML column set in order to get 1000 indexes. It doesn't actually say that, so it's not really a bug, but it does seem to imply it:
Wide tables are tables that contain one or more column sets. A wide table can contain up to 30000 columns, 1000 indexes, and 30000 statistics.
Because you can only reach the 30000 column limit with a wide table, it seems to imply that the 1000 index limit is only for wide tables. But as my script shows, it is not. You can have up to 1024 columns in a non-wide table, and you can now have indexes on every one of those columns.
But do you want that many? That's for another post, another time...
Have fun!
~Kalen
Original Post: Geek City: Too Many Indexes!
The content of the postings is owned by the respective author. SQL Feeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on SQL Feeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.