Read more: http://www.blogsmonetize.com/2010/10/how-to-use-syntax-highlighter-3083-in.html#ixzz1DHzvEgBA

Thursday, April 14, 2011

Table Information

The below script provides a complete possible information on a table.
/*
First Resultset(Table Information)
Table Name
ObjectId
Table Type
Is replicated table or not
Row count of the table

Second Resultset(Column information)

Third Resultset(Index Information)
Name, Id, Seeks, scans, updates, lock information, type of index, constraint information, fill factor and partition number

Forth Resultset(Detail index information)
Fifth Resultset(Detail Statistics information)
Sixth Resultset(Detail relation key information)
*/




Declare @TableName Varchar(100)
Set @TableName = 'TableName'

Select @TableName,A.Object_Id,Case When A.index_id=0 Then 'Heap' Else 'Clustered Table' End ,
Case When B.objId is null Then 'Not Replicated' Else 'Replicated' End Is_Replicated,C.row_count
From sys.partitions A
Left Join sys.dm_db_partition_stats C On A.object_id = C.object_id and c.index_id in(0,1)
Left Join sysarticles B On A.object_id = B.Objid
Where A.index_id <2 and A.object_id = Object_Id(@TableName)

Select column_id,name,TYPE_NAME(user_type_id),max_length,precision,scale,Case When Is_Nullable =0 Then 'No' Else 'Yes' End
, Is_Identity From sys.columns where object_id = OBJECT_ID(@TableName)


Create Table #Temp_Index_Details
(
[Object_Id] BigInt,
[Object_Name] Varchar(100),
Index_ID Int,
Index_Name Varchar(100),
User_Seeks BigInt,
User_Scans BigInt,
User_Lookups BigInt,
Index_Rows Bigint,
User_Updates BigInt,
Index_Lock_Attempt_Count BigInt,
Index_Lock_Promotion_Count BigInt,
page_lock_wait_count BigInt,
page_lock_wait_in_ms BigInt,
[FillFactor] Int,
Constraint_Type VarChar(100),
Partition_Number BigInt
)

Insert Into #Temp_Index_Details ([Object_Id] ,
[Object_Name] ,
Index_ID ,
Index_Name ,
User_Seeks ,
User_Scans ,
User_Lookups ,
Index_Rows ,
User_Updates ,
[FillFactor] ,
Constraint_Type )
SELECT u.object_id,OBJECT_NAME(u.object_id) , i.indid
, i.name , u.user_seeks , u.user_scans
, u.user_lookups , i.rowcnt , u.user_updates,i.OrigFillFactor
, k.[type] AS [constraint type]
FROM sys.dm_db_index_usage_stats u
INNER JOIN sys.sysindexes i ON u.object_id = i.id AND u.index_id = i.indid
LEFT OUTER JOIN sys.key_constraints k
ON i.id = k.parent_object_id AND i.indid = k.unique_index_id
WHERE u.database_id = db_id() and OBJECT_NAME(u.object_id)=@TableName
ORDER BY OBJECT_NAME(u.object_id), i.name, u.user_updates DESC ;



Update B Set B.Partition_Number = A.partition_number,
B.Index_Lock_Attempt_Count = index_lock_promotion_attempt_count,
B.Index_Lock_Promotion_Count = A.index_lock_promotion_count ,
B.page_lock_wait_count = A.page_lock_wait_count,
B.page_lock_wait_in_ms = A.page_lock_wait_in_ms
FROM sys.dm_db_index_operational_stats
(db_id('Cognizant20'), object_id(@TableName), NULL, NULL) A
Inner Join #Temp_Index_Details B On A.object_id = B.Object_Id and A.index_id = B.Index_ID


Select [Object_Name], Index_ID, Index_Name, User_Seeks, User_Scans, User_Lookups, Index_Rows, User_Updates,
Index_Lock_Attempt_Count, Index_Lock_Promotion_Count, page_lock_wait_count, page_lock_wait_in_ms,
[FillFactor], Constraint_Type, Partition_Number From #Temp_Index_Details

Select A.name IndexName,A.index_id,c.name ColumnName,
Case When System_type_id = User_Type_id Then Type_Name(System_type_id) Else Type_Name(User_Type_id) End
,A.type_desc,is_identity,is_included_column,is_replicated
,Is_Unique,Is_Primary_Key,IS_Unique_Constraint,Fill_factor,is_Padded,is_replicated,is_nullable,is_computed

From sys.indexes A
Inner Join sys.index_columns B On A.object_id = B.object_id And A.index_id = B.index_id
Inner Join sys.columns C On c.object_id = B.object_id And C.column_id = B.column_id
Where A.Object_ID = OBJECT_ID(@TableName)
order by A.Index_id,Is_included_Column,Key_ordinal asc

--Stats information
Select A.object_id,A.name,A.stats_id,C.column_id,C.name,A.auto_created,user_created,has_filter,STATS_DATE(A.object_id,A.stats_id)
From sys.stats A
Inner Join sys.stats_columns B On A.object_id = B.object_id and A.stats_id = B.stats_id
Inner Join sys.columns C On B.column_id = C.column_id and A.object_id = C.object_id
where A.object_id = OBJECT_ID(@Tablename)



--Primary Key and Foreign Key information
SELECT tc.TABLE_NAME AS PrimaryKeyTable,
tc.CONSTRAINT_NAME AS PrimaryKey,
COALESCE(rc1.CONSTRAINT_NAME,'N/A') AS ForeignKey ,
COALESCE(tc2.TABLE_NAME,'N/A') AS ForeignKeyTable
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
LEFT JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc1 ON tc.CONSTRAINT_NAME =rc1.UNIQUE_CONSTRAINT_NAME
LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc2 ON tc2.CONSTRAINT_NAME =rc1.CONSTRAINT_NAME
WHERE TC.CONSTRAINT_TYPE ='PRIMARY KEY' and OBJECT_ID(tc.TABLE_NAME) = OBJECT_ID(@Tablename)
ORDER BY tc.TABLE_NAME,tc.CONSTRAINT_NAME,rc1.CONSTRAINT_NAME


Drop Table #Temp_Index_Details

No comments: