2016-09-07 9 views
0

条件とその関数に基づいて合計を計算しているスカラー関数がありますが、私はストアードプロシージャーを使用しています。それは実行するのに時間がかかりすぎる。 25レコード分は約4分かかります。私は自分のクエリを最適化できるという考えはありますか?どんな助けもありがとう。スカラー関数で時間がかかりすぎてクエリを実行する

ここは私のコードです。 ストアドプロシージャの動的クエリここでは、関数を呼び出しています。ここ

select distinct 
         p.ProductID 
         ,p.ProductGUID 
         ,p.[ProductNumber] 
         ,[ProductName] = isnull(p.[Name],'''') 
         ,[ProductNumberLabel] = isnull(p.[ProductNumberLabel],'''') 
         ,[ProductDescription]=isnull(p.[Description],'''') 
         ,[PrimaryImageID] = isnull(p.[PrimaryImageID],0) 
         ,[UserProductID] = isnull(p.[UserProductID],0) 
         ,[OrganizationID] = isnull(p.[OrganizationID],0) 
         ,[BusinessUnitID] = isnull(I.[BusinessUnitID],0) 
         ,[BusinessUnitName] = isnull(bu.[Name],0) 
         ,[OwnerUserGroupID] = isnull(p.[OwnerUserGroupID],0) 
         ,[QuantityOnHand]= (select [dbo].[TotalCalculatedSum] (p.ProductID,''QuantityOnHand'', '[email protected]+', '[email protected]+', I.BusinessUnitID , '[email protected]+')) 
         ,[QuantityBooked] = (select [dbo].[TotalCalculatedSum] (p.ProductID,''QuantityBooked'', '[email protected]+', '[email protected]+', I.BusinessUnitID, '[email protected]+')) 
    from dbo.Product p 
    left join Inventory I on II.InventoryID = I.InventoryID 
    left join dbo.BusinessUnit bu on I.BusinessUnitID=bu.[BusinessUnitID] and bu.[ActiveStatus]=1 
    where p.ActiveStatus = 1 
    and bu.[ActiveStatus]=1 

は、あなたが気づいたように、遅いSELECT文のスカラ値関数を呼び出す合計

ALTER FUNCTION [dbo].[TotalCalculatedSum] 
    ( 
    @ProductID bigint, 
    @TotalType nvarchar(200), 
    @OwnerUserID bigint, 
    @OrganizationID bigint, 
    @BusinessUnitID bigint, 
    @InventoryID bigint 
) 
    RETURNS decimal(32,9) 
    AS 
    BEGIN 
    -- declare the return variable here 
    declare @OutputValue decimal(32,9) 
    Declare @locationValue int =0 

    IF @TotalType = 'QuantityOnHand' 
    BEGIN 
    set @OutputValue = isnull((select sum(ii.[QuantityOnHand]) 
    from dbo.InventoryItems ii, Inventory i 
    where ii.ActiveStatus=1 
    and ii.ProductID = @ProductID 
    and ii.InventoryID = i.InventoryID 
    AND i.OwnerUserGroupID = case @OwnerUserID 
    when 0 then i.OwnerUserGroupID else @OwnerUserID end 
    AND i.OrganizationID = case @OrganizationID 
    when 0 then i.OrganizationID else @OrganizationID end 
    AND i.BusinessUnitID = case @BusinessUnitID 
    when 0 then i.BusinessUnitID else @BusinessUnitID end 
    AND i.InventoryID = case @InventoryID 
    when 0 then i.InventoryID else @InventoryID end), 0.00) 
    END 
    ELSE IF @TotalType = 'QuantityBooked' 
    BEGIN 
    set @OutputValue = isnull((select sum(ii.QuantitySold) 
    from dbo.InventoryItems ii, Inventory i 
    where ii.ActiveStatus=1 
    and ii.ProductID = @ProductID 
    and ii.InventoryID = i.InventoryID 
    AND i.OwnerUserGroupID = case @OwnerUserID 
    when 0 then i.OwnerUserGroupID else @OwnerUserID end 
    AND i.OrganizationID = case @OrganizationID 
    when 0 then i.OrganizationID else @OrganizationID end 
    AND i.BusinessUnitID = case @BusinessUnitID 
    when 0 then i.BusinessUnitID else @BusinessUnitID end 
    AND i.InventoryID = case @InventoryID 
    when 0 then i.InventoryID else @InventoryID end), 0.00) 
    END 
    return @OutputValue 


    END 
+2

私は、この古い方法ではなく内部結合の構文を使うことから始めるだろう。 i.InventoryID = i.InventoryID ii.ActiveStatus = 1と... – GuidoG

+0

dbo.InventoryItems iiからインベントリiをインベントリiに結合するクエリ実行計画を見て、インデックスを推薦するかどうかを確認しましたか?また、ボトルネックがどこにあるかを示すかもしれません。 – Cato

答えて

関連する問題