收缩数据库Shrink a Database

03/14/2017

本文内容

适用于:Applies to: SQL ServerSQL Server(所有支持的版本)SQL ServerSQL Server (all supported versions) Azure SQL 数据库Azure SQL DatabaseAzure SQL 数据库Azure SQL Database适用于:Applies to: SQL ServerSQL Server(所有支持的版本)SQL ServerSQL Server (all supported versions) Azure SQL 数据库Azure SQL DatabaseAzure SQL 数据库Azure SQL Database

本主题说明如何使用 SQL Server 2019 (15.x)SQL Server 2019 (15.x) 或 SQL Server Management StudioSQL Server Management Studio 在 Transact-SQLTransact-SQL中使用对象收缩数据库。This topic describes how to shrink a database by using Object in SQL Server 2019 (15.x)SQL Server 2019 (15.x) by using SQL Server Management StudioSQL Server Management Studio or Transact-SQLTransact-SQL.

收缩数据文件通过将数据页从文件末尾移动到更靠近文件开头的未占用的空间来恢复空间。Shrinking data files recovers space by moving pages of data from the end of the file to unoccupied space closer to the front of the file. 在文件末尾创建足够的可用空间后,可以取消对文件末尾的数据页的分配并将它们返回给文件系统。When enough free space is created at the end of the file, data pages at end of the file can be deallocated and returned to the file system.

本主题内容In This Topic

开始之前Before You Begin

限制和局限Limitations and Restrictions

收缩后的数据库不能小于数据库的最小大小。The database cannot be made smaller than the minimum size of the database. 最小大小是在数据库最初创建时指定的大小,或是上一次使用文件大小更改操作(如 DBCC SHRINKFILE)设置的显式大小。The minimum size is the size specified when the database was originally created, or the last explicit size set by using a file-size-changing operation, such as DBCC SHRINKFILE. 例如,如果数据库最初创建时的大小为 10 MB,后来增长到 100 MB,则该数据库最小只能收缩到 10 MB,即使已经删除数据库的所有数据也是如此。For example, if a database was originally created with a size of 10 MB and grew to 100 MB, the smallest size the database could be reduced to is 10 MB, even if all the data in the database has been deleted.

不能在备份数据库时收缩数据库。You cannot shrink a database while the database is being backed up. 反之,也不能在数据库执行收缩操作时备份数据库。Conversely, you cannot backup a database while a shrink operation on the database is in process.

建议Recommendations

若要查看数据库中当前的可用(未分配)空间量。To view the current amount of free (unallocated) space in the database.

当您计划收缩数据库时,请考虑以下信息:Consider the following information when you plan to shrink a database:

在执行会产生许多未使用空间的操作(如截断表或删除表操作)后,执行收缩操作最有效。A shrink operation is most effective after an operation that creates lots of unused space, such as a truncate table or a drop table operation.

大多数数据库都需要一些可用空间,以供常规日常操作使用。Most databases require some free space to be available for regular day-to-day operations. 如果反复收缩数据库并注意到数据库大小变大,则表明收缩的空间是常规操作所必需的。If you shrink a database repeatedly and notice that the database size grows again, this indicates that the space that was shrunk is required for regular operations. 在这种情况下,反复收缩数据库是一种无谓的操作。In these cases, repeatedly shrinking the database is a wasted operation.

收缩操作不会保留数据库中索引的碎片状态,通常还会在一定程度上增加碎片。A shrink operation does not preserve the fragmentation state of indexes in the database, and generally increases fragmentation to a degree. 这是不要反复收缩数据库的另一个原因。This is another reason not to repeatedly shrink the database.

除非有特定要求,否则不要将 AUTO_SHRINK 数据库选项设置为 ON。Unless you have a specific requirement, do not set the AUTO_SHRINK database option to ON.

SecuritySecurity

权限Permissions

要求具有 sysadmin 固定服务器角色或 db_owner 固定数据库角色的成员身份。Requires membership in the sysadmin fixed server role or the db_owner fixed database role.

使用 SQL Server Management StudioUsing SQL Server Management Studio

收缩数据库To shrink a database

在 对象资源管理器 中,连接到 SQL Server 数据库引擎SQL Server Database Engine的实例,然后展开该实例。In Object Explorer, connect to an instance of the SQL Server 数据库引擎SQL Server Database Engine, and then expand that instance.

展开“数据库”,再右键单击要收缩的数据库。Expand Databases, and then right-click the database that you want to shrink.

指向 “任务” ,指向 “收缩” ,然后单击 “数据库” 。Point to Tasks, point to Shrink, and then click Database.

DatabaseDatabase

显示所选数据库的名称。Displays the name of the selected database.

当前分配的空间Current allocated space

显示所选数据库的总已用空间和未使用空间。Displays the total used and unused space for the selected database.

可用空间Available free space

显示所选数据库的日志和数据文件中可用空间的总和。Displays the sum of free space in the log and data files of the selected database.

在释放未使用的空间前重新组织文件Reorganize files before releasing unused space

选择此选项等效于执行指定了目标百分比选项的 DBCC SHRINKDATABASE。Selecting this option is equivalent to executing DBCC SHRINKDATABASE specifying a target percent option. 清除此选项等效于执行带 TRUNCATEONLY 选项的 DBCC SHRINKDATABASE。Clearing this option is equivalent to executing DBCC SHRINKDATABASE with TRUNCATEONLY option. 在打开对话框时,默认情况下不选择此选项。By default, this option is not selected when the dialog is opened. 如果选择此选项,用户必须指定目标百分比选项。If this option is selected, the user must specify a target percent option.

收缩后文件中的最大可用空间Maximum free space in files after shrinking

输入在数据库收缩后数据库文件中剩余可用空间的最大百分比。Enter the maximum percentage of free space to be left in the database files after the database has been shrunk. 值可以介于 0 和 99 之间。Permissible values are between 0 and 99.

单击“确定”。Click OK.

使用 Transact-SQLUsing Transact-SQL

收缩数据库To shrink a database

连接到 数据库引擎Database Engine。Connect to the 数据库引擎Database Engine.

在标准菜单栏上,单击 “新建查询” 。From the Standard bar, click New Query.

将以下示例复制并粘贴到查询窗口中,然后单击“执行” 。Copy and paste the following example into the query window and click Execute. 此实例使用 DBCC SHRINKDATABASE 减少 UserDB 数据库中数据文件和日志文件的大小并允许数据库中有 10 % 的可用空间。This example uses DBCC SHRINKDATABASE to decreases the size of the data and log files in the UserDB database and to allow for 10 percent free space in the database.

DBCC SHRINKDATABASE (UserDB, 10);

GO

跟进:在收缩数据库之后Follow Up: After you shrink a database

被移动用来收缩文件的数据可以分布到文件的任何可用位置。Data that is moved to shrink a file can be scattered to any available location in the file. 这将导致索引碎片并使搜索索引范围的查询变慢。This causes index fragmentation and can slow the performance of queries that search a range of the index. 若要消除碎片,请考虑在收缩后重新生成文件的索引。To eliminate the fragmentation, consider rebuilding the indexes on the file after shrinking.

另请参阅See Also

mysql表收缩时从库也收缩么_收缩数据库 - SQL Server | Microsoft Docs相关推荐

  1. MySQL如何制作报表_(MySQLToSQL) 生成报表 - SQL Server | Microsoft Docs

    生成报告 (MySQLToSQL)Generating Reports (MySQLToSQL) 01/19/2017 本文内容 使用命令执行的某些活动的报告在对象树级别的 SSMA 控制台中生成.T ...

  2. ssma迁移助手_如何使用SQL Server迁移助手(SSMA)和SSIS将MySQL表迁移到SQL Server

    ssma迁移助手 介绍 (Introduction) In this article, we will demonstrate how to migrate MySQL tables to SQL S ...

  3. mysql union all sum_[数据库]SQL Server UNION ALL 结果 SUM函数造成精度丢失

    [数据库]SQL Server UNION ALL 结果 SUM函数造成精度丢失 0 2020-08-03 18:00:30 SQL Server 数据类型 使用场景: 在进行多维度汇总数据时,需要将 ...

  4. 数据库SQL Server 如何将数据库表名等前缀转换成dbo

    数据库SQL Server 如何将数据库表名前缀,框架名前缀,视图前缀,存储过程前缀转换成dbo exec sp_msforeachtable 'sp_changeobjectowner ''?'', ...

  5. 数据库Sql server 表分区

    首先明确表分区和分库分表的概念. 表分区:在不破坏原本表结构的基础上,对单个大表进行切割,分成不同文件,存储在不用文件或硬盘. 分库分表:这里涉及到垂直切分及水平切分(引用网上的文章:https:// ...

  6. mysql查阅建立的库_mysql 怎么查看创建的数据库和表

    展开全部 1.e68a8462616964757a686964616f31333339666635 //看当前使用的是哪个数据库 ,如果你还没选择任何数据库,结果是NULL. mysql>sel ...

  7. mysql表文件与结构_MySQL文件结构、逻辑架构及sql执行流程分析

    1.MySQL文件说明 1.1 MySQL文件夹文件 linux服务器上MySQL安装好之后都有如下文件: auto.cnf:每一个MySQL实例都有一个唯一ID 蓝色文件夹:表示数据库,每个数据库对 ...

  8. mysql表多大的时候需要分区_数据库分区表 什么情况下需要分区

    什么数据库需要进行分区?首先看一下我们的案例:2010年6月我们六期IT开发团队接到一个XX全国连锁店的餐饮系统,经过一周的敏捷开发之后,XX餐饮系统正式上线了,由于该软件的功能强大,操作简单,功能灵 ...

  9. 数据库管理工具神器-DataGrip,可同时管理多个主流数据库[SQL Server,MySQL,Oracle等]连接

    日常开发中少不了各种可视化数据库管理工具.如果需要同时能连接多种数据库,大家肯定都会想到 DBeaver.Navicat Premium.本文介绍另一个十分好用且强大的工具:DataGrip. Dat ...

最新文章

  1. namenode如何存储复本?
  2. 算力超越 iPhone,芯片堪比Mac,网友:“买来能干啥?”
  3. 原生JS和jQuery操作DOM的区别小结
  4. leetcode刷题笔记342 4的幂
  5. mac 没有所有开发者_为什么开发人员应该像产品所有者那样思考
  6. 【每日SQL打卡】​​​​​​​​​​​​​​​DAY 18丨即时食物配送 II【难度中等】​
  7. 新版vue-cli搭建多页面应用
  8. 响应式网页设计简单入门
  9. shell 脚本中 while 只执行一次
  10. 测试工作——XPath
  11. file does not exist 阿里云OSS图片上传遇到的问题
  12. 在Linux 中如何从进程相关的文件描述中恢复数据
  13. 在应用程序级别之外使用注册为 allowDefinition='MachineToApplication'
  14. 【折腾】GitHub Actions 反代 RSSHub + 多实例轮询
  15. 【手把手带你刷LeetCode】——02.出现1次和K次的数(位运算)
  16. nodejs之utility,crypto使用
  17. spyder python使用技巧大全_spyder常用功能
  18. Release That Record Lock!
  19. iPhone媒体服务器修改,出现Apple媒体服务条款与条件已更改是怎么回事如何解决...
  20. 积木Sketch插件进阶开发指南

热门文章

  1. ASP.net 2.0 自定义控件的开发之数据分页 第二章
  2. h3c交换机限制端口访问_H3C交换机端口限速和流量监管典型配置指导
  3. mac svn 可视化界面_svn for mac 操作
  4. 意外终止_美国留学本科意外终止怎么办?
  5. java取geosever数据_Geoserver - 连接到SQL Server 2008 Express并获取数据
  6. qt5中字符串转字符串数组_Qt中的对象类型转换(Qstring 转换char*有三种方法)
  7. python语言包含的错误,Python语言程序中包含的错误,一般分为三种,以下____________不是其中的一种...
  8. 公共端接正极还是负极_【动力电池大事记】现代摩比斯启动首批模组工厂,松下拟挪威建厂,200名车主起诉现代,密歇根大学开发锂金属固态电池及“无负极”工艺...
  9. Java设计模式(访问者模式-迭代器模式-观察者模式-中介者模式)
  10. 他花了一个月,使用MicroPython将自己装进OLED里面