SQL Server中查詢CPU占用高的SQL語句

2022-09-16 20:45:10 字數 2631 閱讀 4955

sql server中查詢cpu占用高的情況,會用到sys.sysprocesses ,dm_exec_sessions ,dm_exec_requests

一、檢視當前的資料庫使用者連線有多少

use master

goselect * from sys.[sysprocesses] where [spid]>50 --and db_name([dbid])='gposdb'

select count(*) from [sys].[dm_exec_sessions] where [session_id]>50

二、選取前10個最耗cpu時間的會話

select top 10

[session_id],

[request_id],

[start_time] as '開始時間',

[status] as '狀態',

[command] as '命令',

dest.[text] as 'sql語句',

db_name([database_id]) as '資料庫名',

[blocking_session_id] as '正在阻塞其他會話的會話id',

[wait_type] as '等待資源型別',

[wait_time] as '等待時間',

[wait_resource] as '等待的資源',

[reads] as '物理讀次數',

[writes] as '寫次數',

[logical_reads] as '邏輯讀次數',

[row_count] as '返回結果行數'

from sys.[dm_exec_requests] as der

sys.[dm_exec_sql_text](der.[sql_handle]) as dest

where [session_id]>50 and db_name(der.[database_id])='gposdb'

order by [cpu_time] desc

三、查詢前10個最耗cpu時間的sql語句

select top 10 dest.[text] as 'sql語句'

from sys.[dm_exec_requests] as der

sys.[dm_exec_sql_text](der.[sql_handle]) as dest

where [session_id]>50

order by [cpu_time] desc

四、查詢會話中有多少個worker在等待 

select top 10

[session_id],

[request_id],

[start_time] as '開始時間',

[status] as '狀態',

[command] as '命令',

dest.[text] as 'sql語句',

db_name([database_id]) as '資料庫名',

[blocking_session_id] as '正在阻塞其他會話的會話id',

der.[wait_type] as '等待資源型別',

[wait_time] as '等待時間',

[wait_resource] as '等待的資源',

[dows].[waiting_tasks_count] as '當前正在進行等待的任務數',

[reads] as '物理讀次數',

[writes] as '寫次數',

[logical_reads] as '邏輯讀次數',

[row_count] as '返回結果行數'

from sys.[dm_exec_requests] as der

inner join [sys].[dm_os_wait_stats] as dows

on der.[wait_type]=[dows].[wait_type]

sys.[dm_exec_sql_text](der.[sql_handle]) as dest

where [session_id]>50

order by [cpu_time] desc

五、查詢cpu占用高的語句

select top 10

total_worker_time/execution_count as **g_cpu_cost, plan_handle,

execution_count,

(select substring(text, statement_start_offset/2 + 1,

(case when statement_end_offset = -1

then len(convert(nvarchar(max), text)) * 2

else statement_end_offset

end - statement_start_offset)/2)

from sys.dm_exec_sql_text(sql_handle)) as query_text

from sys.dm_exec_query_stats

order by [**g_cpu_cost] desc

SQL Server中的查詢

本博文簡單介紹一下sql server中常用的幾類查詢及相關使用的方法。一 executescalar方法獲取單一值 executescalar方法是sqlcommand類的方法之一,執行查詢,並返回查詢所返回的結果集中的第一行第一列。csharp view plain copy print cla...

linux中查詢cpu型號 核心版本

cpu 型別和型號 核心版本 從系統最後一期啟動以來的時間,形式為 dd hh mm ss void partb printf machine hostname s n computer 列印cpu型號 procfile open proc cpuinfo o rdonly info read pr...

查詢Linux中CPU的核數

以一台linux伺服器為例。這台linux包括兩顆intel r xeon r cpu e5 2630 v4 2.20ghzcpu,單顆cpu包括 10 個 cpu core,使用超執行緒包含20個邏輯cpu core,具體的官方介紹 e5 2630 v4。下面讓我們通過linux的命令來查詢對應的...