I was having a little design rant with Rob about an ERP system I'm working with (http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=188112) and I created this query to count the "clustered index column distribution". What it basically does is count the number of columns in the clustered index and then count the number of tables that has the same count. The following result means that there are 32 tables in the database (out of 3327 tables) that have 16 columns in the clustered index, and the percentage means that 0.96% of the tables have 16 columns in their clustered indexesCAN ANYBODY BEAT THIS???ColumnsInClusteredIndex TablesCount TotalCount Percentage16 32 3327 0.9615 15 3327 0.4514 23 3327 0.6913 33 3327 0.9912 38 3327 1.1411 66 3327 1.9810 69 3327 2.079 105 3327 3.168 165 3327 4.967 245 3327 7.366 322 3327 9.685 458 3327 13.774 535 3327 16.083 631 3327 18.972 407 3327 12.231 183 3327 5.50
Here is the query:with cte as ( select distinct TableName = OBJECT_NAME(ic.OBJECT_ID), ColumnName = COL_NAME(ic.OBJECT_ID,ic.column_id) FROM sys.indexes AS i INNER JOIN sys.index_columns AS ic ON i.OBJECT_ID = ic.OBJECT_ID AND i.index_id = ic.index_id WHERE i.index_id = 1),cte2 as ( select TableName, ColumnsInClusteredIndex = count(*) from cte group by TableName ),cte3 as ( select TotalCount = count(*) from cte2)select ColumnsInClusteredIndex, TablesCount = count(*), TotalCount, Percentage = CONVERT(decimal(5, 2), ((count(*)*1.0) / TotalCount) * 100)from cte2 cross join cte3group by ColumnsInClusteredIndex, TotalCountorder by ColumnsInClusteredIndex desc
- LumbagoMy blog-> http://thefirstsql.com