sql-server-2008 - 缩小数据库以释放物理空间有多安全 - Sql Server

标签 sql-server-2008

在寻找解决磁盘空间问题的过程中,我浏览了几篇文章,发现收缩数据库是一种选择,而另一些则是删除错误日志文件和事务日志文件。但是,

I found the first option controversial where in order to free up the unused space SQL uses an ugly process and results in Index fragmentation that affects performance in the long run. Meaning that after deallocating the space we are giving authority to the Operating system do what it needs to with it.

那么,从可用选项列表中删除收缩选项是否足够公平?

最佳答案

确实不建议收缩数据库。您可以这样理解它,当您收缩数据库时,它会导致碎片增加,现在为了减少您尝试重建索引的碎片,这最终会导致数据库大小增加。

您可以自己运行查询来测试

SELECT name, (size*8) Size_KB
FROM sys.database_files
GO
-- Check Fragmentations in the database
SELECT avg_fragmentation_in_percent, fragment_count
FROM sys.dm_db_index_physical_stats (DB_ID(), OBJECT_ID('SecondTable'), NULL, NULL, 'LIMITED')
GO
-- Shrink the Database
DBCC SHRINKDATABASE (ShrinkIsBed);
GO
-- Name of the Database and Size
SELECT name, (size*8) Size_KB
FROM sys.database_files
GO
-- Check Fragmentations in the database
SELECT avg_fragmentation_in_percent, fragment_count
FROM sys.dm_db_index_physical_stats (DB_ID(), OBJECT_ID('SecondTable'), NULL, NULL, 'LIMITED')
GO
-- Rebuild Index on FirstTable
ALTER INDEX IX_SecondTable_ID ON SecondTable REORGANIZE
GO
-- Name of the Database and Size
SELECT name, (size*8) Size_KB
FROM sys.database_files
GO
-- Check Fragmentations in the database
SELECT avg_fragmentation_in_percent, fragment_count
FROM sys.dm_db_index_physical_stats (DB_ID(), OBJECT_ID('SecondTable'), NULL, NULL, 'LIMITED')
GO

来自 here 的查询源

可以查看Don’t Touch that Shrink Database Button!

What Happens when you Shrink a Database?

When you click that shrink database button (or leave a DB in autoshrink, or schedule a job to perform shrinks), you are asking SQL Server to remove the unused space from your database’s files.The process SQL uses is ugly and results in Index fragmentation that affects performance in the long run. You’ve deallocated that space and are letting the O/S do what it needs to with it. If you have a growing database (as the majority of production databases tend to be), this means that that database will grow again. Depending on your autogrowth settings (another pet peeve for another post) 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 by your I/O subsystem. At worse this is causing that index fragmentation I mentioned, file fragmentation, interrupting what would have otherwise been contiguous files and potentially causing I/O related performance problems. Really though, you are wasting time and introducing index fragmentation.

关于sql-server-2008 - 缩小数据库以释放物理空间有多安全 - Sql Server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30546429/

相关文章:

c# - 将 varchar 值 'null' 转换为数据类型 int 时转换失败

c# - SqlCommand SQL 语句被执行两次

sql - 在子查询中丢失我的表......有什么问题?

c# - 使用C#登录网站连接现有的sql server db

visual-studio-2010 - 如何查找 SQL Server Management Studio 的服务器名称

sql-server - 将 SQL Server 2008 db 复制到 SQL Server 2008 Express 的工具?

sql - 拆分 SQL 结果

mysql - 为没有在 SQL Server 中指定默认值的非列生成默认值的脚本

sql - 邮政编码使用 10 位字符的约束检查

c# - Entity Framework 4 : Bad performance with SQL Server 2008