存储过程执行需要太多次

时间:2012-05-23 12:57:05

标签: sql sql-server

存储过程是:

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]>=@dStartDate 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]>=@dStartDate 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]>=@dStartDate 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]>=@dStartDate 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]>=@dStartDate 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]>=@dStartDate 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]>=@dStartDate 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]>=@dStartDate 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]>=@dStartDate 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]>=@dStartDate AND IT.[DateTime]<@dEndDate)

那么..存储过程有什么问题?

1 个答案:

答案 0 :(得分:0)

如果这是针对SQL Server ...

这不一定是一个完美的解决方案,但它可能会消除你看到的差异。

尝试将WITH RECOMPILE添加到存储过程定义中。

CREATE PROCEDURE [dbo].[spManagedRouteSheetList]
    @dStartDate DateTime,
    @dEndDate DateTime
WITH RECOMPILE
AS ...
相关问题