SQL / Windows OS 释放未使用的 space

SQL / Windows OS releasing unused space

我最近对我的数据做了一些归档,并执行了以下操作:

我有一个数据库 table,其中有超过 3300 万条记录,其中许多是重复的。

我备份了 table 并将唯一数据插入到新的 table,然后 renamed/swapped table 名称,这实现了我需要的。

现在我只剩下两个 tables...

执行此操作后,我的 SQL mdf/data 文件已增加到 319.7 GB,我的日志文件已增加到 182 GB。

这占用了我的大部分空闲时间 OS space,我的 D 盘现在 space。

我的问题是,一旦我对存档数据感到满意,我将删除 _backup table 留下我的美好生活 table。

但据我所知,SQL 不会将任何免费的 space 还给我 OS,从log/mdf 文件,我读过很多关于缩小 db/log 的内容,但很多人说这是不好的做法,任何建议都很好...

TL;DR; 不要收缩你的数据库。曾经。

But what if you really do need to shrink it?

根据 SQL 服务器专家 Brant Ozar 所链接的文章 - 在某些情况下缩小数据库是一个合理的选择:

  • Your database is 1TB or larger
  • You’ve deleted 50% of the data
  • You have 500GB+ empty space
  • You’re never going to need that space because you’re now doing regular deletions and archiving

完整答案:

你写道你一直在读这个 - 所以我希望你遇到过 post 像 Brent Ozar's What’s So Bad About Shrinking Databases with DBCC SHRINKDATABASE?:

You have high fragmentation, so you rebuild your indexes.

Which leaves a lot of empty space around, so you shrink your database.

Which causes high fragmentation, so you rebuild your indexes, which grows the databases right back out and leaves empty space again, and the cycle keeps perpetuating itself.

Mike Walsh's Don’t Touch that Shrink Database Button In SQL Server! - 他解释相同的地方:

What Happens when you Shrink a Database?

When you shrink a database, you are asking SQL Server to remove the unused space from your database’s files.The process SQL uses can be ugly and result in Index fragmentation. This fragmentation affects performance in the long run. You’ve freed that space and are letting the O/S do what it needs to with it, so you got what you asked for at least. If you have a growing database, this means that database will grow again. Depending on your autogrowth settings, this growth will probably be more than necessary and you will end up shrinking again. At best this is just extra work (shrink grow/shrink grow) and the resulting file fragmentation is handled alright. At worse this is causing index fragmentation, file fragmentation, and potentially causing performance problems during the shrink.

Aaron Bertrand's answer to SHRINKFILE best practices and experience 在 dba.StackExchange.com - 他基本上是说你可以自由地忽略聪明、有经验的人的好建议,并假设你的情况不同 - 但风险自负.这是他的结案陈词:

It will be a much more expensive operation to shrink the file to 4GB, then force it to grow to accommodate the new data. This is like washing an already clean towel that you're about to use to wipe up a mess..

结论 - 你真的,真的应该注意专家写的东西。明确一点:我不认为自己是该主题的专家。
我对开发人员方面的 T-SQL 有深入的了解,但我在 DBA 方面的经验很少——我一方面可以指望我必须编写维护计划、数据库迁移或处理任何问题的次数DBA 会做的系统管理工作。
但是,我提到的所有这些人都是领先的 DBA:Brent Ozar 是 MCM(Microsoft 认证大师),Mike Walsh 是 9 次 MVP(自 2011 年以来),Aaron Bertrand 是 22 次 MVP(自 1997 年以来)- 这些伙计们真的知道他们在写什么。
我会在一周中的任何一天和周日两次接受他们中任何一个的免费建议。

更新 - 关于日志文件:

缩小日志文件有点不同 - 定期这样做是不好的做法。
日志文件大小基本上取决于您的备份策略和选择的恢复模型。

推荐阅读:Mike Walsh 的 self answered post over on dba.stackexchange - 如果您愿意,我建议您阅读他的完整答案以及 Aaron Bertrand 对同一 post 的完整答案。