查看SqlServer的內存使用狀況

SQL SERVER內存按存放數據的類型,大概能夠分爲三類:html

一、buffer pool,存放數據頁面的緩衝區,sql server數據都是存放在一個個8K的頁面裏,當用戶須要使用這個頁面上的數據時,都是把整個頁面加載到內存的buffer pool區緩存起         來。sql

二、各種consumer:數據庫

      connect:SQL SERVER爲每個客戶端鏈接分配一塊內存,用來存儲鏈接的信息,以及發過來的指令和緩存指令結果待待客戶端取走緩存

      無數據:表、存儲過程、索引等的元數據數據結構

      鎖:SQL SERVER中鎖是稀有資源,會佔用大量內存post

      Query plan:緩存SQL的執行計劃url

      Optimizer:生成執行 計劃過程當中須要使用內存spa

三、線程內存:sql server會爲每一個線程分配0.5M的內存,用來存放線程的數據結構和相關信息.net

 

sql1:線程

-- 查詢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)    --SQL200五、2008
        --, sum(multi_pages_kb)        --SQL200五、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

sql2:

select OBJECT_NAME(object_id) 表名,COUNT(*) 頁數,COUNT(*)*8/1024.0 Mb                            
from   sys.dm_os_buffer_descriptors a,sys.allocation_units b,sys.partitions c                            
where  a.allocation_unit_id=b.allocation_unit_id 
       and b.container_id=c.hobt_id           
       and database_id=DB_ID()                            
group by OBJECT_NAME(object_id)                         
order by 2 desc

 

 

參考:

http://www.cnblogs.com/zhaoguan_wang/p/4602866.html

http://blog.csdn.net/shutao917/article/details/51444424

http://blog.csdn.net/burgess_liu/article/details/52813727

http://blog.csdn.net/burgess_liu/article/details/17739725

http://blog.csdn.net/burgess_liu/article/details/17733149

sql server 各類等待類型-轉

相關文章
相關標籤/搜索