SQL Server - 存储过程突然变慢
我昨天编写了一个存储过程,通常在一秒钟内完成。今天,大约需要 18 秒。我昨天也遇到了这个问题,似乎可以通过删除并重新创建存储过程来解决。如今,这个伎俩似乎不起作用了。 :(
有趣的是,如果我复制存储过程的主体并将其作为简单查询执行,它会很快完成。事实似乎是它是一个存储过程,导致速度减慢......!
有谁知道问题可能是什么我已经搜索过答案,但他们通常建议通过查询分析器运行它,但我没有 - 我现在使用 SQL Server 2008 Express
如下;
ALTER PROCEDURE [dbo].[spGetPOIs] @lat1 float, @lon1 float, @lat2 float, @lon2 float, @minLOD tinyint, @maxLOD tinyint, @exact bit AS BEGIN -- Create the query rectangle as a polygon DECLARE @bounds geography; SET @bounds = dbo.fnGetRectangleGeographyFromLatLons(@lat1, @lon1, @lat2, @lon2); -- Perform the selection if (@exact = 0) BEGIN SELECT [ID], [Name], [Type], [Data], [MinLOD], [MaxLOD], [Location].[Lat] AS [Latitude], [Location].[Long] AS [Longitude], [SourceID] FROM [POIs] WHERE NOT ((@maxLOD [MaxLOD])) AND (@bounds.Filter([Location]) = 1) END ELSE BEGIN SELECT [ID], [Name], [Type], [Data], [MinLOD], [MaxLOD], [Location].[Lat] AS [Latitude], [Location].[Long] AS [Longitude], [SourceID] FROM [POIs] WHERE NOT ((@maxLOD [MaxLOD])) AND (@bounds.STIntersects([Location]) = 1) END END
存储过程 表具有 MinLOD、MaxLOD 索引和 Location 空间索引。
I have written a stored procedure that, yesterday, typically completed in under a second. Today, it takes about 18 seconds. I ran into the problem yesterday as well, and it seemed to be solved by DROPing and re-CREATEing the stored procedure. Today, that trick doesn't appear to be working. :(
Interestingly, if I copy the body of the stored procedure and execute it as a straightforward query it completes quickly. It seems to be the fact that it's a stored procedure that's slowing it down...!
Does anyone know what the problem might be? I've searched for answers, but often they recommend running it through Query Analyser, but I don't have have it - I'm using SQL Server 2008 Express for now.
The stored procedure is as follows;
ALTER PROCEDURE [dbo].[spGetPOIs] @lat1 float, @lon1 float, @lat2 float, @lon2 float, @minLOD tinyint, @maxLOD tinyint, @exact bit AS BEGIN -- Create the query rectangle as a polygon DECLARE @bounds geography; SET @bounds = dbo.fnGetRectangleGeographyFromLatLons(@lat1, @lon1, @lat2, @lon2); -- Perform the selection if (@exact = 0) BEGIN SELECT [ID], [Name], [Type], [Data], [MinLOD], [MaxLOD], [Location].[Lat] AS [Latitude], [Location].[Long] AS [Longitude], [SourceID] FROM [POIs] WHERE NOT ((@maxLOD [MaxLOD])) AND (@bounds.Filter([Location]) = 1) END ELSE BEGIN SELECT [ID], [Name], [Type], [Data], [MinLOD], [MaxLOD], [Location].[Lat] AS [Latitude], [Location].[Long] AS [Longitude], [SourceID] FROM [POIs] WHERE NOT ((@maxLOD [MaxLOD])) AND (@bounds.STIntersects([Location]) = 1) END END
The 'POI' table has an index on MinLOD, MaxLOD, and a spatial index on Location.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
啊,难道查询计划很糟糕吗?
SP 的编译/查询计划在首次使用时确定 - 取决于参数。因此,第一次调用的参数(当不存在计划时)确定查询计划。有一次我从缓存中删除,生成了新计划。
下次运行缓慢时,可能会使用查询分析器进行调用并获取选定的计划 - 并检查它的外观。
如果是这样 - 添加一个选项,在每次调用时重新编译 SP(使用重新编译)。
Ah, can it be the query plan sucks?
SP's get compiled / query plan determined on FIRST USE - depending on parameters. So, the parameters of the first call (when no plan is present) determine the query plan. At one point I gets dropped from cache, new plan generated.
Next time it runs slow, possibly make a call using query analyzer and get the selected plan - and check how it looks.
if it is this - put in an option to recompile the SP on every call (with recompile).
parameter sniffing 谷歌一下。 try this, which will "remap" the input parameters to local variables to prevent SQL Server from trying to guess the query plan based on parameters:
parameter sniffing google it. try this, which will "remap" the input parameters to local variables to prevent SQL Server from trying to guess the query plan based on parameters:
我有一个类似的问题,它与索引有关。
重建它们可以帮助 SP 再次快速运行。
我在此处找到了解决方案
I had a similar problem and it was related with indexes.
Rebuilding them help the SP to run fast again.
I found the solution here