2010-11-17 61 views
1

我有一个存储过程,我已经开发了SQL2008服务器上运行< 1秒。在另一台SQL2005服务器上,同一个数据库上的同一个服务器需要大约1分钟的时间。没有深入了解数据库模式的细节,任何人都可以在此SP中看到任何可能导致此性能差异的显而易见的内容?这可能是CTE的使用吗?有其他选择吗?SQL存储过程性能很好SQL2008,但在SQL2005很糟糕

编辑 - 我现在已经注意到,如果我直接在SQL 2005上运行SQL它运行在〜4secs但执行SP仍需要一分钟?看起来问题可能会在SP执行?

CREATE PROCEDURE Workflow.GetTopTasks 
    -- Add the parameters for the stored procedure here 
    @ownerUserId int, 
    @topN int = 10 
AS 
BEGIN 
    -- SET NOCOUNT ON added to prevent extra result sets from 
    -- interfering with SELECT statements. 
    SET NOCOUNT ON; 

    SET ROWCOUNT @topN; 

    -- Insert statements for procedure here 

WITH cteCalculatedDate (MilestoneDateId, CalculatedMilestoneDate) 
AS 
(
-- Anchor member definition 
    SELECT md.MilestoneDateId, md.SpecifiedDate 
    FROM Workflow.MilestoneDate md 
    WHERE md.RelativeMilestoneDateId IS NULL 
UNION ALL 
-- Recursive member definition 
    SELECT md.MilestoneDateId, CalculatedMilestoneDate + md.RelativeDays 
    FROM Workflow.MilestoneDate md 
      INNER JOIN cteCalculatedDate cte 
       on md.RelativeMilestoneDateId = cte.MilestoneDateId 
) 

-- Statement that executes the CTE 

    select 
     we.* 
    from Workflow.WorkflowElement we 
     left outer join cteCalculatedDate cte 
      on cte.MilestoneDateId = we.DueDateId 
     inner join Workflow.WorkflowInstance wi 
      on wi.WorkflowInstanceId = we.WorkflowInstanceId 
     left outer join Workflow.SchemeWorkflow sw 
      on sw.WorkflowInstanceId = wi.WorkflowInstanceId 
     left outer join Workflow.Scheme s 
      on s.SchemeId = sw.SchemeId 
     inner join Workflow.WorkflowDefinition wd 
      on wd.WorkflowDefinitionId = wi.WorkflowDefinitionId 
    where 
     we.OwnerId = @ownerUserId   -- for given owner 
     and we.CompletedDate is null  -- is not completed 
     and we.ElementTypeId <= 4   -- is Action, Data, Decision or Document (Not End, Start or KeyDate) 
     and cte.CalculatedMilestoneDate is not null -- has a duedate 

    UNION 

    select 
     we.* 
    from Workflow.WorkflowElement we 
     left outer join cteCalculatedDate cte 
      on cte.MilestoneDateId = we.DueDateId 
     inner join Workflow.WorkflowInstance wi 
      on wi.WorkflowInstanceId = we.WorkflowInstanceId 
     left outer join Workflow.SchemeWorkflow sw 
      on sw.WorkflowInstanceId = wi.WorkflowInstanceId 
     left outer join Workflow.Scheme s 
      on s.SchemeId = sw.SchemeId 
     inner join Workflow.WorkflowDefinition wd 
      on wd.WorkflowDefinitionId = wi.WorkflowDefinitionId 
    where 
     we.OwnerId = @ownerUserId   -- for given owner 
     and we.CompletedDate is null  -- is not completed 
     and we.ElementTypeId <= 4   -- is Action, Data, Decision or Document (Not End, Start or KeyDate) 
     and cte.CalculatedMilestoneDate is null -- does NOT have a duedate 

    SET ROWCOUNT 0 

END 
+2

更好邮政查询执行计划的双方2008/2005年数据库。这可能有助于更好地了解为什么2005年会更慢 – InSane 2010-11-17 08:13:20

回答

3

您的工会正在选择CalculatedMilestoneDate等于NULL 不等于Null。

这是多余的,只需从where子句中去除CalculatedMilestoneDate上的条件即可删除整个UNION。

除此之外,您应该验证两个数据库是否具有相同的索引定义。

-- Statement that executes the CTE 

    select 
     we.* 
    from Workflow.WorkflowElement we 
     left outer join cteCalculatedDate cte 
      on cte.MilestoneDateId = we.DueDateId 
     inner join Workflow.WorkflowInstance wi 
      on wi.WorkflowInstanceId = we.WorkflowInstanceId 
     left outer join Workflow.SchemeWorkflow sw 
      on sw.WorkflowInstanceId = wi.WorkflowInstanceId 
     left outer join Workflow.Scheme s 
      on s.SchemeId = sw.SchemeId 
     inner join Workflow.WorkflowDefinition wd 
      on wd.WorkflowDefinitionId = wi.WorkflowDefinitionId 
    where 
     we.OwnerId = @ownerUserId   -- for given owner 
     and we.CompletedDate is null  -- is not completed 
     and we.ElementTypeId <= 4   -- is Action, Data, Decision or Document (Not End, Start or KeyDate) 
+1

联合的原因是我想要获得所有的行,首先是CalculatedMilestoneDate(CMD),然后是没有的行。我希望排序顺序为CMD递增,但是对于具有NULL CMD的行最后不能排在第一位。我现在已经删除了该工会,并通过ISNULL(CMD,'9999-12-31')添加了一个订单,以确保这些空位是最后一个。谢谢 – 2010-11-17 09:34:45

+1

联盟可能已经为你工作,但有一些问题。由于不使用UNION ALL,因此强制SQL Server删除重复的行*(没有任何但不好的SQL Server不知道)*。如果我没有弄错,通过排序和合并两个数据集来完成删除重复行,这是一个耗时的过程。此外,在没有明确指定排序顺序的情况下,返回的极限记录的顺序本质上是任意的,*可能会随着下一个版本或当前版本的更新而改变。你不应该依赖那个,而应该关注你的排序顺序。 – 2010-11-17 10:35:40

0

如果模式匹配,那么你可能在sql server 2005实例中缺少重要的索引。尝试运行sql server调优顾问并应用其索引建议。

+0

我已在2008年恢复了2005年数据库并获得此i所以我知道这些索引是相同的 – 2010-11-17 08:19:55

相关问题