Sql 2012中利用擴充套件事件(Xevents)捕捉高消耗SQL查詢
生產環境中有時需要使用者抓取一些特定的語句分析,如超超長查詢,或高IO查詢等.一般來說大家對跟蹤比較熟悉,主要因為其有完善的UI支援.由於擴充套件事件(Xevents)在sql2012才提供UI支援,所以雖然在08時就已經存在的更輕量級的擴充套件事件沒有被廣泛使用.這裡和大家分享一個擴充套件事件指令碼,用來捕捉高消耗的查詢語句.
注意:使用完畢後應停止擴充套件事件捕捉.
應根據自身實際需求設定過濾條件,如CPU,duration,Reads等
在sql2012中會提供更為豐富的內容(如query_hash等)
code 1 將擴充套件事件追蹤儲存到本地檔案中
CREATE EVENT SESSION L_QUERIES ON SERVER ADD EVENT sqlserver.sql_statement_completed( ACTION(sqlserver.username,sqlserver.client_hostname,sqlserver.database_id,sqlserver.session_id,sqlserver.sql_text,sqlserver.plan_handle) WHERE (cpu>1000 AND duration>=5000000 and Reads>=10000 and sqlserver.database_id=17)) ADD TARGET package0.asynchronous_file_target (SET FILENAME = N'D:\L_QUERIES.xel', METADATAFILE = N'D:\L_QUERIES.xem') WITH (max_dispatch_latency = 1 seconds); --cpu 1s --duration 5s --reads io 10000 --database_id 17 if you do not need it ,cut out GO ALTER EVENT SESSION L_QUERIES ON SERVER STATE = START;------start the event GO SELECT data.value ( '(/event[@name=''sql_statement_completed'']/@timestamp)[1]', 'DATETIME') AS [Time], data.value ( '(/event/data[@name=''cpu'']/value)[1]', 'INT') AS [CPU (ms)], data.value ( '(/event/data[@name=''reads'']/value)[1]', 'BIGINT') AS [Reads], data.value ( '(/event/action[@name=''session_id'']/value)[1]', 'smallint') AS [session_id], data.value ( '(/event/action[@name=''database_id'']/value)[1]', 'smallint') AS [database_id], CONVERT (FLOAT, data.value ('(/event/data[@name=''duration'']/value)[1]', 'BIGINT')) / 1000000 AS [Duration (s)], data.value ( '(/event/action[@name=''username'']/value)[1]', 'VARCHAR(100)') AS [username], data.value ( '(/event/action[@name=''client_hostname'']/value)[1]', 'VARCHAR(100)') AS [client_hostname], data.value ( '(/event/action[@name=''sql_text'']/value)[1]', 'VARCHAR(MAX)') AS [SQL Statement], SUBSTRING (data.value ('(/event/action[@name=''plan_handle'']/value)[1]', 'VARCHAR(100)'), 15, 50) AS [Plan Handle] FROM (SELECT CONVERT (XML, event_data) AS data FROM sys.fn_xe_file_target_read_file ('D:\L_QUERIES*.xel', 'D:\L_QUERIES*.xem', null, null) ) entries ORDER BY [Time] DESC;----------query the event info what you need --ALTER EVENT SESSION L_QUERIES ON SERVER STATE = STOP;---stop the event --DROP EVENT SESSION L_QUERIES ON SERVER;------------drop the event
code2 將擴充套件事件儲存到ring buffer中
CREATE EVENT SESSION L_QUERIES ON SERVER ADD EVENT sqlserver.sql_statement_completed( ACTION(sqlserver.username,sqlserver.client_hostname,sqlserver.database_id,sqlserver.session_id,sqlserver.sql_text,sqlserver.plan_handle) WHERE (cpu>1000 AND duration>=5000000 and Reads>=10000 and sqlserver.database_id=17)) ADD TARGET package0.ring_buffer (SET max_memory = 4000) -----not the default 4096kb WITH (max_dispatch_latency = 1 seconds) --cpu 1s --duration 5s --reads io 10000 --database_id 17 if you do not need it ,cut out GO ALTER EVENT SESSION L_QUERIES ON SERVER STATE = START;------start the event GO DECLARE @target_data XML SELECT @target_data=CAST(target_data AS XML) FROM sys.dm_xe_sessions AS s JOIN sys.dm_xe_session_targets AS t ON t.event_session_address = s.address WHERE s.name = 'L_QUERIES' AND t.target_name = 'ring_buffer' SELECT t.value('(@timestamp)[1]', 'datetime2') AS [time], t.value ('(data[@name=''cpu'']/value)[1]', 'INT') AS [CPU (ms)], t.value ('(data[@name=''reads'']/value)[1]', 'BIGINT') AS [Reads], t.value ('(action[@name=''session_id'']/value)[1]', 'smallint') AS [session_id], t.value ('(action[@name=''database_id'']/value)[1]', 'smallint') AS [database_id], CONVERT (FLOAT, t.value ('(data[@name=''duration'']/value)[1]', 'BIGINT')) / 1000000 AS [Duration (s)], t.value ('(action[@name=''username'']/value)[1]', 'VARCHAR(100)') AS [username], t.value ('(action[@name=''client_hostname'']/value)[1]', 'VARCHAR(100)') AS [client_hostname], t.value ('(action[@name=''sql_text'']/value)[1]', 'VARCHAR(MAX)') AS [SQL Statement], SUBSTRING (t.value ('(action[@name=''plan_handle'']/value)[1]', 'VARCHAR(100)'), 15, 50) AS [Plan Handle] FROM @target_data.nodes('RingBufferTarget/event') AS q(t) ORDER BY [Time] DESC;----------query the event info what you need --ALTER EVENT SESSION L_QUERIES ON SERVER STATE = STOP;---stop the event --DROP EVENT SESSION L_QUERIES ON SERVER-----------drop the event
相關文章
- SQL效能的度量 - 利用10046事件擴充套件SQL跟蹤SQL事件套件
- 正確讀取SQL Server中的擴充套件事件SQLServer套件事件
- sql中的擴充套件學習SQL套件
- in_list查詢與or擴充套件套件
- SQL高階查詢SQL
- php利用pcntl擴充套件實現高併發PHP套件
- 指定快照時間內根據等待類查詢消耗較高的sqlSQL
- SQL Server 禁用擴充套件儲存過程SQLServer套件儲存過程
- sql-server高階查詢SQLServer
- sql之20高階查詢SQL
- EFCore之SQL擴充套件元件BeetleX.EFCore.ExtensionSQL套件元件
- SQL Server簡潔查詢正在執行SQL(等待事件)SQLServer事件
- 並查集擴充套件並查集套件
- 高朋Groupon使用 Redis 每分鐘擴充套件數百萬個地理空間查詢Redis套件
- SQLServer如何查詢近3分鐘最消耗CPU的SQLSQLServer
- 進行Windows Azure SQL Database的水平擴充套件GVWindowsSQLDatabase套件
- Flink SQL 在快手的擴充套件和實踐SQL套件
- 水平和垂直劃分擴充套件SQL Server系統套件SQLServer
- SQL Story摘錄(三)————可擴充套件設計 (轉)SQL套件
- SQL Server 2008的效能和擴充套件SQLServer套件
- SQL_Postgresql-一些擴充套件和應用SQL套件
- MySQL 5.7定位消耗CPU高的SQLMySql
- 利用 phpize 安裝 openssl 擴充套件PHP套件
- 利用Delphi編寫IE擴充套件套件
- SQL Server資料庫檔案不滿足擴充套件條件時不再自動擴充套件SQLServer資料庫套件
- oracle 高耗cpu sql語句的捕捉 。OracleSQL
- SQL語言基礎(高階查詢)SQL
- 查詢 分析硬解析較高的sql,SQL
- 利用擴充套件事件進行調優和Troubleshooting PPT分享套件事件
- SQL 查詢中的 NULL 值SQLNull
- Oracle高資源消耗SQL語句定位OracleSQL
- shell中擴充套件命令套件
- 利用Delphi編寫IE擴充套件 (轉)套件
- IOS setOnclick - 點選事件完美擴充套件iOS事件套件
- Oracle 12CR2查詢轉換之表擴充套件Oracle套件
- Mybatis之map操作使用者和模糊查詢擴充套件MyBatis套件
- 在EF Core 中使用AsQueryable擴充套件方法的拼接查詢套件
- 並查集擴充套件應用並查集套件