我有一个使用以下光标的存储过程:
DECLARE Delai_latence CURSOR LOCAL FOR
SELECT [ID]
,[ID_LIVRAISON]
,[DH_ETAPE_DEB]
,[DH_ETAPE_FIN]
,[ORDRE_ETAPE]
FROM [ODS_PFNUM_DELAI]
WHERE ID_GRP = 1
AND STATUT_ETAPE is not Null
AND DH_ETAPE_FIN is not Null
ORDER BY [ID_LIVRAISON], [ORDRE_ETAPE]
为了优化它,我创建了非聚集索引:
CREATE NONCLUSTERED INDEX [IX_LATENCE_PFNUM_DELAI] ON [ODS_PFNUM_DELAI]
(
[ID_GRP] ASC,
[STATUT_ETAPE] ASC,
[DH_ETAPE_FIN] ASC
)
INCLUDE ( [ID],
[ID_LIVRAISON],
[DH_ETAPE_DEB],
[ORDRE_ETAPE]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
然而,执行仍然花费太多时间(你会发现执行计划已附上)。
感谢。
答案 0 :(得分:0)
真正的问题可能是:你真的需要一个光标,特别是考虑到“胖子”这个问题。查询计划中显示的行。
无论如何,出于好奇,查询会从这个(过滤的)索引中受益吗?
CREATE INDEX idx_test ON [ODS_PFNUM_DELAI]
([ID_GRP], [ID_LIVRAISON], [ORDRE_ETAPE])
INCLUDE ([ID], [DH_ETAPE_DEB], [DH_ETAPE_FIN])
WHERE [STATUT_ETAPE] IS NOT NULL AND [DH_ETAPE_FIN] IS NOT NULL
此外,没有关于存储过程的其余部分的线索:如果您碰巧正在更新游标循环内的[ODS_PFNUM_DELAI]
表,并且更改不会影响实际的循环,将光标定义为STATIC
。