[英]Slow running stored procedure in SQL Server 2008 R2
So it's rare that I get completely confused but wanted to see what others thought about what I'm experiencing. 因此,很少有我完全感到困惑,而是想看看别人对我所经历的事情的看法。
I've been asked to look at a slow running stored procedure. 我被要求查看运行缓慢的存储过程。 Currently takes around 8-12 minutes to run.
目前大约需要8到12分钟才能运行。
So I noticed a useful index was missing and added it - now takes 8 seconds. 因此,我注意到一个有用的索引丢失了并添加了它-现在需要8秒钟。 But if I restart SQL Server and re-run the stored proc it's taking 8-12 minutes again.
但是,如果我重新启动SQL Server并重新运行存储的proc,它将再次花费8-12分钟。
If I then stop the query and delete the new index then re-run the stored proc it takes seconds again. 如果然后停止查询并删除新索引,然后重新运行存储的proc,则需要几秒钟的时间。 Bizarre.
离奇。
Has anyone ever experienced anything like this? 有没有人经历过这样的事情? The stored procedure is calling a View if that makes any difference.
如果这有任何区别,则存储过程将调用View。
Q. if I restart SQL Server and re-run the stored proc it's taking 8-12 minutes again.
问:
if I restart SQL Server and re-run the stored proc it's taking 8-12 minutes again.
When you perform your query, the data is read into memory in blocks. 执行查询时,数据将以块的形式读入内存。 These blocks remain in memory but they get "aged".
这些块保留在内存中,但是会“老化”。 This means the blocks are tagged with the last access and when Sql Server requires another block for a new query and the memory cache is full, the least recently used block (the oldest) is kicked out of memory.
这意味着这些块被标记为具有最后访问权限,并且当Sql Server需要另一个块进行新查询并且内存缓存已满时,最近使用最少的块(最旧的块)就会被踢出内存。 (In most cases - full tables scan blocks are instantly aged to prevent full table scans overrunning memory and choking the server).
(在大多数情况下-全表扫描块会立即老化,以防止全表扫描超过内存并阻塞服务器)。
Q. If I then stop the query and delete the new index then re-run the stored proc it takes seconds again.
问:
If I then stop the query and delete the new index then re-run the stored proc it takes seconds again.
What is happening here is that the data blocks in memory from the first query haven't been kicked out of memory yet so can be used for your second query, meaning disk access is avoided and performance is improved. 这里发生的是,第一个查询中的内存中的数据块尚未被踢出内存,因此可以用于第二个查询,这意味着可以避免磁盘访问并提高了性能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.