RPM*_*984 21 performance sql-server sql-server-2012 azure-vm query-performance
我有一个存储过程,它通过覆盖索引从索引视图返回结果。通常,它运行得很快(~10 毫秒),有时它可以运行长达 8 秒。
这是一个随机执行示例(注意:这不是一个缓慢的执行,但是除了传递的值之外,查询文本是相同的):
declare @p2 dbo.IdentityType
insert into @p2 values(5710955)
insert into @p2 values(5710896)
insert into @p2 values(5710678)
insert into @p2 values(5710871)
insert into @p2 values(5711103)
insert into @p2 values(6215197)
insert into @p2 values(5710780)
exec ListingSearch_ByLocationAndStatus @statusType=1,@locationIds=@p2
Run Code Online (Sandbox Code Playgroud)
这是 SPROC:
ALTER PROCEDURE [dbo].[ListingSearch_ByLocationAndStatus]
@LocationIds IdentityType READONLY,
@StatusType TINYINT
AS
BEGIN
SET NOCOUNT ON;
SELECT -- lots of fields
FROM [dbo].[ListingSearchView][a] WITH (NOEXPAND)
INNER JOIN @LocationIds [b] ON [a].[LocationId] = [b].[Id]
WHERE [a].[StatusType] = @statusType
OPTION (RECOMPILE);
Run Code Online (Sandbox Code Playgroud)
(注意:我OPTION (RECOMPILE)
最近根据一些建议添加了提示,但没有帮助。
这是覆盖索引(注意:视图上还有一个聚集索引ListingId
,它是唯一的)
CREATE NONCLUSTERED INDEX [IX_ListingSearchView_ForAPI] ON [dbo].[ListingSearchView]
(
[LocationId] ASC,
[StatusType] ASC
)
INCLUDE ( -- all the fields in the query) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
GO
Run Code Online (Sandbox Code Playgroud)
我使用了 showplan XML 统计信息进行了分析器跟踪。
看起来完全符合我的预期,并且在查询快速时是相同的计划。
这是视图/支持表的完整模式,如果有帮助的话:https : //pastebin.com/wh1sRcbQ
笔记:
WITH OPTION (RECOMPILE);
提示(不起作用,所以不能进行参数嗅探?)关于我接下来可以尝试什么的任何想法?
谢谢
Art*_*yan -1
如果您认为问题出在阻塞上,我建议您使用乐观事务隔离级别“读取已提交快照”(请记住,这会增加您的 tempDB 的开销)。
参考: https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/sql/snapshot-isolation-in-sql-server
如果问题不在于读/写阻塞,您可以尝试在视图上添加索引(索引的最佳选择取决于数据的选择性)
CREATE NONCLUSTERED INDEX IX_ListingSearchView (LocationID, StatusType) INCLUDE (other columns...)
Run Code Online (Sandbox Code Playgroud)