将Azure索引建议作为Json响应获取

时间:2018-05-24 08:01:58

标签: c# azure azure-sql-database

我计划通过实用程序处理azure建议,主要任务是列出订阅下所有数据库的所有索引建议。

我已经尝试查看所有可用的文档,所有其他调用和azure sdk代码,但无法找到通过http请求列出索引的方法。

  1. 我是否可以通过该终点发出http请求并获取所有索引建议?

  2. 我认为索引建议存储在主数据库的一个表中,有些人可以指定在哪个表下查看,我可以查看建议。

  3. 感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

以下查询将返回缺少的索引作为JSON。

create table #results (target_object_name nvarchar(100), equality_columns nvarchar(100), inequality_columns nvarchar(100), included_columns nvarchar(100))

declare @query_plan as xml

declare @totalimpact as float

declare querycursor CURSOR FAST_FORWARD FOR

SELECT top (50) cast(replace(cast(qp.query_plan as nvarchar(max)),'xmlns="http://schemas.microsoft.com/sqlserver/2004/07/showplan"','') as xml),

qp.query_plan.value('declare default element namespace "http://schemas.microsoft.com/sqlserver/2004/07/showplan"; (/ShowPlanXML/BatchSequence/Batch/Statements/StmtSimple/QueryPlan/MissingIndexes/MissingIndexGroup/@Impact)[1]' , 'decimal(18,4)') * execution_count AS TotalImpact

FROM sys.dm_exec_query_stats qs cross apply sys.dm_exec_sql_text(sql_handle) st cross apply sys.dm_exec_query_plan(plan_handle) qp 

WHERE qp.query_plan.exist('declare default element namespace "http://schemas.microsoft.com/sqlserver/2004/07/showplan";/ShowPlanXML/BatchSequence/Batch/Statements/StmtSimple/QueryPlan/MissingIndexes/MissingIndexGroup/MissingIndex[@Database!="m"]') = 1

ORDER BY TotalImpact DESC

OPEN querycursor

FETCH NEXT FROM querycursor

INTO @query_plan, @totalimpact  --need to remove the namespace

WHILE @@FETCH_STATUS=0

BEGIN

insert into #results (target_object_name, equality_columns, inequality_columns, included_columns)

SELECT cast(index_node.query('concat(string((./@Database)[1]),".",string((./@Schema)[1]),".",string((./@Table)[1]))') as nvarchar(100)) as target_object_name

,replace(convert(nvarchar(max), index_node.query('for $colgroup in ./ColumnGroup,$col in $colgroup/Column where $colgroup/@Usage = "EQUALITY" return string($col/@Name)')), '] [', '],[') as equality_columns

,replace(convert(nvarchar(max), index_node.query('for $colgroup in ./ColumnGroup,$col in $colgroup/Column where $colgroup/@Usage = "INEQUALITY" return string($col/@Name)')), '] [', '],[') as inequality_columns

,replace(convert(nvarchar(max), index_node.query('for $colgroup in .//ColumnGroup,$col in $colgroup/Column where $colgroup/@Usage = "INCLUDE"    return string($col/@Name)')), '] [', '],[') as included_columns 

from (select convert(xml, @query_plan) as xml_showplan) as t outer apply xml_showplan.nodes ('//MissingIndexes/MissingIndexGroup/MissingIndex') as missing_indexes(index_node)

 FETCH NEXT FROM querycursor

INTO @query_plan, @totalimpact

END

CLOSE querycursor

DEALLOCATE querycursor

select distinct 'Create NonClustered Index IX_' + substring(replace(replace(target_object_name,'[',''),']',''), 0, charindex('.',replace(replace(target_object_name,'[',''),']',''))) +' On ' + target_object_name + 

' (' + IsNull(equality_columns,'') + 

CASE WHEN equality_columns IS Null And inequality_columns IS Null THEN ',' ELSE '' END + IsNull(inequality_columns, '') + ')' + 

CASE WHEN included_columns='' THEN

';'

ELSE

' Include (' + included_columns + ');'

END 'Index'

from #results
FOR JSON PATH

drop table #results