2012-05-23 82 views
0

存儲過程是:存儲過程的執行採取過很多次

CREATE PROCEDURE [dbo].[spManagedRouteSheetList] 
@dStartDate DateTime, 
@dEndDate DateTime 
AS 
BEGIN 
SELECT IT.TriggerParentID,IT.ProductID, IT.[Weight],IT.[DateTime],IT.ProductType,IT.InOutType,IT.TriggerParentType FROM ITransaction IT 
    WHERE IT.InOutType=101 and IT.LotID in (select ManagedLotID from RSInQCDetail where YarnFormat <>4 and ManagedLotID>0) 
    AND IT.[DateTime]>[email protected] AND IT.[DateTime]<@dEndDate 

    --YarnCategory 
    SELECT YarnCategoryID,Name,Code FROM YarnCategory WHERE YarnCategoryID IN (SELECT Distinct ProductID FROm ITransaction IT WHERE IT.InOutType=101 and IT.LotID in (select ManagedLotID from RSInQCDetail where YarnFormat <>4 and ManagedLotID>0) 
    AND IT.[DateTime]>[email protected] AND IT.[DateTime]<@dEndDate) 

    --RouteSheet 
    SELECT RouteSheetID,RoutesheetNo, ReqYarnQty, [date],RSState,Shift,Subfactory,(SELECT Name FROM Location WHERE LocationID=Subfactory) 
    FROM Routesheet where RoutesheetID IN (SELECT distinct IT.TriggerParentID FROM ITransaction IT WHERE IT.InOutType=101 and IT.LotID in (select ManagedLotID from RSInQCDetail where YarnFormat <>4 and ManagedLotID>0) 
    AND IT.[DateTime]>[email protected] AND IT.[DateTime]<@dEndDate) 

    --RSInQCDetail 
    SELECT RouteSheetID,Qty,YarnFormat,ManagedLotID FROM RSInQCDetail WHERE RouteSheetID IN  (SELECT distinct IT.TriggerParentID FROM ITransaction IT WHERE IT.InOutType=101 and IT.LotID in (select ManagedLotID from RSInQCDetail where YarnFormat <>4 and ManagedLotID>0) 
    AND IT.[DateTime]>[email protected] AND IT.[DateTime]<@dEndDate) 


    --RouteSheet History 
    SELECT RouteSheetID,YarnQtyInLBS,[Event] from RouteSheetHistoryEnhance WHERE EVENt IN (14) 
    AND RouteSheetID IN (SELECT distinct IT.TriggerParentID FROM ITransaction IT WHERE IT.InOutType=101 and IT.LotID in 
    (select ManagedLotID from RSInQCDetail where YarnFormat <>4 and ManagedLotID>0) 
    AND IT.[DateTime]>[email protected] AND IT.[DateTime]<@dEndDate) 

END 

執行過程:

EXECUTE dbo.[spManagedRouteSheetList] '21 May 2012 08:00:00','22 May 2012 08:00:00'.. 

這將需要2個異位但如果我執行以下查詢它只有1或3取秒

DECLARE @dStartDate as datetime 
     ,@dEndDate as datetime 

     SET @dStartDate='21 May 2012 08:00:00' 
     SET @dEndDate='22 May 2012 08:00:00' 


SELECT IT.TriggerParentID,IT.ProductID, IT.[Weight],IT.[DateTime],IT.ProductType,IT.InOutType,IT.TriggerParentType FROM ITransaction IT 
    WHERE IT.InOutType=101 and IT.LotID in (select ManagedLotID from RSInQCDetail where YarnFormat <>4 and ManagedLotID>0) 
    AND IT.[DateTime]>[email protected] AND IT.[DateTime]<@dEndDate 

    --YarnCategory 
    SELECT YarnCategoryID,Name,Code FROM YarnCategory WHERE YarnCategoryID IN (SELECT Distinct ProductID FROm ITransaction IT WHERE IT.InOutType=101 and IT.LotID in (select ManagedLotID from RSInQCDetail where YarnFormat <>4 and ManagedLotID>0) 
    AND IT.[DateTime]>[email protected] AND IT.[DateTime]<@dEndDate) 

    --RouteSheet 
    SELECT RouteSheetID,RoutesheetNo, ReqYarnQty, [date],RSState,Shift,Subfactory,(SELECT Name FROM Location WHERE LocationID=Subfactory) 
    FROM Routesheet where RoutesheetID IN (SELECT distinct IT.TriggerParentID FROM ITransaction IT WHERE IT.InOutType=101 and IT.LotID in (select ManagedLotID from RSInQCDetail where YarnFormat <>4 and ManagedLotID>0) 
    AND IT.[DateTime]>[email protected] AND IT.[DateTime]<@dEndDate) 

    --RSInQCDetail 
    SELECT RouteSheetID,Qty,YarnFormat,ManagedLotID FROM RSInQCDetail WHERE RouteSheetID IN  (SELECT distinct IT.TriggerParentID FROM ITransaction IT WHERE IT.InOutType=101 and IT.LotID in (select ManagedLotID from RSInQCDetail where YarnFormat <>4 and ManagedLotID>0) 
    AND IT.[DateTime]>[email protected] AND IT.[DateTime]<@dEndDate) 


    --RouteSheet History 
    SELECT RouteSheetID,YarnQtyInLBS,[Event] from RouteSheetHistoryEnhance WHERE EVENt IN (14) 
    AND RouteSheetID IN (SELECT distinct IT.TriggerParentID FROM ITransaction IT WHERE IT.InOutType=101 and IT.LotID in `` 
    (select ManagedLotID from RSInQCDetail where YarnFormat <>4 and ManagedLotID>0) 
    AND IT.[DateTime]>[email protected] AND IT.[DateTime]<@dEndDate) 

所以..存儲過程有什麼問題?

+1

執行計劃說什麼? – sharptooth

+0

這是哪個數據庫? (postgres,mysql等 - 請添加適當的標籤) – Bohemian

+2

參數嗅探?看一眼; http://www.sommarskog.se/query-plan-mysteries.html –

回答

0

如果這是SQL Server ...

這不一定是一個完美的解決方案,但它很可能消除你看到其中的差別。

嘗試將WITH RECOMPILE添加到存儲過程定義中。

CREATE PROCEDURE [dbo].[spManagedRouteSheetList] 
    @dStartDate DateTime, 
    @dEndDate DateTime 
WITH RECOMPILE 
AS ... 
+0

我試圖將「WITH RECOMPILE」添加到存儲過程定義中,但不起作用。 –

+0

@ Md.MasudIqbal您使用MS SQL Server嗎? –

+0

是的,我正在使用SQL Server 2008 –