我正在使用 SQL Server 2016/2019。我想从缓存中的执行计划 XML 中提取(对于给定的
@QueryPlanHash BINARY(8) = 0x397CEDB37FA0E1D2
) - 哪些统计信息用于生成该计划。
XML 计划中的这一部分如下所示:
<OptimizerStatsUsage>
<StatisticsInfo Database="[MyDatabaseName]" Schema="[dbo]" Table="[MyTable_1]"
Statistics="[IX_MyTable_1_Field1]" ModificationCount="2"
SamplingPercent="100" LastUpdate="2024-06-13T13:39:04.41" />
<StatisticsInfo Database="[MyDatabaseName]" Schema="[dbo]" Table="[MyTable_2]"
Statistics="[_WA_Sys_00000015_4B4D17CD]" ModificationCount="0"
SamplingPercent="100" LastUpdate="2024-06-13T12:06:33.17" />
</OptimizerStatsUsage>
如果可能的话我想要信息:
我建议使用Powershell。 您可以使用 cmdlet Invoke-SQLCMD 访问数据库。 当您检索 xml 时,您可以使用以下方法来解析 xml
using assembly System.Xml.Linq
$input_filename = 'c:\temp\test.xml'
$doc = [System.Xml.Linq.XDocument]::Load($input_filename)
$OptimizerStatsUsage = $doc.Descendants('OptimizerStatsUsage')[0]
$table = [System.Collections.ArrayList]::new()
foreach($statisticsInfo in $OptimizerStatsUsage[0].Elements('StatisticsInfo'))
{
$schema = $statisticsInfo.Attribute('Schema').Value
$xTable = $statisticsInfo.Attribute('Table').Value
$statistics = $statisticsInfo.Attribute('Statistics').Value
$modificationCount = $statisticsInfo.Attribute('ModificationCount').Value
$samplingPercent = $statisticsInfo.Attribute('SamplingPercent').Value
$lastUpdate = $statisticsInfo.Attribute('LastUpdate').Value
$newRow = [pscustomobject]@{
Schema = $schema.Trim(@('[',']'))
Table = $xTable.Trim(@('[',']'))
Statistics = $statistics.Trim(@('[',']'))
ModificationCount = [int]$modificationCount
SamplingPercent = [decimal]$samplingPercent
LastUpdate = [DateTime]::Parse($lastUpdate)
}
$table.Add($newRow) | out-null
}
$table
结果
Schema : dbo
Table : MyTable_1
Statistics : IX_MyTable_1_Field1
ModificationCount : 2
SamplingPercent : 100
LastUpdate : 6/13/2024 1:39:04 PM
Schema : dbo
Table : MyTable_2
Statistics : _WA_Sys_00000015_4B4D17CD
ModificationCount : 0
SamplingPercent : 100
LastUpdate : 6/13/2024 12:06:33 PM