查看SqlServer的內存使用情況


SQL SERVER內存按存放數據的類型,大概可以分為三類:

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

2、各類consumer:

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

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

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

      Query plan:緩存SQL的執行計划

      Optimizer:生成執行 計划過程中需要使用內存

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

 

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)    --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

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 各種等待類型-轉


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM