280 likes | 313 Views
A Whirlwind tour of DBCC CHECKDB. Arun Sirpal. Who I am?. Objectives. What is CHECKDB?. Consistency / Integrity checks for your SQL Server databases. If we have corruption we need to know about it sooner rather than later! It will manifest into your on-disk backups. Causes of corruption?.
E N D
A Whirlwind tour of DBCC CHECKDB Arun Sirpal
What is CHECKDB? Consistency / Integrity checks for your SQL Server databases. If we have corruption we need to know about it sooner rather than later! It will manifest into your on-disk backups. Causes of corruption? 99% of the time its down to your I/O Subsystem – lots of moving parts. If it does happen the key is being prepared for it. What doesn’t cause corruption? Interrupting Index Rebuilds. Shutting down SQL Server ( including NOWAIT option ). Stopping Shrinking operations.
History of CHECKDB Firstly, CHECKDB needs to get a transactionally consistent view of your database - how this has happened has changed over time. SQL 7.0: Locking was the strategy – similar to that of the WITH TABLOCK option in SQL 2000 and 2005. Concurrency issues. SQL 2000: Transaction Log Analysis – after reading the databases pages CHECKDB would then read the transaction log. Single-threaded and prevented log truncation SQL 2005+: Written by Paul Randal where an Internal database snapshot is used. The database snapshot is created in the same location as the existing database then is removed when DBCC CHECKDB finishes. (Hidden Snapshot until SQL 2014) The best method - Only works on NTFS not FAT or ReFS (until 2014).
Bad practices Some Advice
Disappearing corruption? Story: CHECKDB complained about corruption overnight. In the morning user ran CHECKDB but found no corruption! The maintenance job phenomenon SQL maintenance took place overnight after CHECKDB that rebuilt indexes – hence no corruption – Pages no longer allocated.
What CHECKDB cannot fix We need VALID backups! Do not want to do manual extraction.
Going Parallel Standard Edition – checks always single threaded. For Enterprise Edition, CHECKDB respects the maximum degree of parallelism setting configured for the instance (or the MAXDOP hint) You can confirm this via querying DMVs – Scheduler Ids.
I usually execute: DBCC CHECKDB (' AdventureWorks2014 ') WITH NO_INFOMSGS I personally do not want to see benign output that is just informational. If you run it without the NO_INFOMSGS you will get the following: DBCC results for 'AdventureWorks2014'. Service Broker Msg 9675, State 1: Message Types analyzed: 14. Service Broker Msg 9676, State 1: Service Contracts analyzed: 6. Service Broker Msg 9667, State 1: Services analyzed: 3. Service Broker Msg 9668, State 1: Service Queues analyzed: 3. Service Broker Msg 9669, State 1: Conversation Endpoints analyzed: 0. Service Broker Msg 9674, State 1: Conversation Groups analyzed: 0. Service Broker Msg 9670, State 1: Remote Service Bindings analyzed: 0. Service Broker Msg 9605, State 1: Conversation Priorities analyzed: 0. DBCC results for 'sys.sysrscols'. <SNIPPET> CHECKDB found 0 allocation errors and 0 consistency errors in database 'AdventureWorks2014'. DBCC execution completed. If DBCC printed error messages, contact your system administrator.
When did it last run? Couple of options here Check the BOOT page: TF 3604, you need this to get the DBCC PAGE output to your console DBCC TRACEON (3604); GO — page 9 is the boot page DBCC PAGE (yourDB, 1, 9, 3); GO
2) My personal favourite is to run this code DBCC DBINFO ('AdventureWorks2014') WITH TABLERESULTS If you break CHECKDB into separate checks – Value does not get updated.
How long will it take? IT DEPENDS – more specifically on the following factors. • Size and complexity of the database – bigger database means more pages to read. • CPU and I/O activity on the server, CHECKDB is resource intensive so if we are running it on a server that is already I/O and CPU bound it could take a while. • Capability of your I/O subsystem – do you have SSDs? • CHECKDB consumes a lot of memory. Consider putting CHECKDB in a Resource Group to reduce the memory which increases the speed. • Do you have Enterprise edition? If so you could go parallel meaning LOWER run times. *If parallel checking is an issue you can force it to be single threaded via TF2528*
Do not cancel CHECKDB Is CHECKDB taking its time? Could be doing a “deep-dive” . This happens if it finds corruption – DO NOT CANCEL it. Let it finish! Do you use Computed columns? Prior to SQL 2016 when running DBCC CHECKDB against indexes with computed columns you would suffer from DBCC – OBJECT – METADATA latch contention. SQL 2016 addressed this by changing the behaviour – checks on this type of index no longer the default. User needs to issue EXTENDED_LOGICAL_CHECKS. The Workround? Disable those indexes and rebuild them after the checks.
VLDB Techniques? More for the “larger” databases. Option (1): DBCC CHECKDB essentially runs: DBCC CHECKCATALOG, DBCC CHECKALLOC and DBCC CHECKTABLE. – You could split these commands over couple of days – see my link on some code: https://blobeater.blog/2015/05/16/carving-up-checkdb/ What I have done here is query sys.tables for all the tables within my database and split them into buckets and execute DBCC CHECKTABLE against a bucket and then DBCC CHECKALLOC and CHECKCATALOG independently.
Option (2) - DBCC CHECKFILEGROUP Again some code to read: https://blobeater.blog/2016/09/13/dbcc-checkfilegroup/ If you are using File groups then you are ready to run this BUT you will need to execute DBCC CHECKCATALOG independently. Option (3) – Offload the check to a different server (via backups) This frees up resource on your production server BUT if you get a corruption error on the other server it should probe you to check production. Option (4) – Consider using PHYSICAL_ONLY. This does reduce the time to take but it is not as thorough. Recommended that a full run of DBCC CHECKDB to be performed periodically
Option (5) – Consider using your own database snapshot and run CHECKDB against it. Enterprise edition only (Pre SQL Server 2016 SP1) My facts: 880GB took 6 hours – used the “Buckets” technique to spread the load over 2 days.
Repairing Repair Fast option – Does nothing, here for backward compatibility. If you use REPAIR_ALLOW_DATA_LOSS you will lose data (as the name suggests). REPAIR_REBUILD will not fix everything, REPAIR_REBUILD only fixes problem in nonclustered indexes. Repairs are always offline, in that the database must be in single-user mode.
Repairing(2) STORY: I accidentally ran REPAIR_ALLOW_DATA_LOSS ON msdb. Go to backup instead because it could delete a key record which causes an issue with an agent job. Depending on your context REPAIR could be the option to use over a backup recovery i.e. 2 NCIs to be REPAIR_REBUILD VS 200GB backup restore? Quote from Paul “Repair's a dangerous beast and should only be used as a last resort”......
What does CHECKDB not do? It does NOT do: DBCC CHECKIDENT – A command to check and re-seed values for an identity column. DBCC CHECKCONSTRAINTS – Checks the integrity of a specified constraint or all constraints on a specified table (or all tables) – something that SHOULD BE executed after a REPAIR option.