Azure上的SQL递归CTE超时

时间:2015-10-19 23:12:38

标签: sql sql-server azure

我有以下视图,当它在Azure上运行时会出现严重的性能问题。如果我在本地复制Azure数据库并在视图上运行任何查询,则会在几秒钟内返回。对视图的相同查询在Azure上超时。

为什么Azure上的查看时间会超时?

         CREATE VIEW [dbo].[JobTypeDescendantJobTypes] 
            AS 
            WITH    cte ( ParentJobTypeId, Id ) 
                      AS ( SELECT   Id , 
                                    Id 
                           FROM     dbo.JobTypes 
                           UNION ALL 
                           SELECT   e.Id , 
                                    cte.Id 
                           FROM     cte 
                                    INNER JOIN dbo.JobTypes AS e ON e.ParentJobTypeId = cte.ParentJobTypeId 
                         ) 
            SELECT  ISNULL(Id, 0) AS ParentJobTypeId,
                    ISNULL(ParentJobTypeId, 0) AS Id
            FROM    cte

1 个答案:

答案 0 :(得分:1)

重建您的统计数据/索引。

执行此操作的一种方法是:(*注意:在生产中谨慎运行)

DECLARE @table nvarchar(255) 
DECLARE tablecursor CURSOR FOR
SELECT table_name FROM information_schema.tables WHERE table_type = 'base table'

OPEN tablecursor 
FETCH NEXT FROM tablecursor INTO @table

WHILE @@FETCH_STATUS = 0 
BEGIN
  exec('ALTER INDEX ALL ON ' + @table + ' REBUILD')
  FETCH NEXT FROM tablecursor INTO @table
END

CLOSE tablecursor 
DEALLOCATE tablecursor