Home Database Mysql Tutorial 如何大幅提高DBCC CHECKDB/DBCC CHECKTABLE的性能

如何大幅提高DBCC CHECKDB/DBCC CHECKTABLE的性能

Jun 07, 2016 pm 05:54 PM

随着时间的推移,数据库变的越来越大,几百个GB甚至几个TB大小的数据库越来越多。为了检查数据库的完整性,定期运行DBCC CHECKDB/CHECKTABLE是最佳实践。但是随着数据库的增大,如何缩短DBCC CHECKDB/CHECKTABLE的运行时间是DBA常常需要面对的一个挑战。本短

随着时间的推移,变的越来越大,几百个GB甚至几个TB大小的越来越多。为了检查数据库的完整性,定期运行DBCC CHECKDB/CHECKTABLE是最佳实践。但是随着数据库的增大,如何缩短DBCC  CHECKDB/CHECKTABLE的运行时间是DBA常常需要面对的一个挑战。本短文介绍一些方法,可以大幅缩短常规CHECKDB/CHECKTALE 的运行时间。

正常情况下,CHECKDB/CHECKTABLE的运行不会对数据库使用排它锁,而是使用内部数据库快照(internal database snapshot)。 这个内部数据库快照实质就是Sparse Filestream, 它使用sparse file,COPY-ON-WRITE技术。详细的工作原理可以参考如下的文档:

数据库快照的工作方式

简单说,对数据库快照的读操作如下图所示:

如果你想观察DBCC CHECKDB/CHECKTABLE运行时的快照,你可以使用streams.exe工具。我使用它观测到如下的结果:

上图中的 “MSSQL_DBCC10:$DATA”就是附加在testdb.mdf后面的Sparse Filestream。后面的那串数字是数据库加上stream的总的大小,这个和下面的语句观察到的size_on_disk_bytes是一致的:

select * from  sys.dm_io_virtual_file_stats(5,1)

 

但是要注意到,因为sparse filestream并不实际占有磁盘大小,上面的大小只是一个空间的保留,并不是磁盘上真的有这么多的数据存在,并不真正占有磁盘的这么大的空间。

言归正传,上面介绍的internal snapshot (也就是sparse filestream)有什么关系呢?

先做个实验,运行CHECKDB几次看看运行时间:

DBCC CHECKDB(TESTDB)

这个运行了大概50秒的时间。然后我使用TABLOCK选项测试几次:

DBCC CHECKDB(TESTDB)

withTABLOCK

天啊,它只需要大概5秒的时间就跑完了,整整快了10倍!读到这里,你知道了第一个大幅缩短CHECKDB/CHECKTABLE的办法,就是使用TABLOCK。这个hint 告诉SQL server 使用锁来进行检查,但也影响了数据库用户的使用。比如在检查某个table 的时候,就可能无法对这个table进行修改。 那么有没有更好的不影响用户的办法呢?有的,就是使用snapshot 数据库。

首先建立一个snapshot 数据库:

createdatabase myTESTDB_snapshot

on

( name =TESTDB_Data ,filename='C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\TESTDB_Data.ss')

asSNAPSHOTOF TESTDB

然后对这个snapshot数据库进行CHECKDB:

DBCC CHECKDB(myTESTDB_snapshot)

结果令人惊讶,它也仅仅使用了5秒,和使用TABLOCK一样的性能! 太令人兴奋了,不影响用户的情况下比原来的CHECKDB快了10倍。你发现了什么问题吗?恩,对,我是对myTESTDB_snapshot进行CHECKDB,而不是TESTDB。这样可以么?可以的, 原因自己思考。可以参考snapshot的工作原理来考虑。

那么对CHECKTABLE有什么不一样么? 我测试的结果类似。 就是说, 使用snapshot 数据库来进行CHECKTABLE的性能和使用TABLOCK的性能类似,都能大幅缩短检查的时间。测试的结果如下:

DBCC CHECKDB

40-50 seconds

DBCC CHECKDB with TABLOCK

5 seconds

DBCC CHECKDB on snapshot database

5 seconds

DBCC CHECKTABLE Batch

8-12 minutes

DBCC CHECKTABLE Batch with TABLOCK

18 seconds

DBCC CHECKTABLE Batch on Snapshot database

20 seconds

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain InnoDB Full-Text Search capabilities. Explain InnoDB Full-Text Search capabilities. Apr 02, 2025 pm 06:09 PM

InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

How do you alter a table in MySQL using the ALTER TABLE statement? How do you alter a table in MySQL using the ALTER TABLE statement? Mar 19, 2025 pm 03:51 PM

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

When might a full table scan be faster than using an index in MySQL? When might a full table scan be faster than using an index in MySQL? Apr 09, 2025 am 12:05 AM

Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

Can I install mysql on Windows 7 Can I install mysql on Windows 7 Apr 08, 2025 pm 03:21 PM

Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

Difference between clustered index and non-clustered index (secondary index) in InnoDB. Difference between clustered index and non-clustered index (secondary index) in InnoDB. Apr 02, 2025 pm 06:25 PM

The difference between clustered index and non-clustered index is: 1. Clustered index stores data rows in the index structure, which is suitable for querying by primary key and range. 2. The non-clustered index stores index key values ​​and pointers to data rows, and is suitable for non-primary key column queries.

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? Mar 21, 2025 pm 06:28 PM

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

How do you handle large datasets in MySQL? How do you handle large datasets in MySQL? Mar 21, 2025 pm 12:15 PM

Article discusses strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.

How do you drop a table in MySQL using the DROP TABLE statement? How do you drop a table in MySQL using the DROP TABLE statement? Mar 19, 2025 pm 03:52 PM

The article discusses dropping tables in MySQL using the DROP TABLE statement, emphasizing precautions and risks. It highlights that the action is irreversible without backups, detailing recovery methods and potential production environment hazards.

See all articles