In addition to creating backups, there are a variety of tasks and functions SQL Server makes available which can both improve the performance and reliability of your databases. We have previously showed you how to backup SQL Server databases with a simple command line script so in the same fashion we are providing a script which will allow you to easily perform common maintenance tasks.

除了创建备份外,SQL Server还提供了多种任务和功能,它们可以提高数据库的性能和可靠性。 前面我们已经向您展示了如何使用简单的命令行脚本来备份SQL Server数据库,因此,我们以相同的方式提供了一个脚本,使您可以轻松地执行常见的维护任务。

Compacting/Shrinking a Database [/Compact]

压缩/收缩数据库[/ Compact]

There are several factors which contribute to the physical disk space a SQL Server database uses. Just to name a few:

有几个因素会影响SQL Server数据库使用的物理磁盘空间。 仅举几个:

  • Over time as records are added, deleted and updated, SQL is constantly growing and shrinking tables as well as generating temporary data structures to perform query manipulations. In order to accommodate the disk storage needs, SQL Server will increase the size of the database (usually by 10%) as needed so the database file size isn’t constantly changing. While this is ideal for performance, it can cause a disconnect with the storage space used because if, for example, you add a very large number of records which causes the database to grow and subsequently delete these records, SQL Server will not automatically reclaim this disk space.

    随着时间的推移,随着记录的添加,删除和更新,SQL不断增长和缩小表,并生成临时数据结构来执行查询操作。 为了满足磁盘存储需求,SQL Server将根据需要增加数据库的大小(通常增加10%),以便数据库文件的大小不会不断变化。 尽管这对于提高性能是理想的,但是它可能导致所用存储空间的断开连接,因为例如,如果添加大量记录导致数据库增长并随后删除这些记录,SQL Server将不会自动回收该记录。磁盘空间。
  • If you are using Full Recovery Mode on your databases the transactional log file (LDF) can grow quite large, especially on databases with a high volume of updates.

    如果在数据库上使用完全恢复模式,则事务日志文件(LDF)可能会变得很大,尤其是在更新量很大的数据库上。

Compacting (or shrinking) the database will reclaim unused disk space. For small databases (200 MB or less) this usually will not be very much, but for large databases (1 GB or more) the reclaimed space may be significant.

压缩(或收缩)数据库将回收未使用的磁盘空间。 对于小型数据库(200 MB或更小),通常不会很大,但是对于大型数据库(1 GB或更大),回收空间可能很大。

Reindexing a Database [/Reindex]

重新索引数据库[/ Reindex]

Much like constantly creating, editing and deleting files can lead to disk fragmentation, inserting, updating and deleting records in a database can lead to table fragmentation. The practical results are the same in that read and write operations suffer a performance hit. While not a perfect analogy, reindexing the tables in a database essentially defragments them. In some cases, this can significantly increase the speed of data retrieval.

就像不断创建,编辑和删除文件会导致磁盘碎片一样,在数据库中插入,更新和删除记录也会导致表碎片。 实际结果是相同的,因为读取和写入操作受到性能的影响。 虽然不是一个完美的类比,但在数据库中对表重新编制索引实际上会对它们进行碎片整理。 在某些情况下,这可以显着提高数据检索的速度。

Due to the way SQL Server works, tables must be reindexed individually. For databases with a large number of tables this can be real pain to do manually, but our script hits every table in the respective database and rebuilds all the indexes.

由于SQL Server的工作方式,必须单独对表重新建立索引。 对于具有大量表的数据库,手动执行可能会很麻烦,但是我们的脚本会命中各自数据库中的每个表并重建所有索引。

Verifying Integrity [/Verify]

验证完整性[/ Verify]

In order for a database to remain both functional and produce accurate results, there are numerous integrity items which must be in place. Thankfully, physical and/or logical integrity problems are not very common, but it is good practice to occasionally run the integrity verification process on your databases and review the results.

为了使数据库保持功能正常并产生准确的结果,必须有许多完整性项目。 幸运的是,物理和/或逻辑完整性问题不是很常见,但是最好偶尔在数据库上运行完整性验证过程并查看结果。

When the verify process is run through our script only errors are reported, so no news is good news.

通过我们的脚本运行验证过程时,只会报告错误,因此,没有什么新闻是好消息。

使用脚本 (Using the Script)

The SQLMaint batch script is compatible with SQL 2005 and higher and must be run on a machine which has the SQLCMD tool installed (installed as part of the SQL Server installation). It is recommended you drop this script into a location set in your Windows PATH variable (i.e. C:Windows) so it can easily be called like any other application from the command line.

SQLMaint批处理脚本与SQL 2005及更高版本兼容,并且必须在安装了SQLCMD工具(作为SQL Server安装的一部分安装)的计算机上运行。 建议您将此脚本放到Windows PATH变量(即C:Windows)中设置的位置,以便可以像从命令行中的任何其他应用程序一样轻松地调用它。

To view the help information, simply enter:

要查看帮助信息,只需输入:

SQLMaint /?

SQLMaint /?

批处理脚本使SQL Server数据库维护变得简单

Examples

例子

To run a compact and then a verify on the database “MyDB” using a trusted connection:

要使用受信任的连接在数据库“ MyDB”上运行压缩然后进行验证:

SQLMaint MyDB /Compact /Verify

SQLMaint MyDB /紧凑/验证

To run a reindex and then compact on “MyDB” on the named instance “Special” using the “sa” user with password “123456”:

要运行重新索引,然后使用密码为“ 123456”的“ sa”用户在命名实例“ Special”上的“ MyDB”上进行压缩:

SQLMaint MyDB /S:.Special /U:sa /P:123456 /Reindex /Compact

SQLMaint MyDB / S :.特殊/ U:sa / P:123456 / Reindex / Compact

Using from Inside of a Batch Script

从批处理脚本内部使用

While the SQLMaint batch script can be used like an application from the command line, when you are using it inside of another batch script, it must be preceded with the CALL keyword.

尽管SQLMaint批处理脚本可以像在命令行中一样用作应用程序,但是在另一个批处理脚本中使用它时,必须在其前面加上CALL关键字。

For example, this script runs all maintenance tasks on every non-system database on a default SQL Server installation using trusted authentication:

例如,此脚本使用受信任的身份验证在默认SQL Server安装上的每个非系统数据库上运行所有维护任务:

@ECHO OFFSETLOCAL EnableExtensionsSET DBList=”%TEMP%DBList.txt”SqlCmd -E -h-1 -w 300 -Q “SET NoCount ON; SELECT Name FROM master.dbo.sysDatabases WHERE Name Not IN (‘master’,’model’,’msdb’,’tempdb’)” > %DBList%FOR /F “usebackq tokens=1” %%i IN (%DBList%) DO (CALL SQLMaint “%%i” /Compact /Reindex /VerifyECHO +++++++++++)IF EXIST %DBList% DEL /F /Q %DBList%ENDLOCAL

@ECHO OFFSETLOCAL EnableExtensionsSET DBList =”%TEMP%DBList.txt” SqlCmd -E -h-1 -w 300 -Q“ SET NoCount ON; 从master.dbo.sysDatabases中选择名称,其中名称不在IN(“ master”,“ model”,“ msdb”,“ tempdb”)”>%DBList%FOR / F“ usebackq tokens = 1” %% i IN(%DBList %)做(调用SQLMaint“ %% i” / Compact / Reindex / VerifyECHO +++++++++++)如果存在%DBList%DEL / F / Q%DBList%ENDLOCAL

Download the SQLMaint Batch Script from SysadminGeek.com

从SysadminGeek.com下载SQLMaint批处理脚本

翻译自: https://www.howtogeek.com/52022/batch-script-to-make-sql-server-database-maintenance-simple/

相关文章: