February 23, 2016 at 10:45 am
I'm sure there's a way to query this, but I'd like to generate a query that outputs a list of all indexes projected to be used in a given execution plan. Anyone know how to do this?
February 23, 2016 at 10:48 am
This does the opposite, but could be a good starting point:
-- Gianluca Sartori
February 23, 2016 at 11:17 am
This will get you started. You just have to look at the right properties to only see the indexes
WITH XMLNAMESPACES(DEFAULT N'http://schemas.microsoft.com/sqlserver/2004/07/showplan'),
QueryPlans AS
(
SELECT RelOp.pln.value(N'@PhysicalOp', N'varchar(50)') AS OperatorName,
RelOp.pln.value(N'@NodeId',N'integer') AS NodeId,
RelOp.pln.value(N'@EstimateCPU', N'decimal(10,9)') AS CPUCost,
RelOp.pln.value(N'@EstimateIO', N'decimal(10,9)') AS IOCost,
dest.text
FROM sys.dm_exec_query_stats AS deqs
CROSS APPLY sys.dm_exec_sql_text(deqs.sql_handle) AS dest
CROSS APPLY sys.dm_exec_query_plan(deqs.plan_handle) AS deqp
CROSS APPLY deqp.query_plan.nodes(N'//RelOp') RelOp (pln)
)
SELECT qp.OperatorName,
qp.NodeId,
qp.CPUCost,
qp.IOCost,
qp.CPUCost + qp.IOCost AS EstimatedCost
FROM QueryPlans AS qp
WHERE qp.text = 'SELECT * FROM HumanResources.vEmployee AS ve;'
ORDER BY EstimatedCost DESC;
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
- Theodore Roosevelt
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
February 24, 2016 at 1:46 am
Great script Grant.
Added to my briefcase.
-- Gianluca Sartori
February 24, 2016 at 3:51 am
Thanks.
I should add though, that queries like this can be somewhat expensive. I wouldn't recommend running them under load on a production system.
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
- Theodore Roosevelt
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
February 24, 2016 at 4:35 am
Actually, I never perform cache analysis in production.
I usually dump the whole cache to a table in my local sql server and start from there.
-- Gianluca Sartori
February 24, 2016 at 7:28 am
spaghettidba (2/24/2016)
Actually, I never perform cache analysis in production.I usually dump the whole cache to a table in my local sql server and start from there.
Good plan. And, you can always read a .SQLPLAN file if you want.
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
- Theodore Roosevelt
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
February 24, 2016 at 10:33 am
spaghettidba (2/24/2016)
Actually, I never perform cache analysis in production.I usually dump the whole cache to a table in my local sql server and start from there.
excuse the neophyte question, but how do you grab the cache?
do you mean a typical sql that is grabbing from the dm views and putting the results into a temp/permanent table, or are you using some other magic?
Lowell
February 24, 2016 at 10:45 am
No magic at all. All I do is dump this...
SELECT *
FROM sys.dm_exec_cached_plans
CROSS APPLY sys.dm_exec_query_plan(plan_handle) AS p
... to a a file with BCP.
Than I BCP in the file to my local sql server, to a regular table.
-- Gianluca Sartori
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply