SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.
SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.
Successfully reported this slideshow.
Activate your 14 day free trial to unlock unlimited reading.
How minimalloggingcanhelpme bartoszratajczyk_sql_sat534
4.
The plan
• What is minimal logging?
• How does it work?
(and when?)
• What are the profits?
(and downsides)
• What is efficient logging?
• TRUNCATE TABLE logging?
SQL SATURDAY | #534 | KRAKÓW 2016
5.
Bartosz Ratajczyk
SQL Server consultant
Database programmer
ETL programmer
Database administrator
MCSE: Data Platform, MCT, MCTS SQL Server 2008
http://bartekr.net | b.ratajczyk@gmail.com
SQL SATURDAY | #534 | KRAKÓW 2016
6.
Transaction log
• Keeps track of every data modification in a
database
• Quantity of information saved in a
transaction log may depend on recovery
model
SQL SATURDAY | #534 | KRAKÓW 2016
7.
Recovery models
• FULL
• allows point-in-time recovery
• requires log management
• BULK LOGGED
• point-in-time recovery not possible
• requires log management
• SIMPLE
• point-in-time recovery not possible
• takes care of log management
8.
Minimal logging
„At a minimum, enough information has to be logged when
minimally logged operation is performed to allow SQL Server to
rollback a transaction that has failed.”
Kalen Delaney, SQL Server Internals 2012
„Minimal logging involves logging only the information that is
required to recover the transaction without supporting point-in-
time recovery.”
BOL: https://msdn.microsoft.com/en-us/library/ms191244.aspx
SQL SATURDAY | #534 | KRAKÓW 2016
9.
Minimal logging (simplified)
• Contains only data alocation information *)
• So we save on I/O operations
BUT:
• Log does not contain all information required to
recover lost data
• Log backup will contain pages that changed
*) well, not exactly
SQL SATURDAY | #534 | KRAKÓW 2016
10.
Logging types (simplified)
• Full – the operation has its own log record
• Efficient – the changed page has its own
log record
• Minimal – we log only page allocation
operations *)
*) well, not exactly
SQL SATURDAY | #534 | KRAKÓW 2016
11.
Log investigation
SELECT *
FROM fn_dblog(NULL, NULL)
SELECT *
FROM fn_dblog(@StartLSN, @EndLSN)
SQL SATURDAY | #534 | KRAKÓW 2016
13.
What can be minimally logged?
• BULK INSERT
• bcp
• SELECT INTO
• INSERT INTO .. SELECT
• CREATE INDEX
• ALTER INDEX REBUILD, DBCC DBREINDEX
• REORGANIZE always fully logged
• .WRITE
• WRITETEXT, UPDATETEXT
DEPRECATED
SQL SATURDAY | #534 | KRAKÓW 2016
14.
Requirements summary
non-FULL recovery model
AND NOT replicated
AND (
(Heap AND TABLOCK)
OR (B-tree AND empty AND TABLOCK)
OR (B-tree AND empty AND TF-610)
OR (B-tree AND nonempty AND TF-610 AND
NEW key-range)
)
Source: Itzik Ben-Gan, http://sqlmag.com/t-sql/minimally-logged-inserts
SQL SATURDAY | #534 | KRAKÓW 2016
15.
One more trick
If you can’t use TABLOCK then use:
EXEC sys.sp_tableoption
@TableNamePattern = N’dbo.Table’,
@OptionName = ’table lock on bulk load’,
@OptionValue = ’ON’
SQL SATURDAY | #534 | KRAKÓW 2016
16.
Pros
• Faster transactions and log saving
• Smaller I/O
SQL SATURDAY | #534 | KRAKÓW 2016
17.
And not pros
• Sensitive for data file errors
• Requires SIMPLE recovery model or switching
between FULL and BULK LOGGED
• So we won’t use Mirroring and Availability Groups
• But Log Shipping still can work
• May require TF 610 – sysadmin
• All data pages have to be written to disk before
transaction ends
SQL SATURDAY | #534 | KRAKÓW 2016
18.
TRUNCATE TABLE
• Lightning fast data deletion
• Is it logged or not?
• Why is it so fast?
SQL SATURDAY | #534 | KRAKÓW 2016
20.
What to remember
• Minimal logging is just saving page
allocation information in transaction log *)
• It makes some operations quick, but is
sensitive for data file errors
• TRUNCATE is fully logged operation
*) well, not exactly
SQL SATURDAY | #534 | KRAKÓW 2016
21.
Some things to read
• Itzik Ben-Gan „Minimally logged inserts: http://sqlmag.com/t-
sql/minimally-logged-inserts
• Gail Shaw, Tony Davis „Managing the Log in BULK_LOGGED
Recovery Model”
http://www.sqlservercentral.com/articles/Stairway+Series/94552/
• Paul Randall „The Myth that DROP and TRUNCATE TABLE are
Non-Logged” http://sqlperformance.com/2013/05/sql-
performance/drop-truncate-log-myth
• Remus Rusanu „How to read and interpret the SQL Server log”
http://rusanu.com/2014/03/10/how-to-read-and-interpret-the-sql-
server-log/
SQL SATURDAY | #534 | KRAKÓW 2016