搜尋本站文章

2017-08-06

[SQL Server] sp_whoisactive: find out long running queries (找出費時、耗資源的查詢) (2)


延續 先前文章:[SQL Server] sp_whoisactive: find out long running queries (找出費時、耗資源的查詢)


補充 常用的範例

-- EX01_sp_WhoIsActive:增加顯示 執行計畫、交易資訊、封鎖資訊等
USE master
GO
EXEC sp_WhoIsActive @get_plans=1, @get_transaction_info=1, @find_block_leaders=1
GO
 
-- EX02_sp_WhoIsActive:增加顯示 執行計畫、交易資訊、封鎖資訊、系統資訊等
USE master
GO
EXEC sp_WhoIsActive @get_plans=1, @get_transaction_info=1, 
 @find_block_leaders=1, @get_task_info=2
GO

-- EX03_sp_WhoIsActive:增加顯示 執行計畫、交易資訊、封鎖資訊、系統資訊、
-- 取得更多關聯的 ad hoc 查詢或 執行預存程序的呼叫程序、
-- 取得此工作階段(Session)上的更多參考資訊
USE master
GO
EXEC sp_WhoIsActive @get_plans=1, @get_transaction_info=1, @find_block_leaders=1,
  @get_task_info=2, @get_outer_command = 1, @get_additional_info = 2
GO



sp_whoisactive 參數說明 2

目前已有24個以上的參數,善用 @help = 1 來查詢使用方式。

參數 說明
@get_outer_command = 1 嘗試取得更多關聯的 ad hoc 查詢或 執行預存程序的呼叫程序
Get the associated outer ad hoc query or stored procedure call, if available
@sort_order ='[blocked_session_count] ASC'
排序
Column(s) by which to sort output, optionally with sort directions. 
Valid column choices:
session_id, physical_io, reads, physical_reads, writes, tempdb_allocations, tempdb_current, CPU, context_switches, used_memory, physical_io_delta, 
reads_delta, physical_reads_delta, writes_delta, tempdb_allocations_delta,  tempdb_current_delta, CPU_delta, context_switches_delta, used_memory_delta, 
tasks, tran_start_time, open_tran_count, blocking_session_id, blocked_session_count,  percent_complete, host_name, login_name, database_name, start_time, login_time
Note that column names in the list must be bracket-delimited. 
Commas and/or white space are not required. 
@get_additional_info = 1
取得此工作階段(Session)上的更多參考資訊。

Get additional non-performance-related information about the session or request text_size, language, date_format, date_first, quoted_identifier, arithabort, ansi_null_dflt_on,  ansi_defaults, ansi_warnings, ansi_padding, ansi_nulls, concat_null_yields_null, transaction_isolation_level, lock_timeout, deadlock_priority, row_count, command_type.

If a SQL Agent job is running, an subnode called agent_info will be populated with some or all of the following: job_id, job_name, step_id, step_name, msdb_query_error (in the event of an error)

If @get_task_info is set to 2 and a lock wait is detected, a subnode called block_info will be populated with some or all of the following: lock_type, database_name, object_id, file_id, hobt_id,  applock_hash, metadata_resource, metadata_class_id, object_name, schema_name.
@help = 1 顯示出可用的參數與說明



sp_whoisactive 使用範例

-- 010_使用help_線上說明



-- 011_get_outer_command=1_ 取得此工作階段(Session)上的更多參考資訊



-- 012_get_outer_command=1_ 取得此工作階段(Session)上的更多參考資訊_2



-- 013_增加顯示:封鎖鏈結、封鎖計數,但沒有排序



-- 014_顯示:封鎖鏈結、封鎖計數,並依據 blocked_session_count 排序



-- 015_取得此工作階段(Session)上的更多參考資訊



-- 016_取得此工作階段(Session)上的更多參考資訊_xml_交易隔離層級



-- 021_顯示 執行計畫、交易資訊、封鎖資訊、系統資訊、取得更多關聯的 ad hoc 查詢或 執行預存程序的呼叫程序、取得此工作階段(Session)上的更多參考資訊



-- 022_延續 021_顯示 執行計畫...



-- 023_延續 021_顯示 執行計畫...


-- 024_延續 021_顯示 執行計畫...


-- 025_延續 021_顯示 執行計畫...




sp_whoisactive 使用範例程式碼(2)



-- EX01-sp_WhoIsActive:線上說明
USE master
GO
EXEC sp_WhoIsActive @help = 1
GO
-- EX02-sp_WhoIsActive: @get_outer_command= 1 , 
-- 嘗試取得更多關聯的 ad hoc 查詢或 執行預存程序的呼叫程序
USE master
GO
EXEC sp_WhoIsActive @get_outer_command = 1
GO


-- EX03-sp_WhoIsActive:增加顯示:封鎖鏈結、封鎖計數,
-- 並依據 blocked_session_count 排序
USE master
GO
EXEC sp_WhoIsActive @find_block_leaders = 1 , 
 @sort_order ='[blocked_session_count] ASC'
GO
-- EX04-sp_WhoIsActive:取得此工作階段(Session)上的更多參考資訊
USE master
GO
EXEC sp_WhoIsActive @get_additional_info = 1
GO


-- EX05-sp_WhoIsActive:
-- 增加顯示 執行計畫、交易資訊、封鎖資訊、系統資訊、
-- 取得更多關聯的 ad hoc 查詢或 執行預存程序的呼叫程序、取得此工作階段(Session)上的更多參考資訊
USE master
GO
EXEC sp_WhoIsActive @get_plans=1, @get_transaction_info=1, @find_block_leaders=1, 
 @get_task_info=2, @get_outer_command = 1, @get_additional_info = 2
GO





參考資料
[SQL Server] sp_whoisactive: find out long running queries (找出費時、耗資源的查詢)
http://sharedderrick.blogspot.tw/2017/07/sql-server-spwhoisactive-find-out-long.html

sp_whoisactive
http://whoisactive.com/

Looking for sp_whoisactive?
http://sqlblog.com/blogs/adam_machanic/archive/2017/07/26/looking-for-sp-whoisactive.aspx

sp_whoisactive Documentation
http://whoisactive.com/docs/

2017-08-05

[SQL Server] Stress testing, SQLQueryStress parameter, 壓力測試, 搭配使用 參數 (2)


延續先前文章:[SQL Server] SQLQueryStress, Stress testing, 壓力測試

本文討論:
SQLQueryStress: 搭配 參數(Parameter),以預存程序(Stored Procedure)、T-SQL 參數(Parameter) 為例

-- 001_SQLQueryStress: 搭配 預存程序(Stored Procedure) 與 參數(Parameter)



-- 002_SQLQueryStress: 搭配 T-SQL 參數(Parameter)




SQLQueryStress: 搭配 參數(Parameter),以預存程序(Stored Procedure)、T-SQL 參數(Parameter) 為例

任務01:SQLQueryStress: 搭配 預存程序(Stored Procedure) 與 參數(Parameter)

-- 010_設定要執行 參數化 程式




-- 011_點選_Parameter_Substitution



-- 012_準備設定_Parameter_Query



-- 013_點選_Database_設定資料庫連線資訊



-- 014_輸入要取出參數值的程式碼



-- 015_點選_Get_Columns



-- 016_下拉_點選_Column



-- 017_已經取得資料行



-- 018_點選_DataType



-- 018_02_預存程序,設定參數



-- 019_SQL_Profiler_錄製_帶入參數值來執行



-- 020_SSMS_觀察執行的預存程序_次數



-- 021_SSMS_觀察執行的預存程序_次數_2



-- 022_SSMS_觀察執行的預存程序_次數_3



-- 023_SSMS_觀察執行的預存程序_次數_4




任務02:SQLQueryStress: 搭配 T-SQL 參數(Parameter) 

-- 100_參數化的查詢式



-- 101_設定_Parameter_Query



-- 102_執行 SQLQueryStress



-- 103_SSMS_參數化_執行次數



-- 104_SSMS_參數化_執行次數_2



-- 105_SSMS_參數化_執行次數_3



-- 106_SQL_Profiler_參數化查詢式_帶入參數





範例程式碼

01-執行預存程序 [CustOrdersDetail],參數:@OrderID

-- 00-執行預存程序 [dbo].[CustOrdersDetail],輸入參數:@OrderID
USE Northwind
GO
EXEC [dbo].[CustOrdersDetail] @OrderID=10248
GO

02-SQLQueryStress 要執行的範例程式碼

-- 01-SQLQueryStress 要執行的範例程式碼
EXEC [dbo].[CustOrdersDetail] @OrderID=10248;

-- 02-在 Parameter Substitution 要執行的範例程式碼
SELECT OrderID FROM Orders;

--
SELECT * FROM [Order Details] WHERE OrderID =10248;

03-Query: Execution count for stored procedure

-- 10-Query: Execution count for stored procedure
-- 查詢 預存程序 的執行次數
USE master
GO
SELECT  DB_NAME(st.dbid) 'DB' ,OBJECT_SCHEMA_NAME(st.objectid,dbid) 'Schema',
 OBJECT_NAME(st.objectid,dbid) 'SP' ,MAX(cp.usecounts) 'ExecutionCount'
 , cp.objtype, cp.cacheobjtype, st.text
FROM sys.dm_exec_cached_plans cp CROSS APPLY sys.dm_exec_sql_text(cp.plan_handle) st
WHERE DB_NAME(st.dbid) IS NOT NULL
 AND cp.objtype = 'Proc' AND  DB_NAME(st.dbid) ='Northwind' -- Filter
GROUP BY cp.plan_handle, DB_NAME(st.dbid), OBJECT_SCHEMA_NAME(objectid,st.dbid),
 OBJECT_NAME(objectid,st.dbid), cp.objtype, cp.cacheobjtype,  st.text
ORDER BY MAX(cp.usecounts) DESC
GO

--
SELECT * FROM sys.dm_exec_cached_plans cp CROSS APPLY sys.dm_exec_sql_text(cp.plan_handle) st
WHERE DB_NAME(st.dbid) ='Northwind' -- Filter
GO




參考資料

[SQL Server] SQLQueryStress, Stress testing, 壓力測試