Skip to content

Instantly share code, notes, and snippets.

@othtim
Last active October 24, 2018 00:42
Show Gist options
  • Select an option

  • Save othtim/5936927 to your computer and use it in GitHub Desktop.

Select an option

Save othtim/5936927 to your computer and use it in GitHub Desktop.
shows reads/writes on all tables in a DB as percentages. for choosing better indexes
--show stats on table reads/writes
Select
object_schema_name(UStat.object_id) + '.' + object_name(UStat.object_id) As [Object Name],
Case
When sum(User_Updates + User_Seeks + User_Scans + User_Lookups) = 0
Then Null
Else convert(decimal(5,2),round(Cast(sum(User_Seeks + User_Scans + User_Lookups) As Decimal)
/ Cast(sum(User_Updates
+ User_Seeks
+ User_Scans
+ User_Lookups) As Decimal(19,2)) * 100,2))
End As 'Percent Reads',
Case
When sum(User_Updates + User_Seeks + User_Scans + User_Lookups) = 0 Then Null
Else convert(decimal(5,2),round(Cast(sum(User_Updates) As Decimal)
/ Cast(sum(User_Updates
+ User_Seeks
+ User_Scans
+ User_Lookups) As Decimal(19,2)) * 100,2))
End As 'Percent Writes',
sum(User_Seeks + User_Scans + User_Lookups) As 'Total Read Ops',
sum(User_Updates) As 'Total Write Ops',
cast((sum(User_Seeks + User_Scans + User_Lookups) * 1.00 / (select sum([Total Read Ops]) from (select sum(User_Seeks + User_Scans + User_Lookups) As [Total Read Ops]
From sys.dm_db_Index_Usage_Stats As UStat
Join Sys.Indexes As I
On UStat.object_id = I.object_id
And UStat.index_Id = I.index_Id
Join sys.tables As T
On T.object_id = UStat.object_id
Where I.Type_Desc In ( 'Clustered', 'Heap' )
Group By UStat.object_id) x) ) * 100.00 as decimal(5,2)) as 'Percent of Total Reads',
cast((sum(User_Updates) * 1.00 / (select sum([Total Write Ops]) from (select sum(User_Updates) As [Total Write Ops]
From sys.dm_db_Index_Usage_Stats As UStat
Join Sys.Indexes As I
On UStat.object_id = I.object_id
And UStat.index_Id = I.index_Id
Join sys.tables As T
On T.object_id = UStat.object_id
Where I.Type_Desc In ( 'Clustered', 'Heap' )
Group By UStat.object_id) x) ) * 100.00 as decimal(5,2)) as 'Percent of Total Writes'
From sys.dm_db_Index_Usage_Stats As UStat
Join Sys.Indexes As I
On UStat.object_id = I.object_id
And UStat.index_Id = I.index_Id
Join sys.tables As T
On T.object_id = UStat.object_id
Where I.Type_Desc In ( 'Clustered', 'Heap' )
Group By UStat.object_id
Order By 6 desc, 7 desc
@othtim

othtim commented Jul 5, 2013

Copy link
Copy Markdown
Author

shows reads/writes on all tables in a DB in both percentages of reads/writes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment