1. 程式人生 > >sql server記憶體使用情況 檢視Sql Server 資料庫的記憶體使用情況

sql server記憶體使用情況 檢視Sql Server 資料庫的記憶體使用情況

檢視Sql Server 資料庫的記憶體使用情況

轉自:https://www.cnblogs.com/wanghao4023030/p/8299478.html  複製程式碼
-- 查詢SqlServer總體的記憶體使用情況
select      type
        , sum(virtual_memory_reserved_kb) VM_Reserved
        , sum(virtual_memory_committed_kb) VM_Commited
        , sum(awe_allocated_kb) AWE_Allocated
        , sum(shared_memory_reserved_kb) Shared_Reserved
        , sum(shared_memory_committed_kb) Shared_Commited
        --, sum(single_pages_kb)    --SQL2005、2008
        --, sum(multi_pages_kb)        --SQL2005、2008
from    sys.dm_os_memory_clerks
group by type
order by type


-- 查詢當前資料庫快取的所有資料頁面,哪些資料表,快取的資料頁面數量
-- 從這些資訊可以看出,系統經常要訪問的都是哪些表,有多大?
select p.object_id, object_name=object_name(p.object_id), p.index_id, buffer_pages=count(*) 
from sys.allocation_units a, 
    sys.dm_os_buffer_descriptors b, 
    sys.partitions p 
where a.allocation_unit_id=b.allocation_unit_id 
    and a.container_id=p.hobt_id 
    and b.database_id=db_id()
group by p.object_id,p.index_id 
order by buffer_pages desc 


-- 查詢快取的各類執行計劃,及分別佔了多少記憶體
-- 可以對比動態查詢與引數化SQL(預定義語句)的快取量
select    cacheobjtype
        , objtype
        , sum(cast(size_in_bytes as bigint))/1024 as size_in_kb
        , count(bucketid) as cache_count
from    sys.dm_exec_cached_plans
group by cacheobjtype, objtype
order by cacheobjtype, objtype


-- 查詢快取中具體的執行計劃,及對應的SQL
-- 將此結果按照資料表或SQL進行統計,可以作為基線,調整索引時考慮
-- 查詢結果會很大,注意將結果集輸出到表或檔案中
SELECT  usecounts ,
        refcounts ,
        size_in_bytes ,
        cacheobjtype ,
        objtype ,
        TEXT
FROM    sys.dm_exec_cached_plans cp
        CROSS APPLY sys.dm_exec_sql_text(plan_handle)
ORDER BY objtype DESC ;
GO
複製程式碼

 

  原文:https://www.cnblogs.com/zhaoguan_wang/p/4602866.html

其他一些有幫助的語句:

1. 檢視SQL語句佔用多大記憶體:

複製程式碼
SELECT s2.dbid, 
s1.sql_handle, 
(SELECT TOP 1 SUBSTRING(s2.text,statement_start_offset / 2+1 , 
( (CASE WHEN statement_end_offset = -1 
THEN (LEN(CONVERT(nvarchar(max),s2.text)) * 2) 
ELSE statement_end_offset END) - statement_start_offset) / 2+1)) AS sql_statement,
execution_count, 
plan_generation_num, 
last_execution_time, 
total_worker_time, 
last_worker_time, 
min_worker_time, 
max_worker_time,
total_physical_reads, 
last_physical_reads, 
min_physical_reads, 
max_physical_reads, 
total_logical_writes, 
last_logical_writes, 
min_logical_writes, 
max_logical_writes 
FROM sys.dm_exec_query_stats AS s1 
CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS s2 
WHERE s2.objectid is null 
ORDER BY s1.sql_handle, s1.statement_start_offset, s1.statement_end_offset;
複製程式碼 複製程式碼
-- 查詢SqlServer總體的記憶體使用情況
select      type
        , sum(virtual_memory_reserved_kb) VM_Reserved
        , sum(virtual_memory_committed_kb) VM_Commited
        , sum(awe_allocated_kb) AWE_Allocated
        , sum(shared_memory_reserved_kb) Shared_Reserved
        , sum(shared_memory_committed_kb) Shared_Commited
        --, sum(single_pages_kb)    --SQL2005、2008
        --, sum(multi_pages_kb)        --SQL2005、2008
from    sys.dm_os_memory_clerks
group by type
order by type


-- 查詢當前資料庫快取的所有資料頁面,哪些資料表,快取的資料頁面數量
-- 從這些資訊可以看出,系統經常要訪問的都是哪些表,有多大?
select p.object_id, object_name=object_name(p.object_id), p.index_id, buffer_pages=count(*) 
from sys.allocation_units a, 
    sys.dm_os_buffer_descriptors b, 
    sys.partitions p 
where a.allocation_unit_id=b.allocation_unit_id 
    and a.container_id=p.hobt_id 
    and b.database_id=db_id()
group by p.object_id,p.index_id 
order by buffer_pages desc 


-- 查詢快取的各類執行計劃,及分別佔了多少記憶體
-- 可以對比動態查詢與引數化SQL(預定義語句)的快取量
select    cacheobjtype
        , objtype
        , sum(cast(size_in_bytes as bigint))/1024 as size_in_kb
        , count(bucketid) as cache_count
from    sys.dm_exec_cached_plans
group by cacheobjtype, objtype
order by cacheobjtype, objtype


-- 查詢快取中具體的執行計劃,及對應的SQL
-- 將此結果按照資料表或SQL進行統計,可以作為基線,調整索引時考慮
-- 查詢結果會很大,注意將結果集輸出到表或檔案中
SELECT  usecounts ,
        refcounts ,
        size_in_bytes ,
        cacheobjtype ,
        objtype ,
        TEXT
FROM    sys.dm_exec_cached_plans cp
        CROSS APPLY sys.dm_exec_sql_text(plan_handle)
ORDER BY objtype DESC ;
GO
複製程式碼

 

  原文:https://www.cnblogs.com/zhaoguan_wang/p/4602866.html

其他一些有幫助的語句:

1. 檢視SQL語句佔用多大記憶體:

複製程式碼
SELECT s2.dbid, 
s1.sql_handle, 
(SELECT TOP 1 SUBSTRING(s2.text,statement_start_offset / 2+1 , 
( (CASE WHEN statement_end_offset = -1 
THEN (LEN(CONVERT(nvarchar(max),s2.text)) * 2) 
ELSE statement_end_offset END) - statement_start_offset) / 2+1)) AS sql_statement,
execution_count, 
plan_generation_num, 
last_execution_time, 
total_worker_time, 
last_worker_time, 
min_worker_time, 
max_worker_time,
total_physical_reads, 
last_physical_reads, 
min_physical_reads, 
max_physical_reads, 
total_logical_writes, 
last_logical_writes, 
min_logical_writes, 
max_logical_writes 
FROM sys.dm_exec_query_stats AS s1 
CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS s2 
WHERE s2.objectid is null 
ORDER BY s1.sql_handle, s1.statement_start_offset, s1.statement_end_offset;
複製程式碼