有關鎖和記憶體使用的DMV

2022-01-17 07:49:23 字數 3834 閱讀 9607

檢視連線當前資料庫的spid所加的鎖

set transaction isolation level read uncommitted

select db_name(resource_database_id) as databasename

, request_session_id

, resource_type

, case

when resource_type = 'object'

then object_name(resource_associated_entity_id)

when resource_type in ('key', 'page', 'rid')

then (select object_name(object_id)

from sys.partitions p

where p.hobt_id = l.resource_associated_entity_id)

end as resource_type_name

, request_status

, request_mode

from sys.dm_tran_locks l

where request_session_id !=@@spid

order by request_session_id

結果如圖:

檢視沒關閉事務的空閒session

檢視被阻塞的語句和它們的等待時間

檢視超過30秒等待的查詢

buffer中快取每個資料庫所佔的buffer

set tran isolation level read uncommitted

select

isnull(db_name(database_id), 'resourcedb') as databasename

, cast(count(row_count) * 8.0 / (1024.0) as decimal(28,2))

as [size (mb)]

from sys.dm_os_buffer_descriptors

group by database_id

order by databasename

結果如圖:

當前資料庫中每個表所佔快取的大小和頁數

set transaction isolation level read uncommitted

select 

object_name(p.[object_id]) as [tablename]

, (count(*) * 8) / 1024   as [buffer size(mb)]

, isnull(i.name, '-- heap --') as objectname

,  count(*) as numberof8kpages

from sys.allocation_units as a

inner join sys.dm_os_buffer_descriptors as b

on a.allocation_unit_id = b.allocation_unit_id

inner join sys.partitions as p

inner join sys.indexes i on p.index_id = i.index_id

and p.[object_id] = i.[object_id]

on a.container_id = p.hobt_id

where b.database_id = db_id()

and p.[object_id] > 100

group by p.[object_id], i.name

order by numberof8kpages desc

結果如下:

資料庫級別等待的io

set tran isolation level read uncommitted

select db_name(database_id) as [databasename]

, sum(cast(io_stall / 1000.0 as decimal(20,2))) as [io stall (secs)]

, sum(cast(num_of_bytes_read / 1024.0 / 1024.0 as decimal(20,2)))

as [io read (mb)

, sum(cast(num_of_bytes_written / 1024.0 / 1024.0  as decimal(20,2)))

as [io written (mb)

, sum(cast((num_of_bytes_read + num_of_bytes_written)

/ 1024.0 / 1024.0 as decimal(20,2))) as [totalio (mb)

from sys.dm_io_virtual_file_stats(null, null)

group by database_id

order by [io stall (secs)] desc

結果如下:

按檔案檢視io情況

set tran isolation level read uncommitted

select db_name(database_id) as [databasename]

, file_id

, sum(cast(io_stall / 1000.0 as decimal(20,2))) as [io stall (secs)]

, sum(cast(num_of_bytes_read / 1024.0 / 1024.0 as decimal(20,2)))

as [io read (mb)]

, sum(cast(num_of_bytes_written / 1024.0 / 1024.0  as decimal(20,2)))

as [io written (mb)]

, sum(cast((num_of_bytes_read + num_of_bytes_written)

/ 1024.0 / 1024.0 as decimal(20,2))) as [totalio (mb)]

from sys.dm_io_virtual_file_stats(null, null)

group by database_id, file_id

order by [io stall (secs)] desc

結果如下:

七 記憶體優化(3)使用DMV

一 sys.dm os memory clerks 1.查詢dmv sys.dm os memory clerks返回sql server例項中當前處於活動狀態的全部記憶體clerk的集合。跟蹤這個dmv,可以看到記憶體是如何被sql server消耗。select type,sum virtual...

七 記憶體優化(3)使用DMV

一 sys.dm os memory clerks 1.查詢dmv sys.dm os memory clerks返回sql server例項中當前處於活動狀態的全部記憶體clerk的集合。跟蹤這個dmv,可以看到記憶體是如何被sql server消耗。select type,sum virtual...

七 記憶體優化(3)使用DMV

一 sys.dm os memory clerks 1.查詢dmv sys.dm os memory clerks返回sql server例項中當前處於活動狀態的全部記憶體clerk的集合。跟蹤這個dmv,可以看到記憶體是如何被sql server消耗。select type,sum virtual...