4

我的最终目标是从缓存的执行计划中自动提取所有引用的列。这将帮助我们跟踪我们计划的 SSRS 报告集使用的所有列。

感兴趣的 XML 数据如下所示:

<ColumnReference Database="[AdventureWorksDW2012]" Schema="[dbo]" Table="[DimCustomer]"  Alias="[dC]" Column="HouseOwnerFlag" />

我想将数据库、架构、表、别名和列值存储在一个表中。

但是,为了概念验证,我进行了一个简单的查询,并将完整执行计划的以下部分复制到下面的 TSQL 代码中:

DECLARE @myDoc xml;
SET     @myDoc = '<ShowPlanXML xmlns="http://schemas.microsoft.com/sqlserver/2004/07/showplan"  Version="1.2" Build="11.0.3381.0"  TEST="1">
    <BatchSequence>
      <Batch>
        <Statements>
          <StmtSimple StatementText="SELECT ... &#xD;&#xA;" StatementId="1" StatementCompId="1" ThereIsMoreHere="..." >
            <StatementSetOptions QUOTED_IDENTIFIER="true" ARITHABORT="true" ThereIsMoreHere="..." />
        </StmtSimple>
      </Statements>
    </Batch>
  </BatchSequence>
</ShowPlanXML>';

SELECT StatementId = @myDoc.value('(/ShowPlanXML/BatchSequence/Batch/Statements/StmtSimple/@StatementId)[1]', 'int');
SELECT StatementId = @myDoc.value('(/ShowPlanXML/@TEST)[1]',                                     'int');

两个 SELECT 语句都返回 NULL。这里有什么问题?我觉得我正在慢慢失明。这是针对 SQL Server 2012 SP1 Developers 版本执行的。

4

2 回答 2

6

选择 SQL Server 执行计划中使用的所有列的最终目标解决了:

USE AdventureWorksDW2012

DBCC FREEPROCCACHE

SELECT dC.Gender, dc.HouseOwnerFlag, 
SUM(fIS.SalesAmount) AS SalesAmount 
FROM 
    dbo.DimCustomer dC INNER JOIN
    dbo.FactInternetSales fIS ON fIS.CustomerKey = dC.CustomerKey 
GROUP BY dC.Gender, dc.HouseOwnerFlag
ORDER BY dC.Gender, dc.HouseOwnerFlag
/*
query_hash          query_plan_hash
0x752B3F80E2DB426A  0xA15453A5C2D43765
*/

DECLARE @MyQ AS XML;

-- SELECT qstats.query_hash, query_plan_hash, qplan.query_plan AS [Query Plan],qtext.text 
SELECT @MyQ = qplan.query_plan 
 FROM sys.dm_exec_query_stats AS qstats 
  CROSS APPLY sys.dm_exec_query_plan(qstats.plan_handle) AS qplan 
  cross apply sys.dm_exec_sql_text(qstats.plan_handle) as qtext 
  where text like '% fIS %'
and query_plan_hash = 0xA15453A5C2D43765

SeLeCt @MyQ

;WITH xmlnamespaces (default 'http://schemas.microsoft.com/sqlserver/2004/07/showplan')
SELECT DISTINCT
    [Database] = x.value('(@Database)[1]', 'varchar(128)'),
    [Schema]   = x.value('(@Schema)[1]',   'varchar(128)'),
    [Table]    = x.value('(@Table)[1]',    'varchar(128)'),
    [Alias]    = x.value('(@Alias)[1]',    'varchar(128)'),
    [Column]   = x.value('(@Column)[1]',   'varchar(128)')
FROM   @MyQ.nodes('//ColumnReference') x1(x)

导致以下输出:

Database                  Schema Table            Alias Column
------------------------- ------ ---------------- ----- ----------------
NULL                      NULL   NULL             NULL  Expr1004
[AdventureWorksDW2012]    [dbo]  [DimCustomer]    [dC]  CustomerKey
[AdventureWorksDW2012]    [dbo]  [DimCustomer]    [dC]  Gender
[AdventureWorksDW2012]    [dbo]  [DimCustomer]    [dC]  HouseOwnerFlag
[AdventureWorksDW2012]    [dbo]  [FactInternetSal [fIS] CustomerKey
[AdventureWorksDW2012]    [dbo]  [FactInternetSal [fIS] SalesAmount
于 2013-11-08T06:04:07.013 回答
5

您需要为 xml 指定架构和默认命名空间:-

with xmlnamespaces (default 'http://schemas.microsoft.com/sqlserver/2004/07/showplan')
SELECT StatementId = @myDoc.value('(/ShowPlanXML/BatchSequence/Batch/Statements/StmtSimple/@StatementId)[1]', 'int');

with xmlnamespaces (default 'http://schemas.microsoft.com/sqlserver/2004/07/showplan')
SELECT StatementId = @myDoc.value('(/ShowPlanXML/@TEST)[1]', 'int');
于 2013-11-08T03:13:11.453 回答