`
chenhsong
  • 浏览: 42495 次
  • 性别: Icon_minigender_1
  • 来自: 洛阳
社区版块
存档分类
最新评论

How Memory is being used

阅读更多

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

 

分享到:
评论

相关推荐

    微软内部资料-SQL性能优化2

    To reserve or commit memory and unintentionally not release it when it is no longer being used. A process can leak resources such as process memory, pool memory, user and GDI objects, handles, threads...

    Learning C__ by Creating Games - William Sherif.mobi

    how data is saved in computer memory, and how a program flows Keep track of player inventory, create monsters, and keep those monsters at bay with basic spell casting by using your C++ programming ...

    操作系统(内存管理)

    /* This is the memory location we will return. It will * be set to 0 until we find something suitable */ void *memory_location; /* Initialize if we haven't already done so */ if(! has_...

    SQL on Big Data(Apress,2016)

    You will understand the architectures of the various SQL engines being used and how the tools work internally in terms of execution, data movement, latency, scalability, performance, and system ...

    Mastering High Performance with Kotlin pdf

    Learn popular design patterns currently being used in Kotlin Understand how to apply modern Kotlin features to data processing Learn how to use profiling tools Discover how to read bytecode Learn to ...

    Professional Assembly Language

    Chapter 9, “Advanced Math Functions,” discusses the IA-32 Floating Point Unit (FPU), and how it is used to handle complex floating-point arithmetic. Floating-point arithmetic is often a crucial ...

    SQL.on.Big.Data.Technology.Architecture.and.Innovation

    You will understand the architectures of the various SQL engines being used and how the tools work internally in terms of execution, data movement, latency, scalability, performance, and system ...

    JavaScript and JSON Essentials2018

    then, explains how JSON is used in mongoDB. Next, it reaches the central theme of performing different operations on mongoDB JSON documents. Chapter 10, Configuring the Task Runner Using JSON, ...

    Mastering Python(PACKT,2016)

    Python being multi-paradigm, it can be used to achieve the same thing in different ways and it is compatible across different platforms. Even if you find writing Python code easy, writing code that ...

    Fundamentals of Kalman Filtering A Practical Approach, Third Edition+代码.zip

    Chapter 19 shows how a bank of linear sine-wave Kalman filters, each one tuned to a different sine-wave frequency, can be used to estimate the actual frequency of noisy sinusoidal measurements and ...

    uhdd.sys源码

    and it can also load in 640K DOS memory RDISK is a simple and small RAMdisk driver for use when resizing or other features are not needed UIDE is a DOS "Universal IDE" caching driver It...

    Senfore_DragDrop_v4.1

    Even if you have used previous versions of the Drag and Drop Component Suite it would be a good idea to have a quick look at the demos. The library has been completely rewritten and a lot of new ...

    Kenneth Reek - Pointers on C

    Dynamic memory allocation has been at times a somewhat painful aspect of programming in C, but the author shows how to do straightforwardly in the book. Having a book like this that is predominantly...

    微软内部资料-SQL性能优化5

    The main difference between the two types of indexes is how much information is stored at the leaf. The leaf levels of both types of indexes contain all the key values in order, but they also contain ...

    Scala and Spark for Big Data Analytics

    Spark, built on Scala, has gained a lot of recognition and is being used widely in productions. Thus, if you want to leverage the power of Scala and Spark to make sense of big data, this book is for ...

    kgb档案压缩console版+源码

    The -3 is optional, and is used to trade off compression vs. speed and memory. Valid options are -0 to -9. Higher numbers compress better but run slower and use more memory. -3 is the default, and ...

    ora分析脚本

    - pga: tell how much pga memory is used - pga_detail | -mem <size_mb>: Gives details on how PGA memory is consumed by a process (given its os PID) or by the set of precesses consuming more than ...

    JavaScript Applications with Node.js, React, React Native and MongoDB

    This is due to the huge developer community and it being used in every layer of an application technology stack. This book will teach you how to develop JavaScript applications with simple to use, ...

    Advanced Apple Debugging & Reverse Engineering v0.9.5

    For the final chapter in this section, you’ll go through the same steps I myself took to understand how the MallocStackLogging environment variable is used to get the stack trace when an object is ...

    The.Data.Science.Handbook.pdf

    Core technologies of “Big Data,” including their strengths and limitations and how they can be used to solve real-world problems Coverage of the practical realities of the tools, keeping theory to a...

Global site tag (gtag.js) - Google Analytics