Breaking News
Loading...
20/03/2013

Who Is Active?

'WhoIsActive' là 1 tool hữu ích cho ta biết cái gì thực sự đang xảy ra trong SQL Server, cùng với rất nhiều thông tin hữu ích khác như wait statistics, locking/blocking. Tool này được viết ra để thay thế sp_who có sẵn của SQL Server.

Có nhiều tham số để ta sử dụng, đây là danh sách khá đầy đủ và giá trị default của nó:

@filter sysname = '' 
@filter_type VARCHAR(10) = 'session' 
@not_filter sysname = '' 
@not_filter_type VARCHAR(10) = 'session' 
@show_own_spid BIT = 0 
@show_system_spids BIT = 0 
@show_sleeping_spids TINYINT = 1 
@get_full_inner_text BIT = 0 
@get_plans TINYINT = 0 
@get_outer_command BIT = 0 
@get_transaction_info BIT = 0 
@get_task_info TINYINT = 1 
@get_locks BIT = 0 
@get_avg_time BIT = 0 
@get_additional_info BIT = 0 
@find_block_leaders BIT = 0 
@delta_interval TINYINT = 0 
@output_column_list VARCHAR(8000) = '[dd%][session_id][sql_text][sql_command][login_name][wait_info][tasks][tran_log%][cpu%][temp%][block%][reads%][writes%][context%][physical%][query_plan][locks][%]' 
@sort_order VARCHAR(500) = '[start_time] ASC' 
@format_output TINYINT = 1 
@destination_table VARCHAR(4000) = '' 
@return_schema BIT = 0 
@schema VARCHAR(MAX) = NULL OUTPUT 
@help BIT = 0

Sau đây là các tham số mà tôi dùng và thấy rất hay:

Tham số @get_plans=1: để thấy các execution plan:

sp_whoisactive @get_plans=1

Tham số @get_full_inner_text = 1: Có 1 điều tuyệt vời của WhoIsActive là ví dụ ta thực thi một SQL Batch gồm nhiều câu lệnh SQL như sau, thì WhoIsActive cho ta biết lệnh nào đang chạy.

SELECT * 
FROM sys.tables

WAITFOR DELAY '10:10:10'

SELECT * 
FROM sys.databases 
GO

Nếu ta chạy WhoIsActive thì đây là kết quả


Nếu ta muốn có được toàn bộ SQL Batch (đây là cái tôi muốn), thì dùng lệnh sau:

EXEC sp_WhoIsActive 
    @get_full_inner_text = 1

Đây là kết quả

Tham số @get_outer_command = 1: Có 1 tình huống rất thú vị như đoạn script sau:

USE tempdb 
GO

--Create two "inner" procs

CREATE PROC inner_test_proc1 
AS 
BEGIN 
    WAITFOR DELAY '10:10:10' 
END 
GO

CREATE PROC inner_test_proc2 
AS 
BEGIN 
    WAITFOR DELAY '20:20:20' 
END 
GO

--Create one "outer" proc to call the others

CREATE PROC outer_test_proc 
    @x INT 
AS 
BEGIN 
    IF @x = 1 
        EXEC inner_test_proc1 
    ELSE 
        EXEC inner_test_proc2 
END 
GO

--Call the “outer” test proc 
EXEC outer_test_proc 2 
GO

Đoạn script ở trên tạo ra 2 Stored, sau đó tạo 1 Stored thứ 3 gọi 1 trong 2 Stored đầu tùy theo điều kiện, cái hay của WhoIsActive là nó cho ta biết Stored nào đang chạy.

EXEC sp_WhoIsActive 
    @get_outer_command = 1


Tham số @Filter_Type và @Filter: nếu ta chỉ muốn monitor một database nào đó thì dùng cú pháp như sau:

EXEC sp_whoisactive @filter_type = 'database', @filter = 'your DB name'

Nếu ta muốn thấy thêm các thông tin khác như các session đang slepping, các system session và cả session đang chạy WhoIsActive thì dùng cú pháp sau:
EXEC sp_WhoIsActive 
    @show_sleeping_spids = 2, 
    @show_system_spids = 1, 
    @show_own_spid = 1

Tham khảo:
http://sqlblog.com/blogs/adam_machanic/archive/2011/04/10/commands-batches-and-the-mysteries-of-text-a-month-of-activity-monitoring-part-10-of-30.aspx

http://sqlblog.com/blogs/adam_machanic/archive/2011/04/27/who-is-active-v11-00-a-month-of-activity-monitoring-part-27-of-30.aspx

Một tài liệu khác về tool này:

http://www.brentozar.com/archive/2013/08/query-plans-what-happens-when-row-estimates-get-high/

Một bài viết hướng dẫn tạo Alert dùng sp_WhoIsActive khi có blocking xảy ra

http://jonmorisissqlblog.blogspot.com/2013/04/alert-based-automation-of-whoisactive.html

Một video về cách sử dụng tool này

0 comments:

Post a Comment

 
Toggle Footer