十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
當(dāng)SQL Server 引擎接收到用戶發(fā)出的查詢請求時(shí),SQL Server執(zhí)行優(yōu)化器將查詢請求(Request)和Task綁定,并為Task分配一個(gè)Workder,SQL Server申請操作系統(tǒng)的進(jìn)程(Thread)來執(zhí)行Worker。如果以并行的方式執(zhí)行Request,SQL Server根據(jù)Max DOP(Maximum Degree Of Parallelism) 配置選項(xiàng)創(chuàng)建新的Child Tasks,SQL Server將Request和多個(gè)Task綁定;例如,如果Max DOP=8,那么將會(huì)存在 1個(gè)Master Task和 8 個(gè)Child Tasks。每個(gè)Task綁定到一個(gè)Worker中,SQL Server引擎將分配相應(yīng)數(shù)量的Worker來執(zhí)行Tasks。
創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比海湖新網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式海湖新網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋海湖新地區(qū)。費(fèi)用合理售后完善,十余年實(shí)體公司更值得信賴。一,查看正在執(zhí)行的Request
使用 sys.dm_exec_requests 返回正在執(zhí)行的查詢請求(Request)關(guān)聯(lián)的查詢腳本,阻塞和資源消耗。
1,查看SQL Server正在執(zhí)行的查詢語句
sql_handle,statement_start_offset,statement_end_offset ,能夠用于查看正在執(zhí)行的查詢語句;
字段plan_handle,用于查看查詢語句的執(zhí)行計(jì)劃;
字段 command 用于表示正在被處理的Command的當(dāng)前的類型:SELECT,INSERT,UPDATE,DELETE,BACKUP LOG ,BACKUP DATABASE,DBCC,F(xiàn)OR;
2,查看阻塞(Block)的語句
字段 wait_type:如果Request正在被阻塞,字段wait_type 返回當(dāng)前的Wait Type
字段 last_wait_type:上一次阻塞的Wait Type
字段 wait_resource:當(dāng)前阻塞的Request正在等待的資源
字段 blocking_session_id :將當(dāng)前Request阻塞的Session
3,內(nèi)存,IO,CPU消耗統(tǒng)計(jì)
字段 granted_query_memory: 授予內(nèi)存的大小,Number of pages allocated to the execution of a query on the request
字段 cpu_time,total_elapsed_time :消耗的CPU時(shí)間和總的消耗時(shí)間
字段 reads,writes,logical_reads:物理Read,邏輯Write 和邏輯Read的次數(shù)
二,查看SQL Server 當(dāng)前正在執(zhí)行的SQL查詢語句
在進(jìn)行故障排除時(shí),使用DMV:sys.dm_exec_requests 查看SQL Server當(dāng)前正在執(zhí)行的查詢語句:
select r.session_id, r.blocking_session_id as blocking, r.wait_type as current_wait_type, r.wait_resource, r.last_wait_type, r.wait_time/1000 as wait_s, r.status, r.command, r.cpu_time,r.reads,r.writes,r.logical_reads, r.total_elapsed_time,r.start_time,r.database_id, substring( st.text, r.statement_start_offset/2+1, ( case when r.statement_end_offset = -1 then len(convert(nvarchar(max), st.text)) else (r.statement_end_offset - r.statement_start_offset)/2 end ) ) as individual_query --,db_name(r.database_id) as dbname,r.percent_complete,r.estimated_completion_time,r.granted_query_memoryfrom sys.dm_exec_requests router APPLY sys.dm_exec_sql_text(r.sql_handle) as stwhere ((r.wait_type<>'MISCELLANEOUS' and r.wait_type <> 'DISPATCHER_QUEUE_SEMAPHORE' ) or r.wait_type is null) and r.session_id>50 and r.session_id<>@@spidorder by r.session_id asc
1,在故障排除時(shí),可以過濾掉一些無用的wait type 和當(dāng)前Session:
@@SPID 表示當(dāng)前的spid,一般來說,SPID<=50是system session,SPID>50的是User Session;
WaitType 為'MISCELLANEOUS' 時(shí),不用于標(biāo)識(shí)任何有效的Wait,僅僅作為默認(rèn)的Wait;
WaitType 為‘DISPATCHER_QUEUE_SEMAPHORE’時(shí),表示當(dāng)前的Thread在等待處理更多的Work,如果Wait Time增加,說明Thread調(diào)度器(Dispatcher)非??臻e;
關(guān)于WaitType ,請查看 The SQL Server Wait Type Repository;
2,查看request執(zhí)行的SQL查詢語句
sql_handle 字段表示當(dāng)前查詢語句的句柄(handle),將該字段傳遞給sys.dm_exec_sql_text函數(shù),將獲取Request執(zhí)行的SQL語句,SQL Server對某些包含常量的查詢語句自動(dòng)參數(shù)化(“Auto-parameterized”),獲取的SQL 查詢語句格式如下,SQL Server在查詢語句的開頭增加參數(shù)聲明:
(@P1 int,@P2 int,@P3 datetime2(7),@P4 datetime2(7))WITH CategoryIDs AS (SELECT B.CategoryID, .....
兩個(gè)字段:stmt_start和stmt_end,用于標(biāo)識(shí)參數(shù)聲明的開始和結(jié)尾的位置,使用這兩個(gè)字段,將參數(shù)聲明剝離,返回SQL Server執(zhí)行的查詢語句。
3,阻塞
字段 blocking_session_id :阻塞當(dāng)前Request的Session,但排除0,-2,-3,-4 這四種ID值:
If this column is 0, the request is not blocked, or the session information of the blocking session is not available (or cannot be identified).
-2 = The blocking resource is owned by an orphaned distributed transaction.
-3 = The blocking resource is owned by a deferred recovery transaction.
-4 = Session ID of the blocking latch owner could not be determined at this time because of internal latch state transitions.
三,查看SQL Server實(shí)例中活動(dòng)的Task
使用DMV:sys.dm_os_tasks 查看當(dāng)前實(shí)例中活動(dòng)的Task
1,字段 task_state,標(biāo)識(shí)Task的狀態(tài)
PENDING: Waiting for a worker thread.
RUNNABLE: Runnable, but waiting to receive a quantum.
RUNNING: Currently running on the scheduler.
SUSPENDED: Has a worker, but is waiting for an event.
DONE: Completed.
SPINLOOP: Stuck in a spinlock.
2,掛起的IO(Pending)
pending_io_count
pending_io_byte_count
pending_io_byte_average
3,關(guān)聯(lián)的Request和Worker(associated)
request_id :ID of the request of the task.
worker_address :Memory address of the worker that is running the task. NULL = Task is either waiting for a worker to be able to run, or the task has just finished running.
4, Task Hierarchy
task_address:Memory address of the object.
parent_task_address:Memory address of the task that is the parent of the object.
5,監(jiān)控并發(fā)Request(Monitoring parallel requests)
For requests that are executed in parallel, you will see multiple rows for the same combination of (<session_id>, <request_id>).
SELECT session_id, request_id, task_state, pending_io_count, pending_io_byte_count, pending_io_byte_average, scheduler_id, context_switches_count, task_address, worker_address, parent_task_addressFROM sys.dm_os_tasksORDER BY session_id, request_id;
或利用 Task Hierarchy來查詢
select tp.session_id, tp.task_state as ParentTaskState, tc.task_state as ChildTaskStatefrom sys.dm_os_tasks tpinner join sys.dm_os_tasks tc on tp.task_address=tc.parent_task_address
四,等待資源的Task(waiting)
使用DMV:sys.dm_os_waiting_tasks 查看系統(tǒng)中正在等待資源的Task
waiting_task_address: Task that is waiting for this resouce.
blocking_task_address:Task that is currently holding this resource
resource_description: Description of the resource that is being consumed. 參考sys.dm_os_waiting_tasks (Transact-SQL)
在對阻塞進(jìn)行故障排除時(shí),查看Block 和 爭用的資源:
select wt.waiting_task_address, wt.session_id, --Wait and Resource wt.wait_duration_ms, wt.wait_type, wt.resource_address, wt.resource_description, wt.blocking_task_address, wt.blocking_session_idfrom sys.dm_os_waiting_tasks wt
五,使用dbcc inputbuffer(spid)獲取spid最后一次執(zhí)行的SQL語句
dbcc inputbuffer(spid)
Appendix:
引用《How to isolate the current running commands in SQL Server》,該文章描述了如何分離Request執(zhí)行的查詢語句:
View Code
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。