Hello Geeks and welcome to the Day 43 of the long series of One DMV a day. In this series of blogs I will be exploring and explaining the most useful DMVs in SQL Server. As we go ahead in this series we will also talk about the usage, linking between DMVs and some scenarios where these DMVs will be helpful while you are using SQL Server. For the complete list in the series please click here.
Today I will be covering sys.dm_exec_cached_plan_dependent_objects. This DMF gives you the information about dependent objects on the current plan. You need to pass the valid plan_handle and get the dependent objects.
Sys.dm_exec_cached_plan_dependent_objects is mapped to sys.dm_os_memory_objects on memory_object_address. This will help you get to the memory object associated with every plan in the cache.
Let us get the details from sys.dm_exec_cached_plan_dependent_objects. I will create procedure to use a cursor and fetch only one row from a table. This way I will have the procedure itself and the cursor as objects for the plan.
CREATE PROCEDURE usp_getRandomEmployee AS BEGIN DECLARE cur1 CURSOR FOR SELECT eId, eName FROM Repl_Pub.dbo.pubLogger_tbl OPEN cur1 FETCH NEXT FROM cur1 CLOSE cur1 DEALLOCATE cur1 END
Now execute the procedure at least once and run the below query to get the plan handle.
SELECT DB_NAME(CAST(db.value AS INT)) AS DBName, OBJECT_NAME(CAST(obj.value AS INT)) AS objName, plan_handle, refcounts, usecounts, pool_id, cacheobjtype, bucketid FROM sys.dm_exec_cached_plans cp (NOLOCK) CROSS APPLY sys.dm_exec_plan_attributes(cp.plan_handle) db CROSS APPLY sys.dm_exec_plan_attributes(cp.plan_handle) obj WHERE db.attribute = 'dbid' AND db.value = DB_ID('repl_pub') AND obj.attribute = 'objectid' AND obj.value = OBJECT_ID('usp_getRandomEmployee')
Pass the plan handle to sys.dm_exec_cached_plan_dependent_objects to see the dependent objects.
SELECT * FROM sys.dm_exec_cached_plan_dependent_objects(0x05000600034DCC6D109E64610100000001000000000000000000000000000000000000000000000000000000)
In the output you can see that it has displayed two objects. The compiled Plan is related to the plan of the procedure. The cursor is related to the cursor inside the procedure. The objects like CLR also are shown when used in sys.dm_exec_cached_plan_dependent_objects.
Tomorrow I will be covering another execution related DMV. Stay tuned. Till then
Like us on FaceBook | Join the fastest growing SQL Server group on FaceBook |
Follow me on Twitter | Follow me on FaceBook