简体   繁体   English

导出一个SQL计划并清除azure SQL数据库的计划缓存

[英]Export a SQL plan and clear the plan cache of azure SQL database

I was trying to write a powershell script to try find the top 2 running queries on a Azure SQL database and then capture the query plans of those 2 queries and then peform a DBCC cache free and capture those plans again after clearing the cache and send those plans to email using sendgird. I was trying to write a powershell script to try find the top 2 running queries on a Azure SQL database and then capture the query plans of those 2 queries and then peform a DBCC cache free and capture those plans again after clearing the cache and send those计划 email 使用 sendgird。

Issue1: plan handles stored in $delta2 are incorrect and doesn't work when used with sys.dm_exec_query_plan()问题 1:存储在 $delta2 中的计划句柄不正确,并且在与 sys.dm_exec_query_plan() 一起使用时不起作用

Issue 2: if i manage to get the correct handles from the sql studio and use them in $delta3 sys.dm_exec_query_plan() doesn't give any response in PowerShell问题 2:如果我设法从 sql 工作室获得正确的句柄并在 $delta3 sys.dm_exec_query_plan() 中使用它们在 PowerShell 中没有给出任何响应

Issue 3: the Export-clixml corrpts the xml file and doesnt open in SQL studio.问题 3:Export-clixml 损坏了 xml 文件,并且未在 SQL 工作室中打开。

Any help is much appreciated.任何帮助深表感谢。 Thanks in advance.提前致谢。

$params = @{
    'Database' = 'xx'
    'ServerInstance' =  'xx'
    'Username' = 'xx'
    'Password' = 'xxx'
    'OutputSqlErrors' = $true
}
$sqlcmd= "
SELECT TOP 2
    GETDATE() runtime, *
FROM (SELECT  convert(VARCHAR(50), query_stats.query_hash, 1) as query_hash, SUM(query_stats.cpu_time) 'Total_Request_Cpu_Time_Ms', SUM(logical_reads) 'Total_Request_Logical_Reads', MIN(start_time) 'Earliest_Request_start_Time', COUNT(*) 'Number_Of_Requests', SUBSTRING(REPLACE(REPLACE(MIN(query_stats.statement_text), CHAR(10), ' '), CHAR(13), ' '), 1, 256) AS" + '"Statement_Text" '+"
    FROM (SELECT req.*, SUBSTRING(ST.text, (req.statement_start_offset / 2)+1, ((CASE statement_end_offset WHEN -1 THEN DATALENGTH(ST.text)ELSE req.statement_end_offset END-req.statement_start_offset)/ 2)+1) AS statement_text
        FROM sys.dm_exec_requests AS req
                CROSS APPLY sys.dm_exec_sql_text(req.sql_handle) AS ST ) AS query_stats
    GROUP BY query_hash) AS t
ORDER BY Total_Request_Cpu_Time_Ms DESC;"
$delta1 = Invoke-Sqlcmd -query $sqlcmd @params -MaxCharLength 999999 
$qhash = $delta1.query_hash
$delta2 =  "select convert(VARCHAR(100), plan_handle, 1) as plan_handle  from sys.dm_exec_query_stats where query_hash = $qhash"
$getplanhandle = Invoke-Sqlcmd -query $delta2  @params -MaxCharLength 999999 
$getplanhandle
foreach($plan in $getplanhandle)
{
$handle = $plan.plan_handle
$delta3 = "select * from sys.dm_exec_query_plan($plan.plan_handle)"
$saveplan = Invoke-Sqlcmd -query $delta3  @params -MaxCharLength 999999 
$saveplan.query_plan | export-clixml -path ./query_plan.sqlplan
}

You very likely don't need to do most/any of this.您很可能不需要做大部分/任何事情。 It's not obvious from your post as to "why" you are trying to do this, but I'll assume you just want to record the query plan for later performance analysis since that's usually why one would want to look at the plan.从您的帖子中看不到您尝试这样做的“原因”,但我假设您只想记录查询计划以供以后进行性能分析,因为这通常是人们想要查看计划的原因。

The good news is that Azure SQL DB has a feature to automatically capture most query plans for you already - Query Store.好消息是 Azure SQL DB 具有自动为您捕获大多数查询计划的功能 - 查询存储。 It stores them in the database and you can look at the full history of what query plans have been used for a given query over time and how they have performed.它将它们存储在数据库中,您可以查看随着时间的推移对给定查询使用了哪些查询计划以及它们如何执行的完整历史记录。 You can read more about it here: Query Store overview .您可以在此处阅读有关它的更多信息: 查询存储概述 Please note that by default the query store mat not capture all queries (it can overwhelm a database for some kinds of workloads).请注意,默认情况下,查询存储不会捕获所有查询(对于某些类型的工作负载,它可能会使数据库不堪重负)。 If you are doing this on a test system, there is a capture mode "all" which you can use to record all plans.如果您在测试系统上执行此操作,则可以使用“全部”捕获模式来记录所有计划。

If you happen to be trying to determine if you have parameter sensitivity issues for a given query (where the runtime variance can be high if the selectivity of a predicate with a parameter changes significantly and thus causes a given query plan to process few/many rows on different parameter values), you can also see this property in the query store - it will record sum-of-squares variance for you for each query plan it tracks.如果您碰巧试图确定给定查询是否存在参数敏感性问题(如果带有参数的谓词的选择性发生显着变化并因此导致给定查询计划处理少量/多行,则运行时差异可能会很高在不同的参数值上),您还可以在查询存储中看到此属性 - 它会为您记录它跟踪的每个查询计划的平方和方差。 SQL Server Management Studio has a nice UI to help you navigate the query store data. SQL Server Management Studio 有一个很好的 UI 可以帮助您浏览查询存储数据。 You can read more about that here .您可以在此处阅读更多相关信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM