Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 我可以使这个存储过程更快吗_Sql_Sql Server_Performance_Subquery - Fatal编程技术网

Sql 我可以使这个存储过程更快吗

Sql 我可以使这个存储过程更快吗,sql,sql-server,performance,subquery,Sql,Sql Server,Performance,Subquery,我有下面的存储过程。即使在我的开发人员计算机上,此存储过程也可以在3-4秒内运行,但在服务器上则需要15-20秒。我尝试将一些子查询更改为交叉应用,将一些子查询更改为外部应用。但这会导致花费更长的时间 @startDate datetime , @endDate datetime , @customerId int ;WITH t1(Plate,UsedFuelTypeUID,RemainingBefore,DateRangeTotal,DateRangeTransactionsTotal,R

我有下面的存储过程。即使在我的开发人员计算机上,此存储过程也可以在3-4秒内运行,但在服务器上则需要15-20秒。我尝试将一些子查询更改为交叉应用,将一些子查询更改为外部应用。但这会导致花费更长的时间

@startDate datetime ,
@endDate datetime ,
@customerId int

;WITH t1(Plate,UsedFuelTypeUID,RemainingBefore,DateRangeTotal,DateRangeTransactionsTotal,RemaininCurrent) AS
(
select  
v.Plate, v.UsedFuelTypeUID,

isnull((select isnull( sum(vls1.FT_TotalLimit),0) 
    from VehicleChildLog vls1 
    where vls1.VehicleChildId =v.VehicleID and vls1.UpdateDate < @startDate  
    ),0)-
isnull((select isnull( sum(t1.Liter),0) from Transactions t1
    where t1.VehicleChildID=v.VehicleID and t1.SaleDate <@startDate
    ),0)as RemainingBefore,

sum(vl.FT_TotalLimit) DateRangeTotal,

isnull((select isnull( sum(t1.Liter),0) from Transactions t1
where t1.VehicleChildID=v.VehicleID and t1.SaleDate between @startDate and @endDate
),0) as DateRangeTransactionsTotal,

(v.FT_TotalLimit - v.FT_UsedTotalLimit) as RemainingCurrent

from VehicleChildLog vl
inner join VehiclesChild v on vl.VehicleChildId = v.VehicleID
where vl.CustomerChildID = @customerId and vl.UpdateDate between @startDate and @endDate
group by 
v.VehicleID, v.Plate, v.UsedFuelTypeUID
,v.FT_TotalLimit - v.FT_UsedTotalLimit
)

select *, t1.RemainingBefore+t1.DateRangeTotal-t1.DateRangeTransactionsTotal as RemainingAfter from t1 ;

你们这里有什么样的指数?在外键列上有索引总是有帮助的,如果可能的话,在
WHERE
ORDER BY
子句中使用的列上也有索引
[Transactions]
(
    [TransactionID] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
    [SaleDate] [datetime] NULL,
    [Liter] [float] NULL,
    [VehicleChildID] [int] NULL
...   
)

[VehiclesChild] 
(
    [VehicleID] [int] IDENTITY(1,1) NOT NULL,
    [CustomerChildID] [int] NULL,
    [Plate] [varchar](16) NULL,
    [UsedFuelTypeUID] [int] NULL,
    [FT_TotalLimit] [float] NULL,
    [FT_UsedTotalLimit] [float] NULL
    ...
)

[VehicleChildLog]
(
    [VehiclesChildLogId] [int] IDENTITY(1,1) NOT NULL,
    [VehicleChildId] [int] NOT NULL,
    [CustomerChildId] [int] NOT NULL,
    [FT_TotalLimit] [float] NULL,
    [FT_UsedTotalLimit] [float] NULL,
    [UpdateDate] [datetime] NULL
    ...
)