SQL Server 2000 - 我的查询突然运行缓慢

发布于 2024-07-24 09:31:06 字数 182 浏览 3 评论 0原文

我的数据库查询一直运行得非常快,直到最近变得非常慢。 除了正常的数据增长之外,数据库没有发生任何变化。

我注意到数据库统计信息“从未”更新过。

有没有一种简单的方法可以更新整个数据库中的这些统计信息,以便我可以查看这是否是问题所在?

我使用的是 SQL Server 2000 Sp4。

My database query has been running very fast until it changed to very slow recently. No changed have occurred in the database apart from normal data growth.

I have noticed that the database statistics have "never" been updated.

Is there an easy way that I can update these statistics across my entire database so I can see if that is the problem?

I am using SQL Server 2000 Sp4.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

盗心人 2024-07-31 09:31:06

您可以使用它

CREATE PROC usp_UPDATE_STATISTICS
(@dbName sysname, @sample int)
AS

SET NOCOUNT ON

DECLARE @SQL nvarchar(4000)
DECLARE @ID int
DECLARE @TableName sysname
DECLARE @RowCnt int

CREATE TABLE ##Tables
(
 TableID INT IDENTITY(1, 1) NOT NULL, 
 TableName SYSNAME NOT NULL
)

SET @SQL = ''
SET @SQL = @SQL + 'INSERT INTO ##Tables (TableName) '
SET @SQL = @SQL + 'SELECT [name] '
SET @SQL = @SQL + 'FROM ' + @dbName + '.dbo.sysobjects ' 
SET @SQL = @SQL + 'WHERE xtype = ''U'' AND [name] <> ''dtproperties'''

EXEC sp_executesql @statement = @SQL

SELECT TOP 1 @ID = TableID, @TableName = TableName
FROM ##Tables
ORDER BY TableID

SET @RowCnt = @@ROWCOUNT

WHILE @RowCnt <> 0
BEGIN

 SET @SQL = 'UPDATE STATISTICS ' + @dbname + '.dbo.[' + @TableName + '] WITH SAMPLE ' + CONVERT(varchar(3), @sample) + ' PERCENT'

 EXEC sp_executesql @statement = @SQL

 SELECT TOP 1 @ID = TableID, @TableName = TableName
 FROM ##Tables
 WHERE TableID > @ID
 ORDER BY TableID

 SET @RowCnt = @@ROWCOUNT

END

DROP TABLE ##Tables


GO

,这将更新数据库中所有表的统计信息。 您还应该查看索引并重建/碎片整理作为必要的

Raj

You can use this

CREATE PROC usp_UPDATE_STATISTICS
(@dbName sysname, @sample int)
AS

SET NOCOUNT ON

DECLARE @SQL nvarchar(4000)
DECLARE @ID int
DECLARE @TableName sysname
DECLARE @RowCnt int

CREATE TABLE ##Tables
(
 TableID INT IDENTITY(1, 1) NOT NULL, 
 TableName SYSNAME NOT NULL
)

SET @SQL = ''
SET @SQL = @SQL + 'INSERT INTO ##Tables (TableName) '
SET @SQL = @SQL + 'SELECT [name] '
SET @SQL = @SQL + 'FROM ' + @dbName + '.dbo.sysobjects ' 
SET @SQL = @SQL + 'WHERE xtype = ''U'' AND [name] <> ''dtproperties'''

EXEC sp_executesql @statement = @SQL

SELECT TOP 1 @ID = TableID, @TableName = TableName
FROM ##Tables
ORDER BY TableID

SET @RowCnt = @@ROWCOUNT

WHILE @RowCnt <> 0
BEGIN

 SET @SQL = 'UPDATE STATISTICS ' + @dbname + '.dbo.[' + @TableName + '] WITH SAMPLE ' + CONVERT(varchar(3), @sample) + ' PERCENT'

 EXEC sp_executesql @statement = @SQL

 SELECT TOP 1 @ID = TableID, @TableName = TableName
 FROM ##Tables
 WHERE TableID > @ID
 ORDER BY TableID

 SET @RowCnt = @@ROWCOUNT

END

DROP TABLE ##Tables


GO

This will update stats on all the tables in the DB. You should also look at indexes and rebuild / defrag as nexessary

Raj

梦幻的心爱 2024-07-31 09:31:06

尝试此处

这应该会加快您的索引速度和密钥分发。 重新分析表统计信息可以优化 SQL Server 对查询的索引选择,尤其是对于大型数据集

Try here

This should speed up your indices and key distribution. Re-analyzing table statistics optimises SQL Server's choice of index for queries, especially for large datasets

巨坚强 2024-07-31 09:31:06

一定要为自己制定一个每周自动运行的任务来更新数据库的统计数据。

Definitely make yourself a weekly task that runs automatically to update the database's statistics.

最后的乘客 2024-07-31 09:31:06

正常数据增长足以作为理由,证明几乎任何未优化查询的减速都是合理的。

与数据库大小相关的可扩展性问题在数据量增长之前不会显现出来。

发布您的查询+粗略数据量,我们将帮助您了解情况。

Normal Data Growth is good enough as a reson to justify a slowdown of pretty much any not optimized query.

Scalability issues related db size won't manifest till the data volume grows.

Post your query + rough data volume and we'll help you to see what's what.

冷心人i 2024-07-31 09:31:06

我们在 MSSQL 2005 中也遇到过非常类似的问题,查询运行速度突然变慢。

我们是这样解决这个问题的:我们为查询中的每个 select 语句添加 (nolock)。 例如:

select count(*) from SalesHistory with(nolock)

请注意,nolock 也应该添加到嵌套 select 语句以及连接中。 这里有一篇文章提供了有关使用 nolock 时如何提高性能的更多详细信息。 http://www.mollerus.net/tom/blog/2008/ 03/using_mssqls_nolock_for_faster_queries.html

显然,不要忘记保留原始查询的备份。 请尝试一下并告诉我。

We've had a very similar problem with MSSQL 2005 and suddenly slow running queries.

Here's how we solved it: we added (nolock) for every select statement in the query. For example:

select count(*) from SalesHistory with(nolock)

Note that nolock should also be added to nested select statements, as well as joins. Here's an article that gives more details about how performance is increased when using nolock. http://www.mollerus.net/tom/blog/2008/03/using_mssqls_nolock_for_faster_queries.html

Don't forget to keep a backup of your original query obviously. Please give it a try and let me know.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文