size does matter

90
Database Design: Size DOES Matter! Thomas LaRock Technical Evangelist and Senior DBA Confio Software 3/19/2013 1

Upload: thomas-larock

Post on 20-Jul-2015

984 views

Category:

Technology


5 download

TRANSCRIPT

Page 1: Size does matter

Database Design: Size DOES Matter!

Thomas LaRock

Technical Evangelist and Senior DBA

Confio Software

3/19/2013 1

Page 2: Size does matter

Why Are You Here?

• You have data

• It is growing larger

• Performance suffers

• You want to make things better

– Or you have been told to make things better

3/19/2013 2

Page 3: Size does matter

A little about me…

Thomas LaRock

[email protected] thomaslarock.com

@sqlrockstar

Page 4: Size does matter

Agenda

• What’s the problem?

• Why datatypes matter

• Solution options

3/19/2013 4

Page 5: Size does matter

THE PROBLEM

3/19/2013 5

Page 6: Size does matter

The Problem

• Your database has a design

– Just like a ship, or a car, has a design

3/19/2013 6

Page 7: Size does matter

The Problem

• Your database has a design

– Just like a ship, or a car, has a design

3/19/2013 7

Page 8: Size does matter

The Problem

• Your database has a design

– Just like a ship, or a car, has a design

• Data goes in, rarely falls off

3/19/2013 8

Page 9: Size does matter

The Problem

• Your database has a design

– Just like a ship, or a car, has a design

• Data goes in, rarely falls off

• Databases are mixed-use

3/19/2013 9

Page 10: Size does matter

The Problem

• Your database has a design

– Just like a ship, or a car, has a design

• Data goes in, rarely falls off

• Databases are mixed-use

• Data professionals are often asked to convert a pickup truck into

3/19/2013 10

Page 11: Size does matter

The Problem

• Your database has a design

– Just like a ship, or a car, has a design

• Data goes in, rarely falls off

• Databases are mixed-use

• Data professionals are often asked to convert a pickup truck into a Ferrari

3/19/2013 11

Page 12: Size does matter

The Problem

• Your database has a design

– Just like a ship, or a car, has a design

• Data goes in, rarely falls off

• Databases are mixed-use

• Data professionals are often asked to convert a pickup truck into a Ferrari

• Great database performance starts with great database design

3/19/2013 12

Page 13: Size does matter

How Database Designs Fail

• Many designs do not account for size

3/19/2013 13

Page 14: Size does matter

How Database Designs Fail

• Many designs do not account for size

• Rowcounts might be considered

3/19/2013 14

Page 15: Size does matter

How Database Designs Fail

• Many designs do not account for size

• Rowcounts might be considered

• What about inside the rows?

– Those are the columns

– Their width is often overlooked

3/19/2013 15

Page 16: Size does matter

WHY DATATYPES MATTER

3/19/2013 16

Page 17: Size does matter

3/19/2013 ‹#›

Page 18: Size does matter

3/19/2013 ‹#›

Page 19: Size does matter

3/19/2013 ‹#›

Page 20: Size does matter

3/19/2013 ‹#›

Page 21: Size does matter

3/19/2013 ‹#›

Page 22: Size does matter

3/19/2013 ‹#›

Page 23: Size does matter

3/19/2013 ‹#›

Page 24: Size does matter

3/19/2013 ‹#›

Page 25: Size does matter

What Is an Integer?

• Math major says: integers are numbers, positive and negative, including 0, without any fractions

3/19/2013 25

Page 26: Size does matter

What Is an Integer?

• Math major says: integers are numbers, positive and negative, including 0, without any fractions

• Database designer: integers are the range of numbers from -2,147,483,648 to 2,147,483,647 and require 4 bytes of storage

3/19/2013 26

Page 27: Size does matter

What Is an Integer?

• Math major says: integers are numbers, positive and negative, including 0, without any fractions

• Database designer: integers are the range of numbers from -2,147,483,648 to 2,147,483,647 and require 4 bytes of storage

• Do you see a disconnect there?

3/19/2013 27

Page 28: Size does matter

Visual Studio Defaults

3/19/2013 28

Page 29: Size does matter

Visual Studio Defaults

3/19/2013 29

Page 30: Size does matter

Visual Studio Defaults

3/19/2013 30

Page 31: Size does matter

Visual Studio Defaults

3/19/2013 31

Mismatch Likely!

Page 32: Size does matter

SSMS Defaults

3/19/2013 32

Page 33: Size does matter

SSMS Defaults

3/19/2013 33

Page 34: Size does matter

SSMS Defaults

3/19/2013 34

Page 35: Size does matter

SSMS Defaults

3/19/2013 35

Page 36: Size does matter

Date Example

Datatype Name Length (bytes)

Date 3

Smalldatetime 4

Time 5

Datetime2 6, 7, or 8

Datetime 8

Datetimeoffset 10

3/19/2013 36

Page 37: Size does matter

Where Will This Hurt?

• Disk space

• Memory space

• Performance

3/19/2013 37

Page 38: Size does matter

Datatype Pain #1: Disk Space

• Disk is cheap…sure it is…but there are hidden costs!

– More storage = longer maintenance

– More storage = longer database backups

– More storage = longer tape backups

3/19/2013 38

Page 39: Size does matter

Datatype Pain #1: Disk Space

• Disk is cheap…sure it is…but there are hidden costs!

– More storage = longer maintenance

– More storage = longer database backups

– More storage = longer tape backups

• Most are unaware how that tiny piece of data is stored multiple times

– Wide clustering key is spread to EVERY NC index!

3/19/2013 39

Page 40: Size does matter

Datatype Pain #2: Memory Space

• Those extra bytes are read from disk into SQL Server buffer cache

3/19/2013 40

Page 41: Size does matter

Datatype Pain #2: Memory Space

• Those extra bytes are read from disk into SQL Server buffer cache

• Extra Logical I/O needed to return a query result

3/19/2013 41

Page 42: Size does matter

Datatype Pain #2: Memory Space

• Those extra bytes are read from disk into SQL Server buffer cache

• Extra Logical I/O needed to return a query result

• What about indexes?

– Same thing, they drag around the extra I/O

3/19/2013 42

Page 43: Size does matter

EXTRA LOGICAL I/ODEMO

3/19/2013 43

Page 44: Size does matter

Datatype Pain #3: Performance

• Datatype mismatch results in implicit conversions

3/19/2013 44

Page 45: Size does matter

Datatype Pain #3: Performance

• Datatype mismatch results in implicit conversions

• Optimizer knows this is bad, will display a warning for you

3/19/2013 45

Page 46: Size does matter

Datatype Pain #3: Performance

• Datatype mismatch results in implicit conversions

• Optimizer knows this is bad, will display a warning for you

• It is up to you to make a change, SQL Server won’t change your code for you!

3/19/2013 46

Page 47: Size does matter

Datatype Pain #3: Performance

• Datatype mismatch results in implicit conversions

• Optimizer knows this is bad, will display a warning for you

• It is up to you to make a change, SQL Server won’t change your code for you!

• Issue with code generators (ADO.NET 3.5, EMF, nHibernate, LINQ)

– Supposedly fixed?

3/19/2013 47

Page 48: Size does matter

IMPLICIT CONVERSIONSDEMO

3/19/2013 48

Page 49: Size does matter

SOLUTION OPTIONS

3/19/2013 49

Page 50: Size does matter

First Step: Admit You Have a Problem!

• First sign: when OLAP queries interfere with OLTP processing

3/19/2013 50

Page 51: Size does matter

First Step: Admit You Have a Problem!

• First sign: when OLAP queries interfere with OLTP processing

• For SQL Server, that typically means high levels of locking waits and blocking

3/19/2013 51

Page 52: Size does matter

First Step: Admit You Have a Problem!

• First sign: when OLAP queries interfere with OLTP processing

• For SQL Server, that typically means high levels of locking waits and blocking

• Another sign: plan cache bloat

3/19/2013 52

Page 53: Size does matter

Solutions

• Find duplicate/unused/misused indexes

• Compression

• Filtered indexes

• Archiving/partitioning

• Verify datatypes are correct

• Update code

• Update architecture

3/19/2013 53

Page 54: Size does matter

Index Maintenance

• Want to reduce the extra I/O

– Remove duplicate indexes

– Remove unused/misused indexes

3/19/2013 54

Page 55: Size does matter

Index Maintenance

• Want to reduce the extra I/O

– Remove duplicate indexes

– Remove unused/misused indexes

• Test thoroughly!

– Examine plan cache to find if indexes are used

– Plan cache resets on service restart (or manual)

3/19/2013 55

Page 56: Size does matter
Page 57: Size does matter

Solutions

• Find duplicate/unused/misused indexes

• Compression

• Filtered indexes

• Archiving/partitioning

• Verify datatypes are correct

• Update code

• Update architecture

3/19/2013 57

Page 58: Size does matter

Compression

• Native compression has two steps

– Row compression

– Page compression

– Each table/index requires compression to be enabled

3/19/2013 58

Page 59: Size does matter

ROW/PAGE COMPRESSIONDEMO

3/19/2013 59

Page 60: Size does matter

Compression

• Native compression has two steps

– Row compression

– Page compression

– Each table/index requires compression to be enabled

• ColumnStore

– Dictionary compression

– Segments and batches

3/19/2013 60

Page 61: Size does matter

Solutions

• Find duplicate/unused/misused indexes

• Compression

• Filtered indexes

• Archiving/partitioning

• Verify datatypes are correct

• Update code

• Update architecture

3/19/2013 61

Page 62: Size does matter

Filtered Indexes

• Look like regular indexes

– Contain a WHERE clause

• Smaller footprint

• Less logical I/O

3/19/2013 62

Page 63: Size does matter

FILTERED INDEXESDEMO

3/19/2013 63

Page 64: Size does matter

Solutions

• Find duplicate/unused/misused indexes

• Compression

• Filtered indexes

• Archiving/partitioning

• Verify datatypes are correct

• Update code

• Update architecture

3/19/2013 64

Page 65: Size does matter

Partitioning

• Can be viable alternative to proper archiving

3/19/2013 65

Page 66: Size does matter

Partitioning

• Can be viable alternative to proper archiving

• Queries hit only the partitions necessary

– Better than full table scans

3/19/2013 66

Page 67: Size does matter

Partitioning

• Can be viable alternative to proper archiving

• Queries hit only the partitions necessary

– Better than full table scans

• Easier to maintain

– Not necessarily easier to administer!

3/19/2013 67

Page 68: Size does matter

Partitioning

• Can be viable alternative to proper archiving

• Queries hit only the partitions necessary

– Better than full table scans

• Easier to maintain

– Not necessarily easier to administer!

3/19/2013 68

Page 69: Size does matter

Solutions

• Find duplicate/unused/misused indexes

• Compression

• Filtered indexes

• Archiving/partitioning

• Verify datatypes are correct

• Update code

• Update architecture

3/19/2013 69

Page 70: Size does matter

Verify Datatypes

• Compare datatype definition to actual data

– i.e., BIGINT defined, but only SMALLINT used

3/19/2013 70

Page 71: Size does matter

Verify Datatypes

• Compare datatype definition to actual data

– i.e., BIGINT defined, but only SMALLINT used

• You can estimate savings

– Space as well as LIO

3/19/2013 71

Page 72: Size does matter

Verify Datatypes

• Compare datatype definition to actual data

– i.e., BIGINT defined, but only SMALLINT used

• You can estimate savings

– Space as well as LIO

• Can be intrusive to alter

– Especially if PK/FK defined

3/19/2013 72

Page 73: Size does matter

Verify Datatypes

• Compare datatype definition to actual data

– i.e., BIGINT defined, but only SMALLINT used

• You can estimate savings

– Space as well as LIO

• Can be intrusive to alter

– Especially if PK/FK defined

• Periodically check to make sure you are not running out!

3/19/2013 73

Page 74: Size does matter

VERIFY DATATYPESDEMO

3/19/2013 74

Page 75: Size does matter

Solutions

• Find duplicate/unused/misused indexes

• Compression

• Filtered indexes

• Archiving/partitioning

• Verify datatypes are correct

• Update code

• Update architecture

3/19/2013 75

Page 76: Size does matter

Update Code/Architecture

• If you are seeing the signs, then you may consider updating code

3/19/2013 76

Page 77: Size does matter

Update Code/Architecture

• If you are seeing the signs, then you may consider updating code

• Some frameworks are not optimal

– ADO.NET 3.5

– LINQ to SQL

– EMF

3/19/2013 77

Page 78: Size does matter

Update Code/Architecture

• If you are seeing the signs, then you may consider updating code

• Some frameworks are not optimal

– ADO.NET 3.5

– LINQ to SQL

– EMF

• Scale out architecture

– Create real reporting solution

– AlwaysOn

3/19/2013 78

Page 79: Size does matter

10 Steps For Right Sizing

1. Understand the storage requirements for every datatype you consider

3/19/2013 79

Page 80: Size does matter

10 Steps For Right Sizing

1. Understand the storage requirements for every datatype you consider

2. Review all design decisions based on the shape of the data – where it is now and where it is likely to be later.

3/19/2013 80

Page 81: Size does matter

10 Steps For Right Sizing

1. Understand the storage requirements for every datatype you consider

2. Review all design decisions based on the shape of the data – where it is now and where it is likely to be later.

3. Set datatypes based on business requirements, not tool defaults

3/19/2013 81

Page 82: Size does matter

10 Steps For Right Sizing

4. Measure and monitor fit of the data to its datatypes regularly

3/19/2013 82

Page 83: Size does matter

10 Steps For Right Sizing

4. Measure and monitor fit of the data to its datatypes regularly

5. Review each index creation request to see if it is duplicate

3/19/2013 83

Page 84: Size does matter

10 Steps For Right Sizing

4. Measure and monitor fit of the data to its datatypes regularly

5. Review each index creation request to see if it is duplicate

6. Measure for unused and duplicate indexes regularly

3/19/2013 84

Page 85: Size does matter

10 Steps For Right Sizing

7. Review new stored procedures to verify parameters are matching

3/19/2013 85

Page 86: Size does matter

10 Steps For Right Sizing

7. Review new stored procedures to verify parameters are matching

8. Find longest running and most often used queries

3/19/2013 86

Page 87: Size does matter

10 Steps For Right Sizing

7. Review new stored procedures to verify parameters are matching

8. Find longest running and most often used queries

9. Look for implicit conversions in your plan cache

3/19/2013 87

Page 88: Size does matter

10 Steps For Right Sizing

7. Review new stored procedures to verify parameters are matching

8. Find longest running and most often used queries

9. Look for implicit conversions in your plan cache

10.Remember that size matters

3/19/2013 88

Page 89: Size does matter

Summary

• What’s the problem?

• Why datatypes matter

• Solution options

3/19/2013 89

Page 90: Size does matter

For More Information

• http://tinyurl.com/sql-datatypes

• http://tinyurl.com/imp-cols-in-plan-cache

• http://tinyurl.com/data-access-perf

• http://tinyurl.com/row-compression

3/19/2013 90