简体   繁体   English

SQL Server 2008 R2中运行缓慢的存储过程

[英]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.

相关问题 如何在SQL Server 2008 R2存储过程中获取记录集? - How to get the recordset in SQL Server 2008 r2 stored procedure? 带文字的存储过程参数在SQL Server 2008 R2中不起作用 - Stored procedure parameters with literals not working in SQL Server 2008 R2 如何在SQL Server 2008 R2中加快存储过程 - How to speed up stored procedure in SQL Server 2008 R2 计划Microsoft SQL Server 2008 R2中的存储过程 - Schedule Stored Procedure in Microsoft SQL Server 2008 R2 SQL Server 2008 R2中具有多个选择和插入操作的存储过程的性能问题 - Performance issue of stored procedure with several select and insert operations in SQL Server 2008 R2 如何获取SQL Server 2008 R2中从python调用的存储过程返回的值 - How to get the value returned by a stored procedure called from python in SQL server 2008 R2 带FORMSOF和同义词库的存储过程参数(SQL Server 2008 R2 SP2) - Stored Procedure Parameter with FORMSOF and Thesaurus (SQL Server 2008 R2 SP2) SQL Server 2008 R2:从存储过程返回结果并插入临时表 - SQL Server 2008 R2: Return result from stored procedure and insert into temp table 在SQL Server 2008 R2中存储过程中存储多个数据集? - Store multiple data sets from stored procedure in SQL Server 2008 R2? SQL Server 2008 R2:具有一个或多个表名称的存储过程作为参数 - SQL Server 2008 R2: stored procedure with one or more table names a parameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM