How Memory is being used

本文介绍如何使用SQL Server的动态管理视图(dmvs)来检查和优化缓存中的查询计划及数据库缓冲池。通过具体SQL查询示例,展示了如何确定哪些查询最常被使用,以及哪些数据库对象消耗最多内存。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

To determine what plans are in the cache and how often they're used we can use sys.dm_os_memory_cache_counters dm view .

 

SELECT  TOP 6
 LEFT([name], 20) as [name],
 LEFT([type], 20) as [type],
 [single_pages_kb] + [multi_pages_kb] AS cache_kb,
 [entries_count]
FROM sys.dm_os_memory_cache_counters 
order by single_pages_kb + multi_pages_kb DESC

 here :

CACHESTORE_OBJCP are compiled plans for stored procedures, functions and triggers. 
CACHESTORE_SQLCP are cached SQL statements or batches that aren't in stored procedures, functions and triggers.  This includes any dynamic SQL or raw SELECT statements sent to the server. 
CACHESTORE_PHDR  These are algebrizer trees for views, constraints and defaults.  An algebrizer tree is the parsed SQL text that resolves the table and column names.

(you will find these counters in DBCC Memorystatus as well.Infact DBCC Memory Status uses this dm)

Generally you will find that CACHESTORE_SQLCP > CACHESTORE_OBJCP , but if the ratio of one to another is very high then we can say that there are more adhoc plans being run then Stored procedures.
That is the reason the sal statements are going in to  Plan cache.

You can also monitor the number of data pages in the plan cache using Performance Monitor (PerfMon) using SQLServer:Plan Cache object with the Cache Pages counter.  There are instances for SQL Plans (CACHESTORE_SQLCP), Object Plans (CACHESTORE_OBJCP) and Bound Trees (CACHESTORE_PHDR). This will give you the same picture ..for e.g. under bound tree : multiply cache pages by 8. you will get the same output as in dbcc memorystatus and the dm we used above.

After this to know the querry we can use sys.dm_exec_cached_plans and sys.dm_exec_sql_text dm views to find the queries

select  TOP 100
 objtype,
    usecounts, 
 p.size_in_bytes/1024 'IN KB',
 LEFT([sql].[text], 100) as [text]
from sys.dm_exec_cached_plans p
outer apply sys.dm_exec_sql_text (p.plan_handle) sql
ORDER BY usecounts DESC

 Now , SQL Server memory is primarily used to store data (buffer) and query plans (cache). 

We will try to find what tables and indexes are in the buffer memory of your server you can use sys.dm_os_buffer_descriptors DMV.

Further , the query below can give us total currrent size of buffer pool .

select count(*) AS Buffered_Page_Count
 ,count(*) * 8192 / (1024 * 1024) as Buffer_Pool_MB
from sys.dm_os_buffer_descriptors

 After we have found the Bufferpool size , we can see which database is using more memory by runnig the query below .

SELECT LEFT(CASE database_id 
   WHEN 32767 THEN 'ResourceDb' 
   ELSE db_name(database_id) 
        END, 20) AS Database_Name,
 count(*)AS Buffered_Page_Count, 
 count(*) * 8192 / (1024 * 1024) as Buffer_Pool_MB
FROM sys.dm_os_buffer_descriptors
GROUP BY db_name(database_id) ,database_id
ORDER BY Buffered_Page_Count DESC

 And then we can go further at object level to see what all objects are consuming memory (and how much) .We can use the query below in each database we wish to :

SELECT TOP 25 
 obj.[name],
 i.[name],
 i.[type_desc],
 count(*)AS Buffered_Page_Count ,
 count(*) * 8192 / (1024 * 1024) as Buffer_MB
    -- ,obj.name ,obj.index_id, i.[name]
FROM sys.dm_os_buffer_descriptors AS bd 
    INNER JOIN 
    (
        SELECT object_name(object_id) AS name 
            ,index_id ,allocation_unit_id, object_id
        FROM sys.allocation_units AS au
            INNER JOIN sys.partitions AS p 
                ON au.container_id = p.hobt_id 
                    AND (au.type = 1 OR au.type = 3)
        UNION ALL
        SELECT object_name(object_id) AS name   
            ,index_id, allocation_unit_id, object_id
        FROM sys.allocation_units AS au
            INNER JOIN sys.partitions AS p 
                ON au.container_id = p.hobt_id 
                    AND au.type = 2
    ) AS obj 
        ON bd.allocation_unit_id = obj.allocation_unit_id
LEFT JOIN sys.indexes i on i.object_id = obj.object_id AND i.index_id = obj.index_id
WHERE database_id = db_id()
GROUP BY obj.name, obj.index_id , i.[name],i.[type_desc]
ORDER BY Buffered_Page_Count DESC

 

Write a program to simulate a process of translation from a logical address to physical address. Assumptions 1. Assume the file la.txt includes the sequence of generated addresses from CPU. 2. Use a part of memory as backing store that store data for a process. 3. The backing store size is 128 bytes 4. The size of process p is 128 bytes. 5. The contents of p is included in a file pdata.bin which is a binary file. 6. Use a part of memory as RAM. The size of physical memory is 256 bytes, from 0 to 255. All the physical memory is available, allocating starting from beginning in sequence. That is, allocate frame 0 first, then frame 1, then frame 2…. 7. The size of a frame is 32 bytes, i.e., 5 bits for the offset in a frame, total number of frames is 8. 8. At beginning, no page table is available for process p. Requirements Write a program to 1. Setup a simulating backing store in memory. Read the data from pdata.bin to this backing store. 2. Initialize a page table for process p, set the frame number to be -1 for each page, indicating that the page is not loaded into memory yet. 3. Read logical addresses one by one from la.txt. 4. For each logical address, a) if its page has been loaded into physical memory, simply find the frame number in the page table, then generate physical address, find and print out the physical address and data inside this address. b) if the page is used for the first time, i.e., in page table, its frame number is -1,then the page that contains this address should be loaded into a free frame in physical memory (RAM). Then update the page table by adding the frame number to the right index in the page table. Then repeat 4a). Refer to Figure 1 for the relationships and how physical memory, backing store, and CPU are simulated.写一个c文件
05-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值