Advertisement
chadbaldwin

Splunk - SQL Server Index Usage Statistics

Sep 15th, 2023
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. index="myindex" sourcetype="Foo:Bar:Baz:IndexUsageStats"
  2. | rename
  3. SQLServerStartTimeUTC as sSQLServerStartTimeUTC, ```rename to more easily differentiate between fields (s = string)```
  4. | eval ```this could be eliminated if we were to load this value directly as a unix timestamp```
  5. uSQLServerStartTimeUTC = strptime(sSQLServerStartTimeUTC, "%Y-%m-%dT%H:%M:%S.%3N") ```Parse sSQLServerStartTimeUTC to unix timestamp (u = unix)```
  6. | streamstats current=f ```Grab the next event stats```
  7. last("User*Count") as "NextUser*Count"
  8. last(EstimatedIndexSizeKB) as NextEstimatedIndexSizeKB
  9. last(IndexRowCount) as NextIndexRowCount
  10. last(_time) as NextEventTime
  11. last(uSQLServerStartTimeUTC) as uNextSQLServerStartTimeUTC
  12. by host DatabaseName ObjectSchemaName ObjectName IndexName
  13. | eval ```determine whether SQL Server was restarted since the next event```
  14. WasSQLServerRestarted = if(uSQLServerStartTimeUTC < uNextSQLServerStartTimeUTC, 1, 0),
  15. ```calculate the stats deltas```
  16. DiffUserSeeksCount = NextUserSeeksCount - if(WasSQLServerRestarted = 1, 0, UserSeeksCount),
  17. DiffUserScansCount = NextUserScansCount - if(WasSQLServerRestarted = 1, 0, UserScansCount),
  18. DiffUserLookupsCount = NextUserLookupsCount - if(WasSQLServerRestarted = 1, 0, UserLookupsCount),
  19. DiffUserUpdatesCount = NextUserUpdatesCount - if(WasSQLServerRestarted = 1, 0, UserUpdatesCount),
  20. DiffEventTimeSeconds = NextEventTime - if(WasSQLServerRestarted = 1, uNextSQLServerStartTimeUTC, _time),
  21. DiffEstimatedIndexSizeKB = NextEstimatedIndexSizeKB - EstimatedIndexSizeKB,
  22. DiffIndexRowCount = NextIndexRowCount - IndexRowCount
  23. | stats
  24. count as TotalEvents
  25. latest(_time) as LatestEventTime
  26. ```metadata```
  27. latest(IsUnique) as IsUnique
  28. latest(IsUniqueConstraint) as IsUniqueConstraint
  29. latest(IsPrimaryKey) as IsPrimaryKey
  30. latest(IsDisabled) as IsDisabled
  31. latest(HasFilter) as HasFilter
  32. latest(IndexType) as IndexType
  33. latest(ObjectType) as ObjectType
  34.  
  35. latest(ObjectCreateDate) as sObjectCreateDate
  36. latest(EstimatedIndexSizeKB) as EstimatedIndexSizeKB
  37. latest(IndexRowCount) as IndexRowCount
  38. ```sum up each event delta```
  39. sum("DiffUser*Count") as "TotalUser*"
  40. sum(DiffEstimatedIndexSizeKB) as TotalEstimatedIndexSizeKBChange
  41. sum(DiffIndexRowCount) as TotalIndexRowCountChange
  42. sum(DiffEventTimeSeconds) as TotalEventTimeSeconds
  43. by host DatabaseName ObjectSchemaName ObjectName IndexName
  44. | search
  45. TotalEvents > 1
  46. | eval
  47. TotalReads = TotalUserSeeks + TotalUserScans + TotalUserLookups,
  48. TotalWrites = TotalUserUpdates,
  49. TotalStatsDays = TotalEventTimeSeconds / 86400,
  50. IsArchived = if(LatestEventTime < relative_time(now(), "-7d"), 1, 0),
  51. uObjectCreateDate = strptime(sObjectCreateDate, "%Y-%m-%dT%H:%M:%S.%3N")
  52. | fields
  53. host DatabaseName ObjectSchemaName ObjectName uObjectCreateDate sObjectCreateDate ObjectType IndexName IsArchived LatestEventTime
  54. IndexType IsUnique IsUniqueConstraint IsPrimaryKey IsDisabled HasFilter EstimatedIndexSizeKB IndexRowCount
  55. TotalUserSeeks TotalUserScans TotalUserLookups
  56. TotalReads TotalWrites TotalEstimatedIndexSizeKBChange TotalIndexRowCountChange TotalEvents TotalStatsDays
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement