我有一个名为 Transaction_tbl 的表,其中包含超过 400 000 条记录。这是表结构:
CREATE TABLE [dbo].[Transaction_tbl](
[transactID] [numeric](18, 0) IDENTITY(1,1) NOT NULL,
[TBarcode] [varchar](20) NULL,
[cmpid] [int] NULL,
[Locid] [int] NULL,
[PSID] [int] NULL,
[PCID] [int] NULL,
[PCdID] [int] NULL,
[PlateNo] [varchar](20) NULL,
[vtid] [int] NULL,
[Compl] [bit] NULL,
[self] [bit] NULL,
[LstTic] [bit] NULL,
[Gticket] [int] NULL,
[Cticket] [int] NULL,
[Ecode] [varchar](50) NULL,
[dtime] [datetime] NULL,
[LICID] [int] NULL,
[PAICID] [int] NULL,
[Plot] [varchar](50) NULL,
[mkid] [int] NULL,
[mdlid] [int] NULL,
[Colid] [int] NULL,
[Comments] [varchar](100) NULL,
[Kticket] [int] NULL,
[PAmount] [numeric](18, 2) NULL,
[Payid] [int] NULL,
[Paid] [bit] NULL,
[Paydate] [datetime] NULL,
[POICID] [int] NULL,
[DelDate] [datetime] NULL,
[DelEcode] [nvarchar](50) NULL,
[PAICdate] [datetime] NULL,
[KeyRoomDate] [datetime] NULL,
[Status] [int] NULL,
CONSTRAINT [PK_Transaction_tbl] PRIMARY KEY CLUSTERED
(
[transactID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
我在 Locid, dtime 列上有一个非聚集索引。我有一个这样的存储过程:
ALTER procedure [dbo].[IBS_fetchreleasedinpodiumgridnew]
@locid INTEGER = NULL
AS BEGIN
SET NOCOUNT ON
DECLARE @TodayMinus7Days DATETIME
Declare @krrt integer
Declare @DT integer
SET @TodayMinus7Days = getdate()-1
SELECT
t.TBarcode, t.PlateNo, t.DelEcode,cast(t.Paydate as Time) [REQ],
datediff(MINUTE, t.PayDate,
CASE t.Status
WHEN 3 THEN GETDATE()
WHEN 4 THEN t.KeyRoomDate
When 5 THEN t.KeyRoomDate
End) as KRRT,
datediff(MINUTE,t.PayDate,
CASE t.Status
WHEN 3 THEN GETDATE()
WHEN 4 THEN GETDATE()
WHEN 5 THEN t.DelDate
END) as DT
FROM
dbo.Transaction_tbl t
WHERE
(
([status] IN (3,4))
OR
([status] = 5 AND DATEDIFF(n, DelDate, GETDATE()) <= 3)
)
AND locid = 6 AND dtime >= @TodayMinus7Days
ORDER BY
paydate
end
我的执行计划是这样的:
但大多数时候这需要很长时间才能执行..在这种情况下,我怎样才能提高我的存储过程执行性能?
我想使用的任何其他方法..任何帮助都非常有用。谢谢
查询执行计划显示排序需要很长时间..所以如果我给paydate
我的查询性能索引会增加?而不是这个,dtime >= @TodayMinus7Days
我给出了这样的代码:
dtime >= OPTION (optimize for (@TodayMinus7Days))
但出现错误:关键字“OPTION”附近的语法不正确。