CreateSystemhealthDBAndSchema.sql 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  1. /**************************
  2. This Sample Code is provided for the purpose of illustration only and is not intended to be used in a production environment. THIS SAMPLE CODE AND ANY RELATED INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. We grant You a nonexclusive, royalty-free right to use and modify the Sample Code and to reproduce and distribute the object code form of the Sample Code, provided that You agree: (i) to not use Our name, logo, or trademarks to market Your software product in which the Sample Code is embedded; (ii) to include a valid copyright notice on Your software product in which the Sample Code is embedded; and (iii) to indemnify, hold harmless, and defend Us and Our suppliers from and against any claims or lawsuits, including attorneys’ fees, that arise or result from the use or distribution of the Sample Code.
  3. Author: Denzil Ribeiro
  4. Date: Jan 6, 2013
  5. Description:
  6. This T-SQL script extracts information found in the System Health Session and puts them into a permanent tables in a database you create
  7. */
  8. /*
  9. use master
  10. go
  11. drop database XEvents_ImportSystemHealth
  12. go
  13. Create Database XEvents_ImportSystemHealth
  14. go
  15. Alter Database XEvents_ImportSystemHealth SET RECOVERY SIMPLE;
  16. USE [XEvents_ImportSystemHealth]
  17. GO
  18. */
  19. /****** Object: StoredProcedure [dbo].[sp_ImportXML] Script Date: 1/25/2013 3:39:13 PM ******/
  20. use dba_local
  21. GO
  22. SET ANSI_NULLS ON
  23. GO
  24. SET QUOTED_IDENTIFIER ON
  25. GO
  26. Create procedure [dbo].[sp_ImportXML]
  27. @path_to_health_session nvarchar(4000)
  28. as
  29. If object_id('tbl_XEImport') is not null
  30. drop table tbl_XEImport
  31. select [object_name] ,CAST(event_data AS XML) as c1
  32. into tbl_XEImport
  33. from sys.fn_xe_file_target_read_file(@path_to_health_session,NULL,NULL,NULL)
  34. create index ind_xeImport on tbl_XEImport(object_name)
  35. If object_id('tbl_ServerDiagnostics') is not null
  36. drop table tbl_ServerDiagnostics
  37. select c1.value('(event/data[@name="component"]/text)[1]', 'varchar(100)') as SdComponent,c1
  38. into tbl_ServerDiagnostics
  39. from tbl_XEImport
  40. where object_name = 'sp_server_diagnostics_component_result'
  41. /*
  42. else
  43. select c1.value('(event/data[@name="component"]/value)[1]', 'varchar(100)') as SdComponent,c1
  44. into tbl_ServerDiagnostics
  45. from tbl_XEImport
  46. where object_name = 'component_health_result'
  47. */
  48. --create index ind_ServerDiagnostics on tbl_ServerDiagnostics(SdComponent)
  49. GO
  50. /****** Object: StoredProcedure [dbo].[SpLoadComponentSummary] Script Date: 1/25/2013 3:39:13 PM ******/
  51. SET ANSI_NULLS ON
  52. GO
  53. SET QUOTED_IDENTIFIER ON
  54. GO
  55. Create procedure [dbo].[SpLoadComponentSummary]
  56. @UTDDateDiff int
  57. as
  58. if object_id('tbl_Summary') is not null
  59. drop table tbl_Summary
  60. select
  61. c1.value('(event/@timestamp)[1]','datetime') as [UTCtimestamp],
  62. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as timestamp,
  63. c1.value('(event/data[@name="component"]/text)[1]', 'varchar(100)') as component_name,
  64. c1.value('(event/data[@name="state"]/text)[1]', 'varchar(100)') as [component_state]
  65. /*
  66. CASE c1.value('(event/data[@name="component"]/text)[1]', 'varchar(100)')
  67. WHEN '' then c1.value('(event/data[@name="component"]/text)[1]', 'varchar(100)')
  68. ELSE c1.value('(event/data[@name="component"]/value)[1]', 'varchar(100)')
  69. END as component_name,
  70. CASE c1.value('(event/data[@name="state"]/text)[1]', 'varchar(100)')
  71. WHEN '' then c1.value('(event/data[@name="state"]/text)[1]', 'varchar(100)')
  72. ELSE c1.value('(event/data[@name="state_desc"]/value)[1]', 'varchar(100)')
  73. END as [component_state]
  74. */
  75. into tbl_Summary
  76. FROM tbl_ServerDiagnostics
  77. CREATE NONCLUSTERED INDEX [Ind_TblSummary] ON [dbo].[tbl_Summary]
  78. (
  79. [timestamp] ASC
  80. )WITH (SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF) ON [PRIMARY]
  81. GO
  82. /****** Object: StoredProcedure [dbo].[spLoadConnectivity_ring_buffer] Script Date: 1/25/2013 3:39:13 PM ******/
  83. SET ANSI_NULLS ON
  84. GO
  85. SET QUOTED_IDENTIFIER ON
  86. GO
  87. Create procedure [dbo].[spLoadConnectivity_ring_buffer]
  88. @UTDDateDiff int
  89. as
  90. if object_id('tbl_connectivity_ring_buffer') is not null
  91. drop table tbl_connectivity_ring_buffer
  92. select
  93. c1.value('(./event/@timestamp)[1]', 'datetime') as utctimestamp,
  94. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp],
  95. c1.value('(./event/data[@name="type"]/text)[1]', 'varchar(100)') AS [Type],
  96. c1.value('(./event/data[@name="id"]/value)[1]', 'bigint') as record_id,
  97. c1.value('(./event/data[@name="source"]/text)[1]', 'varchar(20)') as source,
  98. c1.value('(./event/data[@name="session_id"]/value)[1]', 'int') as session_id,
  99. c1.value('(./event/data[@name="os_error"]/value)[1]', 'bigint') as os_error,
  100. c1.value('(./event/data[@name="sni_error"]/value)[1]', 'bigint') as sni_error,
  101. c1.value('(./event/data[@name="sni_consumer_error"]/value)[1]', 'bigint') as sni_consumer_error,
  102. c1.value('(./event/data[@name="state"]/value)[1]', 'int') as [state],
  103. c1.value('(./event/data[@name="port"]/value)[1]', 'int') as port,
  104. c1.value('(./event/data[@name="remote_port"]/value)[1]', 'int') as remote_port,
  105. c1.value('(./event/data[@name="tds_input_buffer_error"]/value)[1]', 'bigint') as tds_inputbuffererror,
  106. c1.value('(./event/data[@name="total_login_time_ms"]/value)[1]', 'bigint') as total_login_time_ms,
  107. c1.value('(./event/data[@name="login_task_enqueued_ms"]/value)[1]', 'bigint') as login_task_enqueued_ms,
  108. c1.value('(./event/data[@name="network_writes_ms"]/value)[1]', 'bigint') as network_writes_ms,
  109. c1.value('(./event/data[@name="network_reads_ms"]/value)[1]', 'bigint') as network_reads_ms,
  110. c1.value('(./event/data[@name="ssl_processing_ms"]/value)[1]', 'bigint') as ssl_processing_ms,
  111. c1.value('(./event/data[@name="sspi_processing_ms"]/value)[1]', 'bigint') as sspi_processing_ms,
  112. c1.value('(./event/data[@name="login_trigger_and_resource_governor_processing_ms"]/value)[1]', 'bigint') as login_trigger_and_resource_governor_processing_ms,
  113. c1.value('(./event/data[@name="connection_id"]/value)[1]', 'varchar(50)') as connection_id,
  114. c1.value('(./event/data[@name="connection_peer_id"]/value)[1]', 'varchar(50)') as connection_peer_id,
  115. c1.value('(./event/data[@name="local_host"]/value)[1]', 'varchar (50)') as local_host,
  116. c1.value('(./event/data[@name="remote_host"]/value)[1]', 'varchar (50)') as remote_host,
  117. c1.value('(./event/data[@name="SessionIsKilled"]/value)[1]', 'smallint') as SessionIsKilled
  118. into tbl_connectivity_ring_buffer
  119. from tbl_XEImport where object_name = 'connectivity_ring_buffer_recorded'
  120. GO
  121. /****** Object: StoredProcedure [dbo].[SpLoadErrorRecorded] Script Date: 1/25/2013 3:39:13 PM ******/
  122. SET ANSI_NULLS ON
  123. GO
  124. SET QUOTED_IDENTIFIER ON
  125. GO
  126. Create procedure [dbo].[SpLoadErrorRecorded]
  127. @UTDDateDiff int
  128. as
  129. if object_id('tbl_errors') is not null
  130. drop table tbl_errors
  131. select
  132. c1.value('(./event/@timestamp)[1]', 'datetime') as utctimestamp,
  133. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp],
  134. c1.value('(./event/data[@name="session_id"])[1]', 'int') as session_id,
  135. c1.value('(./event/data[@name="database_id"])[1]', 'int') as database_id,
  136. c1.value('(./event/data[@name="error_number"])[1]', 'int') as [error_number],
  137. c1.value('(./event/data[@name="severity"])[1]', 'int') as severity,
  138. c1.value('(./event/data[@name="state"])[1]', 'int') as [state],
  139. c1.value('(./event/data[@name="category"]/text)[1]', 'nvarchar(100)') as category,
  140. c1.value('(./event/data[@name="destination"]/text)[1]', 'nvarchar(100)') as destination,
  141. c1.value('(./event/data[@name="message"])[1]', 'nvarchar(1000)') as message
  142. into tbl_errors
  143. from tbl_XEImport
  144. where object_name like 'error_reported'
  145. GO
  146. /****** Object: StoredProcedure [dbo].[SpLoadIO_SUBSYSTEMComponent] Script Date: 1/25/2013 3:39:13 PM ******/
  147. SET ANSI_NULLS ON
  148. GO
  149. SET QUOTED_IDENTIFIER ON
  150. GO
  151. Create procedure [dbo].[SpLoadIO_SUBSYSTEMComponent]
  152. @UTDDateDiff int
  153. as
  154. if object_id('tbl_IO_SUBSYSTEM') is not null
  155. drop table tbl_IO_SUBSYSTEM
  156. select
  157. c1.value('(./event/@timestamp)[1]', 'datetime') as UTCtimestamp,
  158. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as timestamp,
  159. c1.value('(event/data[@name="component"]/text)[1]', 'varchar(100)') as [component_name],
  160. c1.value('(event/data[@name="state"]/text)[1]', 'varchar(100)') as [component_state],
  161. c1.value('(event/data[@name="data"]/value/ioSubsystem/@ioLatchTimeouts)[1]','int') as [ioLatchTimeouts],
  162. c1.value('(event/data[@name="data"]/value/ioSubsystem/@intervalLongIos)[1]','int') as [intervalLongIos],
  163. c1.value('(event/data[@name="data"]/value/ioSubsystem/@totalLongIos)[1]','int') as [totalLongIos],
  164. c1.value('(event/data[@name="data"]/value/ioSubsystem/longestPendingRequests/pendingRequest[1]/@duration)[1]','bigint') as [longestPendingRequests_duration],
  165. c1.value('(event/data[@name="data"]/value/ioSubsystem/longestPendingRequests/pendingRequest[1]/@filePath)[1]','nvarchar(500)') as [longestPendingRequests_filePath],
  166. c1.value('(event/data[@name="data"]/value/ioSubsystem/longestPendingRequests/pendingRequest[1]/@offset)[1]','bigint') as [longestPendingRequests_offset],
  167. c1.value('(event/data[@name="data"]/value/ioSubsystem/longestPendingRequests/pendingRequest[1]/@handle)[1]','nvarchar(20)') as [longestPendingRequests_handle]
  168. into tbl_IO_SUBSYSTEM
  169. FROM tbl_ServerDiagnostics
  170. where SdComponent = 'IO_SUBSYSTEM'
  171. GO
  172. /****** Object: StoredProcedure [dbo].[SpLoadQueryProcessing] Script Date: 1/25/2013 3:39:13 PM ******/
  173. SET ANSI_NULLS ON
  174. GO
  175. SET QUOTED_IDENTIFIER ON
  176. GO
  177. Create procedure [dbo].[SpLoadQueryProcessing]
  178. @UTDDateDiff int
  179. as
  180. if object_id('tbl_QUERY_PROCESSING') is not null
  181. drop table [tbl_QUERY_PROCESSING]
  182. select
  183. c1.value('(./event/@timestamp)[1]', 'datetime') as utctimestamp,
  184. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp],
  185. c1.value('(event/data[@name="component"]/text)[1]', 'varchar(100)') as [component_name],
  186. c1.value('(event/data[@name="state"]/text)[1]', 'varchar(100)') as [component_state],
  187. c1.value('(./event//data[@name="data"]/value/queryProcessing/@maxWorkers)[1]', 'int') as maxworkers,
  188. c1.value('(./event//data[@name="data"]/value/queryProcessing/@workersCreated)[1]', 'int') as workerscreated,
  189. c1.value('(./event//data[@name="data"]/value/queryProcessing/@tasksCompletedWithinInterval)[1]', 'int') as tasksCompletedWithinInterval,
  190. c1.value('(./event//data[@name="data"]/value/queryProcessing/@oldestPendingTaskWaitingTime)[1]', 'bigint') as oldestPendingTaskWaitingTime,
  191. c1.value('(./event//data[@name="data"]/value/queryProcessing/@pendingTasks)[1]', 'int') as pendingTasks,
  192. c1.value('(./event//data[@name="data"]/value/queryProcessing/@hasUnresolvableDeadlockOccurred)[1]', 'int') as hasUnresolvableDeadlockOccurred,
  193. c1.value('(./event//data[@name="data"]/value/queryProcessing/@hasDeadlockedSchedulersOccurred)[1]', 'int') as hasDeadlockedSchedulersOccurred,
  194. c1.value('(./event//data[@name="data"]/value/queryProcessing/@trackingNonYieldingScheduler)[1]', 'varchar(10)') as trackingNonYieldingScheduler
  195. into [tbl_QUERY_PROCESSING]
  196. from tblQryProcessingXmlOutput
  197. GO
  198. /****** Object: StoredProcedure [dbo].[SpLoadQueryProcessingComponent] Script Date: 1/25/2013 3:39:13 PM ******/
  199. SET ANSI_NULLS ON
  200. GO
  201. SET QUOTED_IDENTIFIER ON
  202. GO
  203. Create procedure [dbo].[SpLoadQueryProcessingComponent]
  204. @UTDDateDiff int
  205. as
  206. -- Import the XML
  207. If object_id('tblQryProcessingXmlOutput') is not null
  208. drop table tblQryProcessingXmlOutput
  209. CREATE TABLE [dbo].[tblQryProcessingXmlOutput](
  210. [c1] [xml] NULL
  211. ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
  212. insert into tblQryProcessingXmlOutput (c1)
  213. select c1 as snodes
  214. FROM tbl_ServerDiagnostics
  215. where SdComponent = 'QUERY_PROCESSING'
  216. -- Call individual Pieces
  217. exec SpLoadQueryProcessingComponent_TopWaits @UTDDateDiff
  218. exec SpLoadQueryProcessing @UTDDateDiff
  219. exec SpLoadQueryProcessingComponent_Blocking @UTDDateDiff
  220. --exec SpLoadQueryProcessingComponent_HighCPU @UTDDateDiff
  221. --exec SpLoadQueryProcessingComponent_QueryWaits @UTDDateDiff
  222. GO
  223. /****** Object: StoredProcedure [dbo].[SpLoadQueryProcessingComponent_Blocking] Script Date: 1/25/2013 3:39:13 PM ******/
  224. SET ANSI_NULLS ON
  225. GO
  226. SET QUOTED_IDENTIFIER ON
  227. GO
  228. Create procedure [dbo].[SpLoadQueryProcessingComponent_Blocking]
  229. @UTDDateDiff int
  230. as
  231. if object_id('tbl_BlockingXeOutput') is not null
  232. drop table tbl_BlockingXeOutput
  233. select
  234. [utctimestamp],[timestamp],
  235. c1.value('(blocked-process-report/blocking-process/process/inputbuf)[1]', 'nvarchar(max)') as [blocking_process],
  236. c1.value('(blocked-process-report/blocking-process/process[1]/@spid)[1]', 'int') as [blocking_process_id],
  237. c1.value('(blocked-process-report/blocking-process/process[1]/@ecid)[1]', 'int') as [blocking_process_ecid],
  238. c1.value('(blocked-process-report/blocking-process/process[1]/@status)[1]', 'varchar(100)') as [blocking_process_status],
  239. c1.value('(blocked-process-report/blocking-process/process[1]/@isolationlevel)[1]', 'varchar(200)') as [blocking_process_isolationlevel],
  240. c1.value('(blocked-process-report/blocking-process/process[1]/@lastbatchstarted)[1]', 'datetime') as [blocking_process_lastbatchstarted],
  241. c1.value('(blocked-process-report/blocking-process/process[1]/@lastbatchcompleted)[1]', 'datetime') as [blocking_process_lastbatchcompleted],
  242. c1.value('(blocked-process-report/blocking-process/process[1]/@lastattention)[1]', 'datetime') as [blocking_process_lastattention],
  243. c1.value('(blocked-process-report/blocking-process/process[1]/@trancount)[1]', 'int') as [blocking_process_trancount],
  244. c1.value('(blocked-process-report/blocking-process/process[1]/@xactid)[1]', 'bigint') as [blocking_process_xactid],
  245. c1.value('(/blocked-process-report/blocking-process/process[1]/@clientapp)[1]', 'nvarchar(100)') as [blocking_process_clientapp],
  246. c1.value('(/blocked-process-report/blocking-process/process[1]/@hostname)[1]', 'nvarchar(100)') as [blocking_process_hostname],
  247. c1.value('(/blocked-process-report/blocking-process/process[1]/@loginname)[1]', 'nvarchar(100)') as [blocking_process_loginname],
  248. c1.value('(/blocked-process-report/blocking-process/process[1]/@waitresource)[1]', 'nvarchar(200)') as [blocking_process_wait_resource],
  249. c1.value('(/blocked-process-report/blocked-process/process/inputbuf)[1]', 'nvarchar(max)') as [blocked_process],
  250. c1.value('(/blocked-process-report/blocked-process/process[1]/@spid)[1]', 'int') as [blocked_process_id],
  251. c1.value('(/blocked-process-report/blocked-process/process[1]/@ecid)[1]', 'int') as [blocked_process_ecid],
  252. c1.value('(/blocked-process-report/blocked-process/process[1]/@status)[1]', 'varchar(100)') as [blocked_process_status],
  253. c1.value('(/blocked-process-report/blocked-process/process[1]/@waitresource)[1]', 'nvarchar(200)') as [blocked_process_wait_resource],
  254. c1.value('(/blocked-process-report/blocked-process/process[1]/@lockMode)[1]', 'char(5)') as [blocked_process_lockMode],
  255. c1.value('(/blocked-process-report/blocked-process/process[1]/@waittime)[1]', 'nvarchar(200)') as [blocked_process_wait_time],
  256. c1.value('(/blocked-process-report/blocked-process/process[1]/@lastbatchstarted)[1]', 'datetime') as [blocked_process_lastbatchstarted],
  257. c1.value('(/blocked-process-report/blocked-process/process[1]/@lastbatchcompleted)[1]', 'datetime') as [blocked_process_lastbatchcompleted],
  258. c1.value('(/blocked-process-report/blocked-process/process[1]/@lastattention)[1]', 'datetime') as [blocked_process_lastattention],
  259. c1.value('(/blocked-process-report/blocked-process/process[1]/@clientapp)[1]', 'nvarchar(100)') as [blocked_process_clientapp],
  260. c1.value('(/value/blocked-process-report/blocked-process/process[1]/@hostname)[1]', 'nvarchar(100)') as [blocked_process_hostname],
  261. c1.value('(/blocked-process-report/blocked-process/process[1]/@loginname)[1]', 'nvarchar(100)') as [blocked_process_loginname]
  262. --T.bpnodes.value('(event/data[@name="blocked_process"]/value/blocked-process-report/blocking-process/process/executionstack/frame[1]/@sqlhandle)[1]', 'nvarchar(max)') as [blocking_process_sqlhandle]
  263. into tbl_BlockingXeOutput
  264. FROM
  265. (
  266. select c1.value('(event/@timestamp)[1]','datetime') as [utctimestamp]
  267. ,DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp]
  268. ,T.blk.query( '.') as c1 from tblQryProcessingXmlOutput
  269. CROSS APPLY c1.nodes('./event/data[@name="data"]/value/queryProcessing/blockingTasks/blocked-process-report') as T(blk)
  270. ) as T1
  271. GO
  272. /****** Object: StoredProcedure [dbo].[SpLoadQueryProcessingComponent_TopWaits] Script Date: 1/25/2013 3:39:13 PM ******/
  273. SET ANSI_NULLS ON
  274. GO
  275. SET QUOTED_IDENTIFIER ON
  276. GO
  277. Create procedure [dbo].[SpLoadQueryProcessingComponent_TopWaits]
  278. @UTDDateDiff int
  279. as
  280. if object_id('tbl_OS_WAIT_STATS_byDuration') is not null
  281. drop table tbl_OS_WAIT_STATS_byDuration
  282. CREATE TABLE [dbo].[tbl_OS_WAIT_STATS_byDuration](
  283. [UTCtimestamp] [datetime] NULL,
  284. [timestamp] [datetime] NULL,
  285. [wait_type] [varchar](47) NULL,
  286. [waiting_tasks_duration] [bigint] NULL,
  287. [avg_wait_time_ms] [bigint] NULL,
  288. [max_wait_time_ms] [bigint] NULL
  289. ) ON [PRIMARY]
  290. ALTER TABLE [dbo].[tbl_OS_WAIT_STATS_byDuration] ADD [wait_category] AS (case when [wait_type] like 'LCK%' then 'Locks' when [wait_type] like 'PAGEIO%' then 'Page I/O Latch' when [wait_type] like 'PAGELATCH%' then 'Page Latch (non-I/O)' when [wait_type] like 'LATCH%' then 'Latch (non-buffer)' when [wait_type] like 'IO_COMPLETION' then 'I/O Completion' when [wait_type] like 'ASYNC_NETWORK_IO' then 'Network I/O (client fetch)' when [wait_type]='CMEMTHREAD' OR [wait_type]='SOS_RESERVEDMEMBLOCKLIST' OR [wait_type]='RESOURCE_SEMAPHORE' then 'Memory' when [wait_type] like 'RESOURCE_SEMAPHORE_%' then 'Compilation' when [wait_type] like 'MSQL_XP' then 'XProc' when [wait_type] like 'WRITELOG' then 'Writelog' when [wait_type]='FT_IFTS_SCHEDULER_IDLE_WAIT' OR [wait_type]='WAITFOR' OR [wait_type]='EXECSYNC' OR [wait_type]='XE_TIMER_EVENT' OR [wait_type]='XE_DISPATCHER_WAIT' OR [wait_type]='WAITFOR_TASKSHUTDOWN' OR [wait_type]='WAIT_FOR_RESULTS' OR [wait_type]='SNI_HTTP_ACCEPT' OR [wait_type]='SLEEP_TEMPDBSTARTUP' OR [wait_type]='SLEEP_TASK' OR [wait_type]='SLEEP_SYSTEMTASK' OR [wait_type]='SLEEP_MSDBSTARTUP' OR [wait_type]='SLEEP_DCOMSTARTUP' OR [wait_type]='SLEEP_DBSTARTUP' OR [wait_type]='SLEEP_BPOOL_FLUSH' OR [wait_type]='SERVER_IDLE_CHECK' OR [wait_type]='RESOURCE_QUEUE' OR [wait_type]='REQUEST_FOR_DEADLOCK_SEARCH' OR [wait_type]='ONDEMAND_TASK_QUEUE' OR [wait_type]='LOGMGR_QUEUE' OR [wait_type]='LAZYWRITER_SLEEP' OR [wait_type]='KSOURCE_WAKEUP' OR [wait_type]='FSAGENT' OR [wait_type]='CLR_MANUAL_EVENT' OR [wait_type]='CLR_AUTO_EVENT' OR [wait_type]='CHKPT' OR [wait_type]='CHECKPOINT_QUEUE' OR [wait_type]='BROKER_TO_FLUSH' OR [wait_type]='BROKER_TASK_STOP' OR [wait_type]='BROKER_TRANSMITTER' OR [wait_type]='BROKER_RECEIVE_WAITFOR' OR [wait_type]='BROKER_EVENTHANDLER' OR [wait_type]='DBMIRROR_EVENTS_QUEUE' OR [wait_type]='DBMIRROR_DBM_EVENT' OR [wait_type]='DBMIRRORING_CMD' OR [wait_type]='DBMIRROR_WORKER_QUEUE' then 'IGNORABLE' else [wait_type] end)
  291. Create clustered index [tbl_OS_WAIT_STATS_byDuration_Clus] on [tbl_OS_WAIT_STATS_byDuration](timestamp)
  292. INSERT INTO [dbo].[tbl_OS_WAIT_STATS_byDuration]
  293. ([UTCtimestamp]
  294. ,[timestamp]
  295. ,[wait_type]
  296. ,[waiting_tasks_duration]
  297. ,[avg_wait_time_ms]
  298. ,[max_wait_time_ms])
  299. select
  300. c1.value('(event/@timestamp)[1]','datetime') as [UTCtimestamp],
  301. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp],
  302. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byDuration/wait[1]/@waitType)[1]','varchar(47)') as [waitType],
  303. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byDuration/wait[1]/@waits)[1]','bigint') as [waits],
  304. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byDuration/wait[1]/@averageWaitTime)[1]','bigint') as [averageWaitTime],
  305. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byDuration/wait[1]/@maxWaitTime)[1]','bigint') as [maxWaitTime]
  306. FROM tblQryProcessingXmlOutput
  307. UNION ALL
  308. select
  309. c1.value('(event/@timestamp)[1]','datetime') as [UTCtimestamp],
  310. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp],
  311. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byDuration/wait[2]/@waitType)[1]','varchar(47)') as [waitType],
  312. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byDuration/wait[2]/@waits)[1]','bigint') as [waits],
  313. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byDuration/wait[2]/@averageWaitTime)[1]','bigint') as [averageWaitTime],
  314. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byDuration/wait[2]/@maxWaitTime)[1]','bigint') as [maxWaitTime]
  315. FROM tblQryProcessingXmlOutput
  316. UNION ALL
  317. select
  318. c1.value('(event/@timestamp)[1]','datetime') as [UTCtimestamp],
  319. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp],
  320. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byDuration/wait[3]/@waitType)[1]','varchar(47)') as [waitType],
  321. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byDuration/wait[3]/@waits)[1]','bigint') as [waits],
  322. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byDuration/wait[3]/@averageWaitTime)[1]','bigint') as [averageWaitTime],
  323. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byDuration/wait[3]/@maxWaitTime)[1]','bigint') as [maxWaitTime]
  324. FROM tblQryProcessingXmlOutput
  325. UNION ALL
  326. select
  327. c1.value('(event/@timestamp)[1]','datetime') as [UTCtimestamp],
  328. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp],
  329. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byDuration/wait[4]/@waitType)[1]','varchar(47)') as [waitType],
  330. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byDuration/wait[4]/@waits)[1]','bigint') as [waits],
  331. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byDuration/wait[4]/@averageWaitTime)[1]','bigint') as [averageWaitTime],
  332. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byDuration/wait[4]/@maxWaitTime)[1]','bigint') as [maxWaitTime]
  333. FROM tblQryProcessingXmlOutput
  334. UNION ALL
  335. select
  336. c1.value('(event/@timestamp)[1]','datetime') as [UTCtimestamp],
  337. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp],
  338. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byDuration/wait[5]/@waitType)[1]','varchar(47)') as [waitType],
  339. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byDuration/wait[5]/@waits)[1]','bigint') as [waits],
  340. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byDuration/wait[5]/@averageWaitTime)[1]','bigint') as [averageWaitTime],
  341. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byDuration/wait[5]/@maxWaitTime)[1]','bigint') as [maxWaitTime]
  342. FROM tblQryProcessingXmlOutput
  343. UNION ALL
  344. select
  345. c1.value('(event/@timestamp)[1]','datetime') as [UTCtimestamp],
  346. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp],
  347. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byDuration/wait[6]/@waitType)[1]','varchar(47)') as [waitType],
  348. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byDuration/wait[6]/@waits)[1]','bigint') as [waits],
  349. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byDuration/wait[6]/@averageWaitTime)[1]','bigint') as [averageWaitTime],
  350. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byDuration/wait[6]/@maxWaitTime)[1]','bigint') as [maxWaitTime]
  351. FROM tblQryProcessingXmlOutput
  352. UNION ALL
  353. select
  354. c1.value('(event/@timestamp)[1]','datetime') as [UTCtimestamp],
  355. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp],
  356. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byDuration/wait[7]/@waitType)[1]','varchar(47)') as [waitType],
  357. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byDuration/wait[7]/@waits)[1]','bigint') as [waits],
  358. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byDuration/wait[7]/@averageWaitTime)[1]','bigint') as [averageWaitTime],
  359. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byDuration/wait[7]/@maxWaitTime)[1]','bigint') as [maxWaitTime]
  360. FROM tblQryProcessingXmlOutput
  361. UNION ALL
  362. select
  363. c1.value('(event/@timestamp)[1]','datetime') as [UTCtimestamp],
  364. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp],
  365. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byDuration/wait[8]/@waitType)[1]','varchar(47)') as [waitType],
  366. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byDuration/wait[8]/@waits)[1]','bigint') as [waits],
  367. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byDuration/wait[8]/@averageWaitTime)[1]','bigint') as [averageWaitTime],
  368. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byDuration/wait[8]/@maxWaitTime)[1]','bigint') as [maxWaitTime]
  369. FROM tblQryProcessingXmlOutput
  370. UNION ALL
  371. select
  372. c1.value('(event/@timestamp)[1]','datetime') as [UTCtimestamp],
  373. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp],
  374. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byDuration/wait[9]/@waitType)[1]','varchar(47)') as [waitType],
  375. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byDuration/wait[9]/@waits)[1]','bigint') as [waits],
  376. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byDuration/wait[9]/@averageWaitTime)[1]','bigint') as [averageWaitTime],
  377. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byDuration/wait[9]/@maxWaitTime)[1]','bigint') as [maxWaitTime]
  378. FROM tblQryProcessingXmlOutput
  379. UNION ALL
  380. select
  381. c1.value('(event/@timestamp)[1]','datetime') as [UTCtimestamp],
  382. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp],
  383. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byDuration/wait[10]/@waitType)[1]','varchar(47)') as [waitType],
  384. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byDuration/wait[10]/@waits)[1]','bigint') as [waits],
  385. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byDuration/wait[10]/@averageWaitTime)[1]','bigint') as [averageWaitTime],
  386. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byDuration/wait[10]/@maxWaitTime)[1]','bigint') as [maxWaitTime]
  387. FROM tblQryProcessingXmlOutput
  388. UNION ALL
  389. select
  390. c1.value('(event/@timestamp)[1]','datetime') as [UTCtimestamp],
  391. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp],
  392. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byDuration/wait[1]/@waitType)[1]','varchar(47)') as [waitType],
  393. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byDuration/wait[1]/@waits)[1]','bigint') as [waits],
  394. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byDuration/wait[1]/@averageWaitTime)[1]','bigint') as [averageWaitTime],
  395. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byDuration/wait[1]/@maxWaitTime)[1]','bigint') as [maxWaitTime]
  396. FROM tblQryProcessingXmlOutput
  397. UNION ALL
  398. select
  399. c1.value('(event/@timestamp)[1]','datetime') as [UTCtimestamp],
  400. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp],
  401. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byDuration/wait[2]/@waitType)[1]','varchar(47)') as [waitType],
  402. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byDuration/wait[2]/@waits)[1]','bigint') as [waits],
  403. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byDuration/wait[2]/@averageWaitTime)[1]','bigint') as [averageWaitTime],
  404. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byDuration/wait[2]/@maxWaitTime)[1]','bigint') as [maxWaitTime]
  405. FROM tblQryProcessingXmlOutput
  406. UNION ALL
  407. select
  408. c1.value('(event/@timestamp)[1]','datetime') as [UTCtimestamp],
  409. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp],
  410. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byDuration/wait[3]/@waitType)[1]','varchar(47)') as [waitType],
  411. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byDuration/wait[3]/@waits)[1]','bigint') as [waits],
  412. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byDuration/wait[3]/@averageWaitTime)[1]','bigint') as [averageWaitTime],
  413. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byDuration/wait[3]/@maxWaitTime)[1]','bigint') as [maxWaitTime]
  414. FROM tblQryProcessingXmlOutput
  415. UNION ALL
  416. select
  417. c1.value('(event/@timestamp)[1]','datetime') as [UTCtimestamp],
  418. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp],
  419. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byDuration/wait[4]/@waitType)[1]','varchar(47)') as [waitType],
  420. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byDuration/wait[4]/@waits)[1]','bigint') as [waits],
  421. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byDuration/wait[4]/@averageWaitTime)[1]','bigint') as [averageWaitTime],
  422. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byDuration/wait[4]/@maxWaitTime)[1]','bigint') as [maxWaitTime]
  423. FROM tblQryProcessingXmlOutput
  424. UNION ALL
  425. select
  426. c1.value('(event/@timestamp)[1]','datetime') as [UTCtimestamp],
  427. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp],
  428. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byDuration/wait[5]/@waitType)[1]','varchar(47)') as [waitType],
  429. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byDuration/wait[5]/@waits)[1]','bigint') as [waits],
  430. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byDuration/wait[5]/@averageWaitTime)[1]','bigint') as [averageWaitTime],
  431. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byDuration/wait[5]/@maxWaitTime)[1]','bigint') as [maxWaitTime]
  432. FROM tblQryProcessingXmlOutput
  433. UNION ALL
  434. select
  435. c1.value('(event/@timestamp)[1]','datetime') as [UTCtimestamp],
  436. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp],
  437. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byDuration/wait[6]/@waitType)[1]','varchar(47)') as [waitType],
  438. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byDuration/wait[6]/@waits)[1]','bigint') as [waits],
  439. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byDuration/wait[6]/@averageWaitTime)[1]','bigint') as [averageWaitTime],
  440. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byDuration/wait[6]/@maxWaitTime)[1]','bigint') as [maxWaitTime]
  441. FROM tblQryProcessingXmlOutput
  442. UNION ALL
  443. select
  444. c1.value('(event/@timestamp)[1]','datetime') as [UTCtimestamp],
  445. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp],
  446. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byDuration/wait[7]/@waitType)[1]','varchar(47)') as [waitType],
  447. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byDuration/wait[7]/@waits)[1]','bigint') as [waits],
  448. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byDuration/wait[7]/@averageWaitTime)[1]','bigint') as [averageWaitTime],
  449. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byDuration/wait[7]/@maxWaitTime)[1]','bigint') as [maxWaitTime]
  450. FROM tblQryProcessingXmlOutput
  451. UNION ALL
  452. select
  453. c1.value('(event/@timestamp)[1]','datetime') as [UTCtimestamp],
  454. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp],
  455. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byDuration/wait[8]/@waitType)[1]','varchar(47)') as [waitType],
  456. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byDuration/wait[8]/@waits)[1]','bigint') as [waits],
  457. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byDuration/wait[8]/@averageWaitTime)[1]','bigint') as [averageWaitTime],
  458. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byDuration/wait[8]/@maxWaitTime)[1]','bigint') as [maxWaitTime]
  459. FROM tblQryProcessingXmlOutput
  460. UNION ALL
  461. select
  462. c1.value('(event/@timestamp)[1]','datetime') as [UTCtimestamp],
  463. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp],
  464. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byDuration/wait[9]/@waitType)[1]','varchar(47)') as [waitType],
  465. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byDuration/wait[9]/@waits)[1]','bigint') as [waits],
  466. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byDuration/wait[9]/@averageWaitTime)[1]','bigint') as [averageWaitTime],
  467. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byDuration/wait[9]/@maxWaitTime)[1]','bigint') as [maxWaitTime]
  468. FROM tblQryProcessingXmlOutput
  469. UNION ALL
  470. select
  471. c1.value('(event/@timestamp)[1]','datetime') as [UTCtimestamp],
  472. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp],
  473. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byDuration/wait[10]/@waitType)[1]','varchar(47)') as [waitType],
  474. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byDuration/wait[10]/@waits)[1]','bigint') as [waits],
  475. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byDuration/wait[10]/@averageWaitTime)[1]','bigint') as [averageWaitTime],
  476. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byDuration/wait[10]/@maxWaitTime)[1]','bigint') as [maxWaitTime]
  477. FROM tblQryProcessingXmlOutput
  478. CREATE TABLE [dbo].[tbl_OS_WAIT_STATS_byCount](
  479. [UTCtimestamp] [datetime] NULL,
  480. [timestamp] [datetime] NULL,
  481. [wait_type] [varchar](47) NULL,
  482. [waiting_tasks_count] [bigint] NULL,
  483. [avg_wait_time_ms] [bigint] NULL,
  484. [max_wait_time_ms] [bigint] NULL
  485. ) ON [PRIMARY]
  486. ALTER TABLE [dbo].[tbl_OS_WAIT_STATS_byCount] ADD [wait_category] AS (case when [wait_type] like 'LCK%' then 'Locks' when [wait_type] like 'PAGEIO%' then 'Page I/O Latch' when [wait_type] like 'PAGELATCH%' then 'Page Latch (non-I/O)' when [wait_type] like 'LATCH%' then 'Latch (non-buffer)' when [wait_type] like 'IO_COMPLETION' then 'I/O Completion' when [wait_type] like 'ASYNC_NETWORK_IO' then 'Network I/O (client fetch)' when [wait_type]='CMEMTHREAD' OR [wait_type]='SOS_RESERVEDMEMBLOCKLIST' OR [wait_type]='RESOURCE_SEMAPHORE' then 'Memory' when [wait_type] like 'RESOURCE_SEMAPHORE_%' then 'Compilation' when [wait_type] like 'MSQL_XP' then 'XProc' when [wait_type] like 'WRITELOG' then 'Writelog' when [wait_type]='FT_IFTS_SCHEDULER_IDLE_WAIT' OR [wait_type]='WAITFOR' OR [wait_type]='EXECSYNC' OR [wait_type]='XE_TIMER_EVENT' OR [wait_type]='XE_DISPATCHER_WAIT' OR [wait_type]='WAITFOR_TASKSHUTDOWN' OR [wait_type]='WAIT_FOR_RESULTS' OR [wait_type]='SNI_HTTP_ACCEPT' OR [wait_type]='SLEEP_TEMPDBSTARTUP' OR [wait_type]='SLEEP_TASK' OR [wait_type]='SLEEP_SYSTEMTASK' OR [wait_type]='SLEEP_MSDBSTARTUP' OR [wait_type]='SLEEP_DCOMSTARTUP' OR [wait_type]='SLEEP_DBSTARTUP' OR [wait_type]='SLEEP_BPOOL_FLUSH' OR [wait_type]='SERVER_IDLE_CHECK' OR [wait_type]='RESOURCE_QUEUE' OR [wait_type]='REQUEST_FOR_DEADLOCK_SEARCH' OR [wait_type]='ONDEMAND_TASK_QUEUE' OR [wait_type]='LOGMGR_QUEUE' OR [wait_type]='LAZYWRITER_SLEEP' OR [wait_type]='KSOURCE_WAKEUP' OR [wait_type]='FSAGENT' OR [wait_type]='CLR_MANUAL_EVENT' OR [wait_type]='CLR_AUTO_EVENT' OR [wait_type]='CHKPT' OR [wait_type]='CHECKPOINT_QUEUE' OR [wait_type]='BROKER_TO_FLUSH' OR [wait_type]='BROKER_TASK_STOP' OR [wait_type]='BROKER_TRANSMITTER' OR [wait_type]='BROKER_RECEIVE_WAITFOR' OR [wait_type]='BROKER_EVENTHANDLER' OR [wait_type]='DBMIRROR_EVENTS_QUEUE' OR [wait_type]='DBMIRROR_DBM_EVENT' OR [wait_type]='DBMIRRORING_CMD' OR [wait_type]='DBMIRROR_WORKER_QUEUE' then 'IGNORABLE' else [wait_type] end)
  487. Create clustered index [tbl_OS_WAIT_STATS_byCount_Clus] on [tbl_OS_WAIT_STATS_byCount](timestamp)
  488. INSERT INTO [dbo].[tbl_OS_WAIT_STATS_byCount]
  489. ([UTCtimestamp]
  490. ,[timestamp]
  491. ,[wait_type]
  492. ,[waiting_tasks_count]
  493. ,[avg_wait_time_ms]
  494. ,[max_wait_time_ms])
  495. select
  496. c1.value('(event/@timestamp)[1]','datetime') as [UTCtimestamp],
  497. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp],
  498. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byCount/wait[1]/@waitType)[1]','varchar(47)') as [waitType],
  499. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byCount/wait[1]/@waits)[1]','bigint') as [waits],
  500. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byCount/wait[1]/@averageWaitTime)[1]','bigint') as [averageWaitTime],
  501. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byCount/wait[1]/@maxWaitTime)[1]','bigint') as [maxWaitTime]
  502. FROM tblQryProcessingXmlOutput
  503. UNION ALL
  504. select
  505. c1.value('(event/@timestamp)[1]','datetime') as [UTCtimestamp],
  506. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp],
  507. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byCount/wait[2]/@waitType)[1]','varchar(47)') as [waitType],
  508. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byCount/wait[2]/@waits)[1]','bigint') as [waits],
  509. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byCount/wait[2]/@averageWaitTime)[1]','bigint') as [averageWaitTime],
  510. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byCount/wait[2]/@maxWaitTime)[1]','bigint') as [maxWaitTime]
  511. FROM tblQryProcessingXmlOutput
  512. UNION ALL
  513. select
  514. c1.value('(event/@timestamp)[1]','datetime') as [UTCtimestamp],
  515. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp],
  516. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byCount/wait[3]/@waitType)[1]','varchar(47)') as [waitType],
  517. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byCount/wait[3]/@waits)[1]','bigint') as [waits],
  518. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byCount/wait[3]/@averageWaitTime)[1]','bigint') as [averageWaitTime],
  519. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byCount/wait[3]/@maxWaitTime)[1]','bigint') as [maxWaitTime]
  520. FROM tblQryProcessingXmlOutput
  521. UNION ALL
  522. select
  523. c1.value('(event/@timestamp)[1]','datetime') as [UTCtimestamp],
  524. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp],
  525. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byCount/wait[4]/@waitType)[1]','varchar(47)') as [waitType],
  526. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byCount/wait[4]/@waits)[1]','bigint') as [waits],
  527. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byCount/wait[4]/@averageWaitTime)[1]','bigint') as [averageWaitTime],
  528. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byCount/wait[4]/@maxWaitTime)[1]','bigint') as [maxWaitTime]
  529. FROM tblQryProcessingXmlOutput
  530. UNION ALL
  531. select
  532. c1.value('(event/@timestamp)[1]','datetime') as [UTCtimestamp],
  533. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp],
  534. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byCount/wait[5]/@waitType)[1]','varchar(47)') as [waitType],
  535. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byCount/wait[5]/@waits)[1]','bigint') as [waits],
  536. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byCount/wait[5]/@averageWaitTime)[1]','bigint') as [averageWaitTime],
  537. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byCount/wait[5]/@maxWaitTime)[1]','bigint') as [maxWaitTime]
  538. FROM tblQryProcessingXmlOutput
  539. UNION ALL
  540. select
  541. c1.value('(event/@timestamp)[1]','datetime') as [UTCtimestamp],
  542. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp],
  543. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byCount/wait[6]/@waitType)[1]','varchar(47)') as [waitType],
  544. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byCount/wait[6]/@waits)[1]','bigint') as [waits],
  545. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byCount/wait[6]/@averageWaitTime)[1]','bigint') as [averageWaitTime],
  546. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byCount/wait[6]/@maxWaitTime)[1]','bigint') as [maxWaitTime]
  547. FROM tblQryProcessingXmlOutput
  548. UNION ALL
  549. select
  550. c1.value('(event/@timestamp)[1]','datetime') as [UTCtimestamp],
  551. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp],
  552. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byCount/wait[7]/@waitType)[1]','varchar(47)') as [waitType],
  553. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byCount/wait[7]/@waits)[1]','bigint') as [waits],
  554. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byCount/wait[7]/@averageWaitTime)[1]','bigint') as [averageWaitTime],
  555. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byCount/wait[7]/@maxWaitTime)[1]','bigint') as [maxWaitTime]
  556. FROM tblQryProcessingXmlOutput
  557. UNION ALL
  558. select
  559. c1.value('(event/@timestamp)[1]','datetime') as [UTCtimestamp],
  560. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp],
  561. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byCount/wait[8]/@waitType)[1]','varchar(47)') as [waitType],
  562. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byCount/wait[8]/@waits)[1]','bigint') as [waits],
  563. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byCount/wait[8]/@averageWaitTime)[1]','bigint') as [averageWaitTime],
  564. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byCount/wait[8]/@maxWaitTime)[1]','bigint') as [maxWaitTime]
  565. FROM tblQryProcessingXmlOutput
  566. UNION ALL
  567. select
  568. c1.value('(event/@timestamp)[1]','datetime') as [UTCtimestamp],
  569. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp],
  570. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byCount/wait[9]/@waitType)[1]','varchar(47)') as [waitType],
  571. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byCount/wait[9]/@waits)[1]','bigint') as [waits],
  572. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byCount/wait[9]/@averageWaitTime)[1]','bigint') as [averageWaitTime],
  573. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byCount/wait[9]/@maxWaitTime)[1]','bigint') as [maxWaitTime]
  574. FROM tblQryProcessingXmlOutput
  575. UNION ALL
  576. select
  577. c1.value('(event/@timestamp)[1]','datetime') as [UTCtimestamp],
  578. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp],
  579. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byCount/wait[10]/@waitType)[1]','varchar(47)') as [waitType],
  580. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byCount/wait[10]/@waits)[1]','bigint') as [waits],
  581. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byCount/wait[10]/@averageWaitTime)[1]','bigint') as [averageWaitTime],
  582. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/nonPreemptive/byCount/wait[10]/@maxWaitTime)[1]','bigint') as [maxWaitTime]
  583. FROM tblQryProcessingXmlOutput
  584. UNION ALL
  585. select
  586. c1.value('(event/@timestamp)[1]','datetime') as [UTCtimestamp],
  587. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp],
  588. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byCount/wait[1]/@waitType)[1]','varchar(47)') as [waitType],
  589. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byCount/wait[1]/@waits)[1]','bigint') as [waits],
  590. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byCount/wait[1]/@averageWaitTime)[1]','bigint') as [averageWaitTime],
  591. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byCount/wait[1]/@maxWaitTime)[1]','bigint') as [maxWaitTime]
  592. FROM tblQryProcessingXmlOutput
  593. UNION ALL
  594. select
  595. c1.value('(event/@timestamp)[1]','datetime') as [UTCtimestamp],
  596. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp],
  597. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byCount/wait[2]/@waitType)[1]','varchar(47)') as [waitType],
  598. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byCount/wait[2]/@waits)[1]','bigint') as [waits],
  599. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byCount/wait[2]/@averageWaitTime)[1]','bigint') as [averageWaitTime],
  600. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byCount/wait[2]/@maxWaitTime)[1]','bigint') as [maxWaitTime]
  601. FROM tblQryProcessingXmlOutput
  602. UNION ALL
  603. select
  604. c1.value('(event/@timestamp)[1]','datetime') as [UTCtimestamp],
  605. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp],
  606. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byCount/wait[3]/@waitType)[1]','varchar(47)') as [waitType],
  607. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byCount/wait[3]/@waits)[1]','bigint') as [waits],
  608. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byCount/wait[3]/@averageWaitTime)[1]','bigint') as [averageWaitTime],
  609. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byCount/wait[3]/@maxWaitTime)[1]','bigint') as [maxWaitTime]
  610. FROM tblQryProcessingXmlOutput
  611. UNION ALL
  612. select
  613. c1.value('(event/@timestamp)[1]','datetime') as [UTCtimestamp],
  614. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp],
  615. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byCount/wait[4]/@waitType)[1]','varchar(47)') as [waitType],
  616. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byCount/wait[4]/@waits)[1]','bigint') as [waits],
  617. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byCount/wait[4]/@averageWaitTime)[1]','bigint') as [averageWaitTime],
  618. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byCount/wait[4]/@maxWaitTime)[1]','bigint') as [maxWaitTime]
  619. FROM tblQryProcessingXmlOutput
  620. UNION ALL
  621. select
  622. c1.value('(event/@timestamp)[1]','datetime') as [UTCtimestamp],
  623. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp],
  624. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byCount/wait[5]/@waitType)[1]','varchar(47)') as [waitType],
  625. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byCount/wait[5]/@waits)[1]','bigint') as [waits],
  626. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byCount/wait[5]/@averageWaitTime)[1]','bigint') as [averageWaitTime],
  627. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byCount/wait[5]/@maxWaitTime)[1]','bigint') as [maxWaitTime]
  628. FROM tblQryProcessingXmlOutput
  629. UNION ALL
  630. select
  631. c1.value('(event/@timestamp)[1]','datetime') as [UTCtimestamp],
  632. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp],
  633. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byCount/wait[6]/@waitType)[1]','varchar(47)') as [waitType],
  634. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byCount/wait[6]/@waits)[1]','bigint') as [waits],
  635. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byCount/wait[6]/@averageWaitTime)[1]','bigint') as [averageWaitTime],
  636. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byCount/wait[6]/@maxWaitTime)[1]','bigint') as [maxWaitTime]
  637. FROM tblQryProcessingXmlOutput
  638. UNION ALL
  639. select
  640. c1.value('(event/@timestamp)[1]','datetime') as [UTCtimestamp],
  641. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp],
  642. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byCount/wait[7]/@waitType)[1]','varchar(47)') as [waitType],
  643. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byCount/wait[7]/@waits)[1]','bigint') as [waits],
  644. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byCount/wait[7]/@averageWaitTime)[1]','bigint') as [averageWaitTime],
  645. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byCount/wait[7]/@maxWaitTime)[1]','bigint') as [maxWaitTime]
  646. FROM tblQryProcessingXmlOutput
  647. UNION ALL
  648. select
  649. c1.value('(event/@timestamp)[1]','datetime') as [UTCtimestamp],
  650. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp],
  651. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byCount/wait[8]/@waitType)[1]','varchar(47)') as [waitType],
  652. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byCount/wait[8]/@waits)[1]','bigint') as [waits],
  653. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byCount/wait[8]/@averageWaitTime)[1]','bigint') as [averageWaitTime],
  654. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byCount/wait[8]/@maxWaitTime)[1]','bigint') as [maxWaitTime]
  655. FROM tblQryProcessingXmlOutput
  656. UNION ALL
  657. select
  658. c1.value('(event/@timestamp)[1]','datetime') as [UTCtimestamp],
  659. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp],
  660. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byCount/wait[9]/@waitType)[1]','varchar(47)') as [waitType],
  661. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byCount/wait[9]/@waits)[1]','bigint') as [waits],
  662. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byCount/wait[9]/@averageWaitTime)[1]','bigint') as [averageWaitTime],
  663. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byCount/wait[9]/@maxWaitTime)[1]','bigint') as [maxWaitTime]
  664. FROM tblQryProcessingXmlOutput
  665. UNION ALL
  666. select
  667. c1.value('(event/@timestamp)[1]','datetime') as [UTCtimestamp],
  668. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp],
  669. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byCount/wait[10]/@waitType)[1]','varchar(47)') as [waitType],
  670. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byCount/wait[10]/@waits)[1]','bigint') as [waits],
  671. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byCount/wait[10]/@averageWaitTime)[1]','bigint') as [averageWaitTime],
  672. c1.value('(event/data[@name="data"]/value/queryProcessing/topWaits/preemptive/byCount/wait[10]/@maxWaitTime)[1]','bigint') as [maxWaitTime]
  673. FROM tblQryProcessingXmlOutput
  674. GO
  675. /****** Object: StoredProcedure [dbo].[SpLoadResourceComponent] Script Date: 1/25/2013 3:39:13 PM ******/
  676. SET ANSI_NULLS ON
  677. GO
  678. SET QUOTED_IDENTIFIER ON
  679. GO
  680. Create procedure [dbo].[SpLoadResourceComponent]
  681. @UTDDateDiff int
  682. as
  683. if object_id('tbl_Resource') is not null
  684. drop table tbl_Resource
  685. select
  686. c1.value('(./event/@timestamp)[1]', 'datetime') as UTCtimestamp,
  687. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp],
  688. c1.value('(./event//data[@name="state"]/text)[1]', 'varchar(20)') as State,
  689. c1.value('(./event//data[@name="data"]/value/resource/@lastNotification)[1]', 'nvarchar(100)') as lastNotification,
  690. c1.value('(./event//data[@name="data"]/value/resource/@outOfMemoryExceptions)[1]', 'tinyint') as outOfMemoryExceptions,
  691. c1.value('(./event//data[@name="data"]/value/resource/@isAnyPoolOutOfMemory)[1]', 'tinyint') as isAnyPoolOutOfMemory,
  692. c1.value('(./event//data[@name="data"]/value/resource/@processOutOfMemoryPeriod)[1]', 'tinyint') as processOutOfMemoryPeriod,
  693. c1.value('(./event//data[@name="data"]/value/resource/memoryReport[@name="Process/System Counts"]/entry[@description="Available Physical Memory"]/@value)[1]', 'bigint') as available_physical_memory,
  694. c1.value('(./event//data[@name="data"]/value/resource/memoryReport[@name="Process/System Counts"]/entry[@description="Available Virtual Memory"]/@value)[1]', 'bigint') as available_virtual_memory,
  695. c1.value('(./event//data[@name="data"]/value/resource/memoryReport[@name="Process/System Counts"]/entry[@description="Available Paging File"]/@value)[1]', 'bigint') as available_paging_file,
  696. c1.value('(./event//data[@name="data"]/value/resource/memoryReport[@name="Process/System Counts"]/entry[@description="Working Set"]/@value)[1]', 'bigint') as working_set,
  697. c1.value('(./event//data[@name="data"]/value/resource/memoryReport[@name="Process/System Counts"]/entry[@description="Percent of Committed Memory in WS"]/@value)[1]', 'bigint') as percent_workingset_committed,
  698. c1.value('(./event//data[@name="data"]/value/resource/memoryReport[@name="Process/System Counts"]/entry[@description="Page Faults"]/@value)[1]', 'bigint') as page_faults,
  699. c1.value('(./event//data[@name="data"]/value/resource/memoryReport[@name="Process/System Counts"]/entry[@description="System physical memory high"]/@value)[1]', 'bigint') as sys_physical_memory_high,
  700. c1.value('(./event//data[@name="data"]/value/resource/memoryReport[@name="Process/System Counts"]/entry[@description="System physical memory low"]/@value)[1]', 'bigint') as sys_physical_memory_low,
  701. c1.value('(./event//data[@name="data"]/value/resource/memoryReport[@name="Process/System Counts"]/entry[@description="Process physical memory low"]/@value)[1]', 'bigint') as process_phyiscal_memory_low,
  702. c1.value('(./event//data[@name="data"]/value/resource/memoryReport[@name="Process/System Counts"]/entry[@description="Process virtual memory low"]/@value)[1]', 'bigint') as process_virtual_memory_low,
  703. c1.value('(./event//data[@name="data"]/value/resource/memoryReport[@name="Memory Manager"]/entry[@description="VM Reserved"]/@value)[1]', 'bigint') as vm_reserved_kb,
  704. c1.value('(./event//data[@name="data"]/value/resource/memoryReport[@name="Memory Manager"]/entry[@description="VM Committed"]/@value)[1]', 'bigint') as vm_committed_kb,
  705. c1.value('(./event//data[@name="data"]/value/resource/memoryReport[@name="Memory Manager"]/entry[@description="Locked Pages Allocated"]/@value)[1]', 'bigint') as locked_pages_allocated_kb,
  706. c1.value('(./event//data[@name="data"]/value/resource/memoryReport[@name="Memory Manager"]/entry[@description="Large Pages Allocated"]/@value)[1]', 'bigint') as large_pages_allocated_kb,
  707. c1.value('(./event//data[@name="data"]/value/resource/memoryReport[@name="Memory Manager"]/entry[@description="Target Committed"]/@value)[1]', 'bigint') as target_committed_kb,
  708. c1.value('(./event//data[@name="data"]/value/resource/memoryReport[@name="Memory Manager"]/entry[@description="Current Committed"]/@value)[1]', 'bigint') as current_committed_kb,
  709. c1.value('(./event//data[@name="data"]/value/resource/memoryReport[@name="Memory Manager"]/entry[@description="Pages Allocated"]/@value)[1]', 'bigint') as Pages_allocated_kb,
  710. c1.value('(./event//data[@name="data"]/value/resource/memoryReport[@name="Memory Manager"]/entry[@description="Pages Reserved"]/@value)[1]', 'bigint') as pages_reserved_kb,
  711. c1.value('(./event//data[@name="data"]/value/resource/memoryReport[@name="Memory Manager"]/entry[@description="Pages Free"]/@value)[1]', 'bigint') as pages_free_kb,
  712. c1.value('(./event//data[@name="data"]/value/resource/memoryReport[@name="Memory Manager"]/entry[@description="Pages In Use"]/@value)[1]', 'bigint') as pages_in_use_kb,
  713. c1.value('(./event//data[@name="data"]/value/resource/memoryReport[@name="Memory Manager"]/entry[@description="Page Alloc Potential"]/@value)[1]', 'bigint') as page_alloc_potential,
  714. c1.value('(./event//data[@name="data"]/value/resource/memoryReport[@name="Memory Manager"]/entry[@description="NUMA Growth Phase"]/@value)[1]', 'int') as numa_growth_phase,
  715. c1.value('(./event//data[@name="data"]/value/resource/memoryReport[@name="Memory Manager"]/entry[@description="Last OOM Factor"]/@value)[1]', 'int') as last_oom_factor,
  716. c1.value('(./event//data[@name="data"]/value/resource/memoryReport[@name="Memory Manager"]/entry[@description="Last OS Error"]/@value)[1]', 'int') as last_os_error
  717. into tbl_Resource
  718. FROM tbl_ServerDiagnostics
  719. where SdComponent = 'RESOURCE'
  720. GO
  721. /****** Object: StoredProcedure [dbo].[spLoadSchedulerMonitor] Script Date: 1/25/2013 3:39:13 PM ******/
  722. SET ANSI_NULLS ON
  723. GO
  724. SET QUOTED_IDENTIFIER ON
  725. GO
  726. Create Procedure [dbo].[spLoadSchedulerMonitor]
  727. @UTDDateDiff int
  728. as
  729. if object_id('tbl_scheduler_monitor') is not null
  730. drop table tbl_scheduler_monitor
  731. select
  732. c1.value('(./event/@timestamp)[1]', 'datetime') as UTCtimestamp,
  733. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp],
  734. c1.value('(./event/data[@name="id"])[1]', 'bigint') as [id],
  735. c1.value('(./event/data[@name="process_utilization"])[1]', 'int') as process_utilization,
  736. c1.value('(./event/data[@name="system_idle"])[1]', 'int') as system_idle,
  737. c1.value('(./event/data[@name="user_mode_time"])[1]', 'bigint') as user_mode_time,
  738. c1.value('(./event/data[@name="kernel_mode_time"])[1]', 'bigint') as kernel_mode_time,
  739. c1.value('(./event/data[@name="working_set_delta"])[1]', 'numeric(24,0)') as working_set_delta,
  740. c1.value('(./event/data[@name="memory_utilization"])[1]', 'int') as memory_utilization
  741. into tbl_scheduler_monitor
  742. from tbl_XEImport
  743. where object_name like 'scheduler_monitor_system_health_ring_buffer_recorded'
  744. GO
  745. /****** Object: StoredProcedure [dbo].[SpLoadSecurityRingBuffer] Script Date: 1/25/2013 3:39:13 PM ******/
  746. SET ANSI_NULLS ON
  747. GO
  748. SET QUOTED_IDENTIFIER ON
  749. GO
  750. Create procedure [dbo].[SpLoadSecurityRingBuffer]
  751. @UTDDateDiff int
  752. as
  753. if object_id('tbl_security_ring_buffer') is not null
  754. drop table [tbl_security_ring_buffer]
  755. select
  756. c1.value('(./event/@timestamp)[1]', 'datetime') as utctimestamp,
  757. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp],
  758. c1.value('(./event/data[@name="id"])[1]', 'bigint') as id,
  759. c1.value('(./event/data[@name="session_id"])[1]', 'int') as session_id,
  760. c1.value('(./event/data[@name="error_code"])[1]', 'bigint') as [error_code],
  761. c1.value('(./event/data[@name="api_name"])[1]', 'nvarchar(100)') as api_name,
  762. c1.value('(./event/data[@name="calling_api_name"])[1]', 'nvarchar(100)') as calling_api_name
  763. into [tbl_security_ring_buffer]
  764. from tbl_XEImport
  765. where object_name like 'security_error_ring_buffer_recorded'
  766. GO
  767. /****** Object: StoredProcedure [dbo].[SpLoadSYSTEMComponent] Script Date: 1/25/2013 3:39:13 PM ******/
  768. SET ANSI_NULLS ON
  769. GO
  770. SET QUOTED_IDENTIFIER ON
  771. GO
  772. Create procedure [dbo].[SpLoadSYSTEMComponent]
  773. @UTDDateDiff int
  774. as
  775. if object_id('tbl_SYSTEM') is not null
  776. drop table tbl_SYSTEM
  777. select
  778. c1.value('(event/@timestamp)[1]','datetime') as [UTCtimestamp],
  779. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as timestamp,
  780. c1.value('(event/data[@name="component"]/text)[1]', 'varchar(100)') as [component_name],
  781. c1.value('(event/data[@name="state"]/text)[1]', 'varchar(100)') as [component_state],
  782. c1.value('(event/data[@name="data"]/value/system[1]/@spinlockBackoffs)[1]', 'int') as [spinlockBackoffs],
  783. c1.value('(event/data[@name="data"]/value/system[1]/@sickSpinlockTypeAfterAv)[1]', 'varchar(100)') as [sickSpinlockTypeAfterAv],
  784. c1.value('(event/data[@name="data"]/value/system[1]/@latchWarnings)[1]', 'int') as [latchWarnings],
  785. c1.value('(event/data[@name="data"]/value/system[1]/@isAccessViolationOccurred)[1]', 'int') as [isAccessViolationOccurred],
  786. c1.value('(event/data[@name="data"]/value/system[1]/@writeAccessViolationCount)[1]', 'int') as [writeAccessViolationCount],
  787. c1.value('(event/data[@name="data"]/value/system[1]/@totalDumpRequests)[1]', 'int') as [totalDumpRequests],
  788. c1.value('(event/data[@name="data"]/value/system[1]/@intervalDumpRequests)[1]', 'int') as [intervalDumpRequests],
  789. c1.value('(event/data[@name="data"]/value/system[1]/@nonYieldingTasksReported)[1]', 'int') as [nonYieldingTasksReported],
  790. c1.value('(event/data[@name="data"]/value/system[1]/@pageFaults)[1]', 'bigint') as [pageFaults],
  791. c1.value('(event/data[@name="data"]/value/system[1]/@systemCpuUtilization)[1]', 'int') as [systemCpuUtilization],
  792. c1.value('(event/data[@name="data"]/value/system[1]/@sqlCpuUtilization)[1]', 'int') as [sqlCpuUtilization],
  793. c1.value('(event/data[@name="data"]/value/system[1]/@BadPagesDetected)[1]', 'int') as [BadPagesDetected],
  794. c1.value('(event/data[@name="data"]/value/system[1]/@BadPagesFixed)[1]', 'int') as [BadPagesFixed],
  795. c1.value('(event/data[@name="data"]/value/system[1]/@LastBadPageAddress)[1]', 'nvarchar(30)') as [LastBadPageAddress]
  796. into tbl_SYSTEM
  797. FROM tbl_ServerDiagnostics
  798. where SdComponent = 'SYSTEM'
  799. GO
  800. /****** Object: StoredProcedure [dbo].[spLoadWaitQueries] Script Date: 1/25/2013 3:39:13 PM ******/
  801. SET ANSI_NULLS ON
  802. GO
  803. SET QUOTED_IDENTIFIER ON
  804. GO
  805. Create procedure [dbo].[spLoadWaitQueries]
  806. @UTDDateDiff int
  807. as
  808. if object_id('tbl_waitqueries') is not null
  809. drop table tbl_waitqueries
  810. SELECT
  811. c1.value('(./event/@timestamp)[1]', 'datetime') as utctimestamp,
  812. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp],
  813. c1.value('(/event/data[@name="wait_type"]/text)[1]', 'varchar(50)') as WaitType,
  814. c1.value('(/event/data[@name="duration"]/value)[1]', 'bigint') as Duration,
  815. c1.value('(/event/data[@name="signal_duration"]/value)[1]', 'bigint') as signal_duration,
  816. c1.value('(/event/action[@name="session_id"]/value)[1]', 'int') as Session_ID,
  817. c1.value('(/event/action[@name="sql_text"]/value)[1]', 'varchar(max)') as sql_text
  818. into tbl_waitqueries
  819. FROM tbl_XEImport
  820. where object_name like 'wait_info%'
  821. GO
  822. /****** Object: StoredProcedure [dbo].[spLoadDeadlockReport] Script Date: 1/25/2013 3:39:13 PM ******/
  823. SET ANSI_NULLS ON
  824. GO
  825. SET QUOTED_IDENTIFIER ON
  826. GO
  827. Create procedure [dbo].[spLoadDeadlockReport]
  828. @UTDDateDiff int
  829. as
  830. if object_id('tbl_DeadlockReport') is not null
  831. drop table tbl_DeadlockReport
  832. SELECT
  833. c1.value('(./event/@timestamp)[1]', 'datetime') as utctimestamp,
  834. DATEADD(mi,@UTDDateDiff,c1.value('(./event/@timestamp)[1]', 'datetime')) as [timestamp]
  835. , *
  836. into tbl_DeadlockReport
  837. FROM tbl_XEImport
  838. where object_name like 'xml_deadlock_report'
  839. GO
  840. /****** Object: StoredProcedure [dbo].[spLoadSystemHealthSession] Script Date: 1/25/2013 3:39:13 PM ******/
  841. SET ANSI_NULLS ON
  842. GO
  843. SET QUOTED_IDENTIFIER ON
  844. GO
  845. Create procedure [dbo].[spLoadSystemHealthSession]
  846. @path_to_health_session nvarchar(4000) = NULL ,
  847. @UTDDateDiff int = 0
  848. as
  849. if object_id('tbl_ImportStatus') is not null
  850. drop table tbl_ImportStatus
  851. Create table tbl_ImportStatus
  852. ( StepName varchar(100),
  853. Status varchar(20),
  854. Starttime datetime
  855. )
  856. insert into tbl_ImportStatus Values('Load System Health Session','Processing',getdate())
  857. DECLARE @filename varchar(8000) ;
  858. IF (SUBSTRING(CAST(SERVERPROPERTY ('ProductVersion') AS varchar(50)),1,CHARINDEX('.',CAST(SERVERPROPERTY ('ProductVersion') AS varchar(50)))-1) >= 11)
  859. BEGIN
  860. If ( @path_to_health_session is null or @path_to_health_session ='')
  861. begin
  862. SET @UTDDateDiff = DATEDIFF(mi,GETUTCDATE(),GETDATE())
  863. -- Fetch information about the XEL file location
  864. SELECT @filename = CAST(target_data as XML).value('(/EventFileTarget/File/@name)[1]', 'varchar(8000)')
  865. FROM sys.dm_xe_session_targets
  866. WHERE target_name = 'event_file' and event_session_address = (select address from sys.dm_xe_sessions where name = 'system_health')
  867. SET @path_to_health_session = SUBSTRING(@filename,1,CHARINDEX('system_health',@filename,1)-1) + 'system_health*.xel'
  868. select @path_to_health_session,@filename, @UTDDateDiff
  869. end
  870. insert into tbl_ImportStatus Values('Importing XEL file','Processing',getdate())
  871. exec sp_ImportXML @path_to_health_session
  872. insert into tbl_ImportStatus Values('Load Scheduler Monitor','Processing',getdate())
  873. exec spLoadSchedulerMonitor @UTDDateDiff
  874. insert into tbl_ImportStatus Values('Load Resource Server Health Component','Processing',getdate())
  875. exec SpLoadResourceComponent @UTDDateDiff
  876. insert into tbl_ImportStatus Values('Load IO_Subsystem Server Health Component','Processing',getdate())
  877. exec SpLoadIO_SUBSYSTEMComponent @UTDDateDiff
  878. insert into tbl_ImportStatus Values('Load System Server Health Component','Processing',getdate())
  879. exec SpLoadSYSTEMComponent @UTDDateDiff
  880. insert into tbl_ImportStatus Values('Load System Health Summary','Processing',getdate())
  881. exec SpLoadComponentSummary @UTDDateDiff
  882. insert into tbl_ImportStatus Values('Load Query_Processing Server Health Component','Processing',getdate())
  883. exec SpLoadQueryProcessingComponent @UTDDateDiff
  884. insert into tbl_ImportStatus Values('Load Security Ring Buffer','Processing',getdate())
  885. exec SpLoadSecurityRingBuffer @UTDDateDiff
  886. insert into tbl_ImportStatus Values('Load Errors Recorded','Processing',getdate())
  887. exec SpLoadErrorRecorded @UTDDateDiff
  888. insert into tbl_ImportStatus Values('Wait Queries','Processing',getdate())
  889. exec spLoadWaitQueries @UTDDateDiff
  890. insert into tbl_ImportStatus Values('Connectivity Ring Buffer','Processing',getdate())
  891. exec spLoadConnectivity_ring_buffer @UTDDateDiff
  892. insert into tbl_ImportStatus Values('Deadlock Report','Processing',getdate())
  893. exec [spLoadDeadlockReport] @UTDDateDiff
  894. insert into tbl_ImportStatus Values('Import Finished','Done',getdate())
  895. end
  896. Else
  897. select 'Not a supported Server version: ' + @@version
  898. GO
  899. /********** TODO
  900. CREATE INDEXES to improve performance
  901. ****************/
  902. select 'Process System Health Session fom a SQL instance' as ImportMethod, 'Exec spLoadSystemHealthSession' as Example
  903. Union all
  904. select 'Process System Health XEL files from a UNC' as ImportMethod, 'exec spLoadSystemHealthSession @path_to_health_session=''D:\XELFiles\system_health*.xel'',@UTDDateDiff=-6' as Example
  905. /*
  906. exec spLoadSystemHealthSession @path_to_health_session='D:\XELFiles\system_health*.xel',@UTDDateDiff=-6
  907. Exec spLoadSystemHealthSession
  908. */