sql优化的问题
CREATE PROCEDURE InsertFligthCache
(
@AirCode varchar(10),
@AirCompanyName varchar(50),
@LowPrice decimal(9),
@LowDisCount int,
@FlyDate datetime,
@Bunk varchar(10),
@UpDateTime datetime,
@Source varchar(50),
@DistributorID varchar(8),
@DistributorType tinyint,
@CityGroup varchar(50),
@StartCityCode varchar(10),
@EndCityCode varchar(10)
)
AS
SET NOCOUNT ON
BEGIN TRAN
DECLARE @ID int
--有记录
IF (EXISTS(SELECT T_ID FROM BA_AV_Special WHERE AirCode=@AirCode AND StartCityCode=@StartCityCode AND EndCityCode=@EndCityCode AND FlyDate=@FlyDate))
BEGIN
SELECT @ID=T_ID FROM BA_AV_Special WHERE AirCode=@AirCode AND StartCityCode=@StartCityCode AND EndCityCode=@EndCityCode AND FlyDate=@FlyDate
UPDATE BA_AV_Special SET UpDateTime=GETDATE() WHERE T_ID=@ID
END
--无记录
ELSE
BEGIN
INSERT INTO BA_AV_Special
(AirCode,AirCompanyName,LowPrice,LowDisCount,FlyDate,Bunk,UpDateTime,Source,DistributorID,DistributorType,CityGroup,StartCityCode,EndCityCode)
VALUES
(@AirCode,@AirCompanyName,@LowPrice,@LowDisCount,@FlyDate,@Bunk,@UpDateTime,@Source,@DistributorID,@DistributorType,@CityGroup,@StartCityCode,@EndCityCode)
END
IF @@Error <> 0
BEGIN
ROLLBACK TRAN;
END
ELSE
BEGIN
COMMIT TRAN;
END
SET NOCOUNT OFF
RETURN
问大家个问题,以上是我的存储过程
但是在执行的时候
我用查询计划根了一下
reads:283
duration:16
逻辑读很高啊,我这个sql语句没有很复杂啊,还能怎么优化一下啊
暂时还没有索引
------解决方案--------------------现在多少记录,运行一次多少时间
先确定是否有必要优化
------解决方案--------------------IF (EXISTS(SELECT T_ID FROM BA_AV_Special WHERE AirCode=@AirCode AND StartCityCode=@StartCityCode AND EndCityCode=@EndCityCode AND FlyDate=@FlyDate))
BEGIN
SELECT @ID=T_ID FROM BA_AV_Special WHERE AirCode=@AirCode AND StartCityCode=@StartCityCode AND EndCityCode=@EndCityCode AND FlyDate=@FlyDate
UPDATE BA_AV_Special SET UpDateTime=GETDATE() WHERE T_ID=@ID
END
可以更变为以下吗?
IF (EXISTS(SELECT T_ID FROM BA_AV_Special WHERE AirCode=@AirCode AND StartCityCode=@StartCityCode AND EndCityCode=@EndCityCode AND FlyDate=@FlyDate))
BEGIN
UPDATE BA_AV_Special SET UpDateTime=GETDATE() WHERE AirCode=@AirCode AND StartCityCode=@StartCityCode AND EndCityCode=@EndCityCode AND FlyDate=@FlyDate
END