简体   繁体   English

SQL Server MDF文件增长过多

[英]SQL Server MDF file growing too much

I am working with SQL Server and I've noticed my .mdf file is growing to much. 我正在使用SQL Server,但我已经注意到我的.mdf文件越来越多。

I mean, in my logs I changed te recovery model to simple so I've fixed it and the logs are not growing as much as they were. 我的意思是,在我的日志中,我将恢复模式更改为简单,因此我已对其进行修复,并且日志的增长速度没有达到原来的水平。 But now I'd like to shrink my .mdf file. 但现在我想缩小我的.mdf文件。 Doing a shrink, using management studio, to shrink this file will result in data loss? 使用Management Studio进行收缩来收缩该文件会导致数据丢失吗?

How can I prevent this file from growing so much? 如何防止此文件增长太多?

The data file grows because you have put data there. 数据文件会增长,因为您已将数据放在此处。 If you put data there temporarily (or ran a lot of updates or deletes) you can reclaim free space within the file by rebuilding and reorganizing indexes. 如果将数据临时放置在其中(或进行了大量更新或删除),则可以通过重建和重新组织索引来回收文件中的可用空间。 You can determine the biggest indexes in terms of rows using: 您可以使用以下方法确定最大的索引:

SELECT TOP (10) OBJECT_SCHEMA_NAME(object_id),
  OBJECT_NAME(object_id), rows = SUM(rows)
FROM sys.partitions
WHERE index_id IN (0,1)
AND OBJECTPROPERTY(object_id, 'IsMsShipped') = 0
GROUP BY object_id
ORDER BY rows DESC;

And in terms of size using: 并在尺寸方面使用:

SELECT TOP (10) 
  OBJECT_SCHEMA_NAME(i.object_id),
  OBJECT_NAME(i.object_id),
  i.name, 
  numpages = COUNT(p.allocated_page_page_id) 
FROM sys.indexes AS i
CROSS APPLY sys.dm_db_database_page_allocations
  (DB_ID(), i.object_id, i.index_id, NULL, 'LIMITED') AS p
GROUP BY i.object_id, i.name
ORDER BY numpages DESC;

It is rarely the case that you'll want to shrink the database. 很少需要收缩数据库。 The MDF file got big because of your workload. 由于您的工作量,MDF文件变得很大。 If your workload means the data file will get big and then the contents will get smaller again, then making the container smaller only to have it grow again over your next workload cycle accomplishes nothing. 如果您的工作量意味着数据文件变大,然后内容又变小,那么使容器变小只是为了使其在下一个工作量周期内再次增长就什么都没有做。 What will you do with the free space that will only be free temporarily? 您将如何处理只能暂时释放的空闲空间?

If you do decide to shrink your MDF file, please use DBCC SHRINKFILE , not the UI or DBCC SHRINKDATABASE . 如果您决定缩小MDF文件,请使用DBCC SHRINKFILE ,而不要使用UI或DBCC SHRINKDATABASE Expect any shrink operation to increase fragmentation and make query performance worse and, if your workload cycles as I described, expect to have to repeat this process often. 期望任何收缩操作都会增加碎片,并使查询性能变差,并且,如果您的工作周期如我所述,则必须经常重复此过程。 And if you rebuild indexes to remove the fragmentation and improve performance, that operation will grow the file again. 并且,如果您重建索引以消除碎片并提高性能,则该操作将再次使文件增长。 It's like a hole in the bucket, Dear Liza, Dear Liza. 就像桶上的一个洞,亲爱的丽莎,亲爱的丽莎。

OR - just let the file stay large and let your workload reuse the space within it, instead of reclaiming the space temporarily. -只是让文件保持较大并让您的工作量重用其中的空间,而不是临时回收空间。

Unless you stumble across a yet-undiscovered bug, shrinking does not introduce any risk of data loss. 除非您偶然发现尚未发现的错误,否则缩小不会带来任何数据丢失的风险。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM