DBCC CHECKDB، Repair و بررسی Consistency در VLDBها
یادداشت حقوقی: کاربر حق ترجمه و بازنشر این اثر را برای پروژه تأیید کرده است.PAGE-324DBCC CHECKDB
DBCC CHECKDB مهمترین دستور یکپارچگی Database است. این دستور مجموعهای از بررسیهای Allocation، Catalog، Table و Index را اجرا میکند و در شرایط خاص Repair نیز انجام میدهد. CHECKDB برای جلوگیری از Corruption نیست؛ وظیفه آن تشخیص زودهنگام خرابی است تا DBA بتواند با Backup/Restore یا روش مناسب واکنش نشان دهد.
Table 9-5 — بازنمایی متن فنی جدول منبع--- PDF PAGE 324 ---
308
DBCC CHECKDB
DBCC CHECKDB is a utility that can be used to both discover corruption and also fix the
errors. When you run DBCC CHECKDB, by default it creates a database snapshot and runs
the consistency checks against this snapshot. This provides a transactionally consistent
point from which the checks can occur while at the same time reducing contention in
the database. It can check multiple objects in parallel to improve performance, but this
depends on the number of cores that are available and the MAXDOP setting of the instance.
Checking for Errors
When you run DBCC CHECKDB for the purpose of discovering corruption only, then you
can specify the arguments, detailed in Table 9-5.
Table 9-5. DBCC CHECKDB Arguments
Argument
Description
NOINDEX
Specifies that integrity checks should be performed on heap and clustered
index structures but not on nonclustered indexes.
EXTENDED_
LOGICAL_CHECKS
Forces the logical consistency of XML indexes, indexed views, and spatial
indexes to be performed.
NO_INFOMSGS
Prevents informational messages from being returned in the results. This
can reduce noise when you are searching for an issue, since only errors
and warnings with a severity level greater than 10 are returned.
TABLOCK
DBCC CHECKDB creates a database snapshot and runs its consistency
checks against this structure to avoid taking out locks in the database,
which cause contention. Specifying this option changes that behavior so
that instead of creating a snapshot, SQL Server takes out a temporary
exclusive lock on the database, followed by exclusive locks on the
structures that it is checking. In the event of high write load, this can reduce
the time it takes to run DBCC CHECKDB, but at the expense of contention
with other processes that may be running. It also causes the system table
metadata validation and service broker validation to be skipped.
(continued)
Chapter 9 Database Consistency
--- PDF PAGE 325 ---
309
DBCC CHECKDB is a very intensive process that can consume many CPU and IO
resources. Therefore, it is advisable to run it during a maintenance window to avoid
performance issues for applications. The Database Engine automatically decides
how many CPU cores to assign the DBCC CHECKDB based on the instance level setting
for MAXDOP and the amount of throughput to the server when the process begins. If
you expect load to increase during the window when DBCC CHECKDB will be running,
however, then you can throttle the process to a single core by turning on Trace Flag
2528. This flag should be used with caution, however, because it causes DBCC CHECKDB
to take much longer to complete. If a snapshot is not generated, either because you have
specified TABLOCK or because there was not enough space on disk to generate a snapshot,
then it also causes each table to be locked for a much longer period.
The sample in Listing 9-8 does not perform any checks but calculates the amount
of space required in TempDB in order for DBCC CHECKDB to run successfully against the
Chapter9 database.
Argument
Description
ESTIMATEONLY
When this argument is specified, no checks are performed. The only thing
that happens is that the space required in TempDB to perform the checks is
calculated based on the other arguments specified.
PHYSICAL_ONLY
When this argument is used, DBCC CHECKDB is limited to performing
allocation consistency checks on the database, consistency checks on
system catalogs, and validation on each page of every table within the
database. This option cannot be used in conjunction with DATA_PURITY.
DATA_PURITY
Specifies that column integrity checks are carried out, such as ensuring
that values are within their data type boundaries. For use with databases
that have been upgraded from SQL Server 2000 or below only. For any
newer databases, or SQL Server 2000 databases that have already been
scanned with DATA_PURITY, the checks happen by default.
ALL_ERRORMSGS
For backward compatibility only. Has no effect on SQL 2019 databases.
Table 9-5. (continued )
Chapter 9 Database Consistency
|
PAGE-325CHECKDB فرایندی Resource-Intensive است و روی Database بزرگ CPU، I/O و TempDB قابل توجهی مصرف میکند. معمولاً Snapshot داخلی برای Consistent View میسازد. Optionهایی مانند NO_INFOMSGS، ALL_ERRORMSGS، PHYSICAL_ONLY، DATA_PURITY و Repair Level دامنه یا خروجی بررسی را کنترل میکنند.
PAGE-326DBCC CHECKDB (N'Chapter9') WITH ESTIMATEONLY;
DBCC CHECKDB (N'Chapter9') WITH NO_INFOMSGS, ALL_ERRORMSGS;
ESTIMATEONLY فضای تقریبی TempDB را برای CHECKDB گزارش میکند. اجرای عادی خطاهای Page/Table را مشخص میکند و در خروجی Repair Level پیشنهادی نیز ممکن است نمایش داده شود.
Figure 9-5 — شکل/تصویر منبع، صفحه PDF 326PAGE-327برای عملیاتیکردن بررسی سلامت، CHECKDB باید به Job زمانبندیشده تبدیل شود و Failure آن به Operator اطلاع داده شود. مشاهده خطا در Job History تنها زمانی مفید است که Alerting مناسب وجود داشته باشد و پیام به تیم DBA برسد.
Figure 9-7 — شکل/تصویر منبع، صفحه PDF 327Figure 9-6 — شکل/تصویر منبع، صفحه PDF 327PAGE-328SQL Server Agent Operator مسیر اعلان Email/Pager/Net Send را تعریف میکند. در محیط امروزی معمولاً Database Mail و Email استفاده میشود. ساخت Operator و اتصال Notification به Job باعث میشود خرابی Consistency پنهان نماند.
Figure 9-8 — شکل/تصویر منبع، صفحه PDF 328PAGE-329رفع خطاها
اولویت Recovery معمولاً Restore از Backup سالم است. Repair با DBCC زمانی استفاده میشود که Backup مناسب در دسترس نیست یا شرایط خاص آن را توجیه میکند. پیش از Repair باید Backup فعلی—even corrupted—برای بررسیهای بعدی گرفته شود و اثر Data Loss پذیرفته شود.
Figure 9-9 — شکل/تصویر منبع، صفحه PDF 329PAGE-330ALTER DATABASE Chapter9 SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
DBCC CHECKDB (N'Chapter9', REPAIR_ALLOW_DATA_LOSS);
ALTER DATABASE Chapter9 SET MULTI_USER;
REPAIR_ALLOW_DATA_LOSS میتواند Page یا Row غیرقابلبازیابی را Deallocate کند. بنابراین موفقیت فنی DBCC بهمعنای حفظ کامل Business Data نیست.
Figure 9-10 — شکل/تصویر منبع، صفحه PDF 330PAGE-331Emergency Mode
اگر Database وارد SUSPECT/RECOVERY_PENDING شود و Restore ممکن نباشد، EMERGENCY Mode اجازه دسترسی محدود Sysadmin را میدهد. CHECKDB در Emergency Repair ممکن است Transaction Log را بازسازی یا عملیات تهاجمی انجام دهد و باید آخرین انتخاب باشد.
Figure 9-11 — شکل/تصویر منبع، صفحه PDF 331Figure 9-12 — شکل/تصویر منبع، صفحه PDF 331PAGE-332برای Lab، مسیر Transaction Log از sys.master_files پیدا و خرابی/عدم دسترسی Log شبیهسازی میشود. در Production هر دستکاری فایل خارج از SQL Server خطرناک است. هدف مثال نشاندادن سناریویی است که Database به Recovery Pending میرود.
Figure 9-13 — بازنمایی از صفحه اصلی PDF 332PAGE-333ALTER DATABASE Chapter9 SET EMERGENCY;
ALTER DATABASE Chapter9 SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
DBCC CHECKDB (N'Chapter9', REPAIR_ALLOW_DATA_LOSS);
ALTER DATABASE Chapter9 SET MULTI_USER;
در Emergency Repair ممکن است Log جدید ساخته شود و Consistency تا حد امکان بازگردانده شود. پس از آن باید همه دادههای حیاتی اعتبارسنجی و Backup کامل جدید تهیه شود.
Figure 9-14 — شکل/تصویر منبع، صفحه PDF 333PAGE-334اگر CHECKDB در Emergency Mode نیز شکست بخورد، گزینههای داخلی محدودتر میشوند و بازیابی از Backup/Replica یا خدمات تخصصی بازیابی مطرح است. برخی Optionها مانند PHYSICAL_ONLY برای Check سریعتر روزانه مفیدند ولی جای Full CHECKDB دورهای را نمیگیرند.
Figure 9-14 — شکل/تصویر منبع، صفحه PDF 334PAGE-335سایر DBCCهای Consistency
- DBCC CHECKALLOC برای Allocation Structureها
- DBCC CHECKCATALOG برای سازگاری Catalog
- DBCC CHECKTABLE برای Table/Indexed View خاص
- DBCC CHECKFILEGROUP برای Filegroup خاص
تقسیم ساده جدولها و جایگزینکردن دائمی CHECKDB با CHECKTABLE ممکن است بررسیهای Cross-Object/Allocation را از دست بدهد؛ بنابراین باید بدانید هر دستور دقیقاً چه چیزی را پوشش میدهد.
PAGE-336DBCC CHECKIDENT
DBCC CHECKIDENT ('dbo.CorruptTable', NORESEED);
CHECKIDENT مقدار Identity فعلی و بیشترین مقدار را بررسی میکند و در صورت نیاز میتواند Reseed انجام دهد. NORESEED فقط گزارش میدهد.
Table 9-6 — بازنمایی متن فنی جدول منبع--- PDF PAGE 336 ---
320
If you have a partitioned table, which is stored on multiple filegroups, DBCC
CHECKFILEGROUP only checks the consistency of the partition(s) that are stored on the
filegroup being checked. The arguments for DBCC CHECKFILEGROUP are the same as those
for DBCC CHECKDB, with the exception of DATA_PURITY, which is not valid and you cannot
specify any repair options. You also need to specify the filegroup name or ID.
DBCC CHECKIDENT
DBCC CHECKIDENT scans all rows within a specified table to find the highest value in the
IDENTITY column. It then checks to ensure that the next IDENTITY value, which is stored
in a table’s metadata, is higher than the highest value in the IDENTITY column of the
table. DBCC CHECKIDENT accepts the arguments detailed in Table 9-6.
We could check the IDENTITY value against the maximum IDENTITY value in our
CorruptTable table by using the command in Listing 9-13.
Listing 9-13. DBCC CHECKIDENT
DBCC CHECKIDENT('CorruptTable',NORESEED) ;
Table 9-6. DBCC CHECKIDENT Arguments
Argument
Description
Table Name
The name of the table to be checked.
NORESEED
Returns the maximum value of the IDENTITY column and the current
IDENTITY value, but will not reseed the column, even if required.
RESEED
Reseeds the current IDENTITY value to that of the maximum IDENTITY
value in the table.
New Reseed Value
Used with RESEED, specifies a seed for the IDENTITY value. This should
be used with caution, since setting the IDENTITY value to lower than the
maximum value in the table can cause errors to be generated, if there is
a primary key or unique constraint on the IDENTITY column.
WITH NO_INFOMSGS
Causes informational messages to be suppressed.
Chapter 9 Database Consistency
|
PAGE-337DBCC CHECKCONSTRAINTS
CHECKCONSTRAINTS Foreign Key و CHECK Constraintها را روی داده موجود اعتبارسنجی میکند. این موضوع زمانی مهم است که Constraint با NOCHECK ایجاد شده یا پس از Repair نسبت به قابلاعتماد بودن Constraint شک وجود دارد.
Figure 9-15 — شکل/تصویر منبع، صفحه PDF 337Table 9-7 — بازنمایی متن فنی جدول منبع--- PDF PAGE 337 ---
321
The results, displayed in Figure 9-15, show that both the maximum value in the
IDENTITY column and the current IDENTITY value are both 10201, meaning that there is
not currently an issue with the IDENTITY value in our table.
DBCC CHECKCONSTRAINTS
DBCC CHECKCONSTRAINTS can check the integrity of a specific foreign key or check
constraint within a table, check all constraints on a single table, or check all
constraints on all tables of a database. DBCC CHECKCONSTRAINTS accepts the arguments
detailed in Table 9-7.
Figure 9-15. DBCC CHECKIDENT results
Table 9-7. DBCC CHECKCONSTRAINTS Arguments
Argument
Description
Table or Constraint
Specifies either the name or ID of the constraint you wish to check or
specifies the name or ID of a table to check all enabled constraints on that
table. Omitting this argument causes all enabled constraints on all tables
within the database to be checked.
ALL_CONSTRAINTS
If DBCC CHECKCONSTRAINTS is being run against an entire table or
entire database, then this option forces disabled constraints to be checked
as well as enabled ones.
ALL_ERRORMSGS
By default, if DBCC CHECKCONSTRAINTS finds rows that violate a
constraint, it returns the first 200 of these rows. Specifying ALL_
ERRORMSGS causes all rows violating the constraint to be returned, even if
this number exceeds 200.
NO_INFOMSGS
Causes informational messages to be suppressed.
Chapter 9 Database Consistency
|
PAGE-338CREATE TABLE dbo.BadConstraint (ID int, Amount int);
ALTER TABLE dbo.BadConstraint WITH NOCHECK
ADD CONSTRAINT chkBadConstraint CHECK (Amount >= 0);
DBCC CHECKCONSTRAINTS ('dbo.BadConstraint');
PAGE-339پس از اصلاح داده، Constraint باید با WITH CHECK CHECK CONSTRAINT دوباره Trusted شود. Optimizer میتواند از Constraint قابلاعتماد برای سادهسازی Plan استفاده کند؛ Constraint Untrusted این مزیت را از دست میدهد.
Figure 9-16 — شکل/تصویر منبع، صفحه PDF 339PAGE-340CHECKDB روی VLDB
برای Very Large Database ممکن است Full CHECKDB هر شب عملی نباشد. PHYSICAL_ONLY بررسیهای فیزیکی سریعتر را اجرا میکند و میتوان Full CHECKDB را با تناوب کمتر، روی Secondary یا با تقسیم مسئولانه workload انجام داد. Backup WITH CHECKSUM و CHECKALLOC نیز بخشی از راهبرد دفاعیاند، نه جایگزین کامل CHECKDB.
PAGE-341جمعبندی راهبرد VLDB
راهحل میتواند ترکیبی باشد: CHECKDB PHYSICAL_ONLY روزانه، Full CHECKDB هفتگی، Offload به Secondary قابل اعتماد، و پایش زمان اجرا/TempDB. هدف آن است که فاصله کشف Corruption با RPO/RTO و چرخه نگهداری Backup سازگار باشد.
PAGE-342DBCC CHECKDB باید بهصورت منظم اجرا شود و نتیجه آن واقعاً مانیتور شود. در صورت Corruption، Restore از Backup سالم معمولاً امنترین مسیر است. Repair—بهخصوص REPAIR_ALLOW_DATA_LOSS—آخرین گزینه است. پس از هر Repair باید Constraintها، دادههای کلیدی و Backup Strategy دوباره بررسی شوند.