2010-11-17 51 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

相關問題