Recovery, logging and the transaction log

14
©2011 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice Recovery, Logging and the Transaction Log Miroslav Dimitrov

Transcript of Recovery, logging and the transaction log

Page 1: Recovery, logging and the transaction log

©2011 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice

Recovery, Logging and

the Transaction Log

Miroslav Dimitrov

Page 2: Recovery, logging and the transaction log

Agenda

• Transaction Log Architecture• Log Records• Checkpoints• Transaction Log operations• Recovery and Crash Recovery• Recovery Models and Minimal Logging

Page 3: Recovery, logging and the transaction log

Transaction Log Architecture

• Creating and growing the log file• Log is always zero-initialized• TF 3004 and 3605• VLFs, Log blocks and LSNs• Undocumented DBCC LOGINFO • DBCC SQLPERF (Logspace)

Page 4: Recovery, logging and the transaction log

Log Records

• What are they?• Where they stayed?• Are they all part of transactions?• TVF fn_dblog (startLSN,endLSN) – TF 2537• Compensation records (rollback) – only roll forward

Page 5: Recovery, logging and the transaction log

Conundrum

If a full backup is being taken, then you have transaction updated 100 000 records but it is rolled back why is the diff backup so large? Surely nothing has changed.

Page 6: Recovery, logging and the transaction log

Checkpoints

• Why they exists? • Thresholds 20 ms and 100 ms?!• Log records during checkpoint• Log records are written to disk via three ways:

– When we have commit/rollbacked transaction– When a data file page is written to disk– When a log block hits the maximum of 60 kb size and must be flushed to disk

Page 7: Recovery, logging and the transaction log

Checkpoints (cont.)

• Types:– Automatic– Indirect (new in SQL 2012)– Internal– Manual

• TF 3502 (for which db), 3504 (write latency), 3605

• XE Events - checkpoint_begin and checkpoint_end

Page 8: Recovery, logging and the transaction log

Transaction Log operations

• VLF is either active or inactive• DBCC OPENTRAN• VLF is made inactive by log clearing (truncation)

• Circular nature of log• log_reuse_wait_desc in sys.databases – why log clearing did not succeed last time it was attempted

Page 9: Recovery, logging and the transaction log

Recovery and Crash Recovery

• Recovery brings db to a consistent point in time

• Types:– Crash recovery after a system failure– Recovery at the end of restore– Recovery after a failover (CL, AG, Mirroring)

• REDO• UNDO

Page 10: Recovery, logging and the transaction log

Recovery and Crash Recovery (cont.)

• Recovery during restore• Crash Recovery• Where the crash recovery stops?!• How SQL is recovering DBs? – TF 3408 single thread

• Fast Recovery (from SQL 2005 onwards – only ENT) – does not apply for restore

Page 11: Recovery, logging and the transaction log

Recovery Models and Minimal Logging

• Minimal Logged vs Efficiently Logged• FULL• SIMPLE• BULK-LOGGED (why to use it only temp)• Minimally Logged:

– Creating, Dropping, Rebuilding indexes (reorganize is always fully logged)

– Bulk operations – bcp, bulk insert etc.

Page 12: Recovery, logging and the transaction log

Recovery Models and Minimal Logging (cont.)

• pseudoSimple recovery model?• Is switching between FULL and BULK-LOGGED breaking the log backup chain?

• Deferred drop and TRUNCATE table (two ways)

Page 13: Recovery, logging and the transaction log

Questions?

Page 14: Recovery, logging and the transaction log

Demo time