Code Camp NZ 2011

33
Code Camp NZ 2011 #CCNZ www.mscommunities.co.nz

description

Code Camp NZ 2011. # CCNZ. www.mscommunities.co.nz. Minimal Logging & Data Manoeuvring On very Large Tables. What is going on here???. Steven Wang. Senior DBA/Senior BI Specialist at BNZ MCITP/MCTS: BI Developer, Database Developer and Database Administrator - PowerPoint PPT Presentation

Transcript of Code Camp NZ 2011

Page 1: Code Camp NZ 2011

Code Camp NZ 2011

#CCNZ

www.mscommunities.co.nz

Page 2: Code Camp NZ 2011

Minimal Logging & Data Manoeuvring On very Large Tables

What is going on here???

Page 3: Code Camp NZ 2011

Steven Wang

• Senior DBA/Senior BI Specialist at BNZ• MCITP/MCTS: BI Developer, Database

Developer and Database Administrator• Owner of TellYes Data Intelligence• Blogs: www.msbicoe.com• Email: [email protected]

Page 4: Code Camp NZ 2011

Agenda

• What Is minimal logging and why does it matter?

• What can be minimally logged?• Metadata-Only Operations• Things under the hood (Demo)• Data Manoeuvring technics on very large

tables• Real world example• Q & A

Page 5: Code Camp NZ 2011

Small Quiz

A. A minimally logged operation will always reduce the transaction log backup size?

True False

B. A minimally logged operation will always reduce the transaction log size?

True False

C. A minimally logged operation is always faster than fully logged operation ?

True False

Page 6: Code Camp NZ 2011

Answer

A. A minimally logged operation will always reduce the transaction log backup size?

True False

B. A minimally logged operation will always reduce the transaction log size?

True False

C. A minimally logged operation is always faster than fully logged operation?

True False

Page 7: Code Camp NZ 2011

What is Minimal Logging and why does it matter?

• SQL server uses ‘write-ahead logging’. Everything has to be written on to log file first

• A minimally logged operation is one that does not always log every row; it only logs the extend allocations

• Transaction log will fill rapidly under full recovery model, it is likely to be grown during a large quantity of data operation

• “The transaction log is ALWAYS zero initialized when first created, manually grown, or auto-grown.” --Paul Randal

Page 8: Code Camp NZ 2011

What can be minimally logged? • SELECT INTO …. FROM Table

• Bulk Import Operation: BCP BULK Insert INSERT INTO … SELECT FROM

OPENROWSET(BULK…)

• Create/Rebuild Index

• Using the .Write in the Update for new data

• The Creation of new heap by Dropping Index

Page 9: Code Camp NZ 2011

Conditions

• It has to be under Bulk-Logged or Simple Recovery Model

• Table is not being replicated

• Tablock hint needs to be used

• Something talking about later

Page 10: Code Camp NZ 2011

Conditions (Continue)

Heap + Tablock

Data Data Index

Data Data IndexEmpty

Not Empty

H e a p H e a p + I n d e xMinimal Logging Full Logging

Page 11: Code Camp NZ 2011

Conditions (Continue)Clustered + Tablock

Data Data Index

Data Data IndexEmpty

Not Empty

C l u s t e r e d C l u s t e r e d + I n d e x

Minimal Logging Full Logging

Page 12: Code Camp NZ 2011

Conditions (Continue)

• Use Trace Flag 610 Can be used to get minimal logging

for empty heap and clustered table Can be used to get minimal logging

in a non-empty B-Tree Tablock hint is not needed for table

without nonclustered index The first page is always fully logged

• Can be turned on instance-wide or session-wide

• Fully tested in your environment before use

Page 13: Code Camp NZ 2011

Metadata-Only Operations

• Truncate Table

• Drop Table

• Partition Switch

• Partition Merge for 2 empty partitions

• Partition Split for an empty partition

Page 14: Code Camp NZ 2011

Things Under the Hood (Demo)

Page 15: Code Camp NZ 2011

Data Manoeuvring technics on very large tables

• Bulk Load Partitioned Table Use parallelism, per bulk load per

CPU core Create staging tables having exactly

same structure as target table in same filegroup with no indexes

Bulk load data into staging tables Create indexes and constraints on

staging tables. Switch the staging tables into

partitioned table partitions

Page 16: Code Camp NZ 2011

Alter database MyDB Set recovery Bulk_Logged;GoAlter Database MyDB Modify Filegroup FG1 Default;GOSelect Col1, Col2, Col3,…Into Stage_1From Target_Table Where 0=1Go...GoAlter Database MyDB Modify Filegroup FG4 Default;GOSelect Col1, Col2, Col3,…Into Stage_4From Target_Table Where 0=1Go

Page 17: Code Camp NZ 2011

Data_201104

Data_201103

Data_201102

Data_201101

Stage_4(FG4)

Stage_3(FG3)

Stage_2(FG2)

Stage_1(FG1)

Partition_4(FG4)

Source Data File Staging TablesDestination

Partitioned Table

Partition_3(FG3)

Partition_2(FG2)

Partition_1(FG1)

Page 18: Code Camp NZ 2011

Bulk Insert

Bulk Insert

Bulk Insert

Bulk Insert

Data_201104

Data_201103

Data_201102

Data_201101

Stage_4(FG4)

Stage_3(FG3)

Stage_2(FG2)

Stage_1(FG1)

Partition_4(FG4)

Source Data File Staging TablesDestination

Partitioned Table

Partition_3(FG3)

Partition_2(FG2)

Partition_1(FG1)

Page 19: Code Camp NZ 2011

Bulk Insert

Bulk Insert

Bulk Insert

Bulk Insert

Data_201104

Data_201103

Data_201102

Data_201101

Stage_4(FG4)

Stage_3(FG3)

Stage_2(FG2)

Stage_1(FG1)

Partition_4(FG4)

Source Data File Staging TablesDestination

Partitioned Table

Partition_3(FG3)

Partition_2(FG2)

Partition_1(FG1)

1. Create Indexes;2. Apply other settings to comply with

target;3. Create constraints;

Page 20: Code Camp NZ 2011

Bulk Insert

Bulk Insert

Bulk Insert

Bulk Insert

Data_201104

Data_201103

Data_201102

Data_201101

Stage_4(FG4)

Stage_3(FG3)

Stage_2(FG2)

Stage_1(FG1)

Partition_4(FG4)

Source Data File Staging TablesDestination

Partitioned Table

Partition_3(FG3)

Partition_2(FG2)

Partition_1(FG1)

Alter Table Stage_1 SWITCH TO MyTargetTable Partition 1;Go...GoAlter Table Stage_4 SWITCH TO MyTargetTable Partition 4;

Page 21: Code Camp NZ 2011

Bulk Insert

Bulk Insert

Bulk Insert

Bulk Insert

Data_201104

Data_201103

Data_201102

Data_201101

Stage_4(FG4)

Stage_3(FG3)

Stage_2(FG2)

Stage_1(FG1)

Partition_4(FG4)

Source Data File Staging TablesDestination

Partitioned Table

Partition_3(FG3)

Partition_2(FG2)

Partition_1(FG1)

Switch In

Switch In

Switch In

Switch In

Page 22: Code Camp NZ 2011

Data Manoeuvring technics on very large tables (Continues)

• Delete a very large amount of rows in a table, for example, over 50% of the total Don’t Delete! Think of using truncate

or drop table instead.

How??

Page 23: Code Camp NZ 2011

Start

Alter Database MyDB Set recovery bulk_Logged

Select Data need to keep into a new table

Create indexes and other settings as

needed

Drop the Original Table

Rename the new table to Original table name

Set the recovery model back to full

End

Delete a very large amount of data from a non-partitioned table

Page 24: Code Camp NZ 2011

Partition_4(FG4)

Partitioned Table

Partition_3(FG3)

Partition_2(FG2)

Partition_1(FG1)

Delete a very large amount of data from a partitioned table

Switch OutStage_4

(FG4)

Switch OutStage_3

(FG3)

All Data in the partition need to be deleted

More than half of the data need to be

deleted

Select Into Stage_1(FG1)

Page 25: Code Camp NZ 2011

Partition_4(FG4)

Partitioned Table

Partition_3(FG3)

Partition_2(FG2)

Partition_1(FG1)

Delete a very large amount of data from a partitioned table

Stage_4(FG4)

Stage_3(FG3)

Switch OutStage_1

(FG3)Stage_1

(FG1)

Drop Stage Table

Page 26: Code Camp NZ 2011

Partition_4(FG4)

Partitioned Table

Partition_3(FG3)

Partition_2(FG2)

Partition_1(FG1)

Delete a very large amount of data from a partitioned table

Switch InStage_1(FG1)

Page 27: Code Camp NZ 2011

Data Manoeuvring technics on very large tables (Continues)

Really????

• Update a very large amount of rows in a table for majority of columns Don’t Update! Think of using

inserting data instead to achieve minimal logging.

Page 28: Code Camp NZ 2011

Update a very large amount of data for a non-partitioned table

Existing Data

MyData

New Data

MyData_New

Select A.pk_ID, Coalesce(B.Col1, A.Col1) As Col1, Coalesce(B.Col2, A.Col2) As Col2, ...

Into MyData_TempFrom MyData A

Left Outer JoinMyData_New B

On A.pk_ID = B.pk_ID

Updated Data

MyData_Temp

• Drop MyData• Truncate MyData_New• Rename MyData_Temp

to MyData

Page 29: Code Camp NZ 2011

Update a very large amount of data for a partitioned table

I believe that you have already got a fairly good idea how to apply the similar technical to perform the update on partitioned table.

Page 30: Code Camp NZ 2011

Real World Example

• A Table has to be partitioned daily• The data has to be kept more than 7

years• Every first day of the month a partition

merge operation will be performed to consolidate one month’s partitions which is 30 month old to one partition

• How will you do it?

Page 31: Code Camp NZ 2011

Thank You

• Email: [email protected]• Blogs: www.msbicoe.com• Linkedin:

http://nz.linkedin.com/pub/steven-wang/1b/ab/b8b

Page 32: Code Camp NZ 2011

PremierPartners

Sponsor

Thanks to our sponsorsand partners!

AssociatedPartners

SupportingPartners

Page 33: Code Camp NZ 2011