Download presentation
Presentation is loading. Please wait.
Published byConrad Bruce Modified over 7 years ago
1
Silent Killers Lurking in Your Schema Mickey Stuewe
Sr. Database Developer Microsoft Geek Red Gate Nerd
2
Your Background DBA Database Developer Programmer Manager
Just Checking Things Out
3
Objectives Data Types Smart Keys Naming Named Value Pairs Nullability
Constraints Indexes Hard Coded Lists Minimal Auditing
4
Data types can be data hogs Data Types
Affects range of possible values Affects storage Affects behavior of functions Affects choices of indexes Affects quality of your data Data types can be data hogs
5
Data Types TINYINT SMALLINT INT BIGINT Size 1 Byte 2 Bytes 4 Bytes
Range 0 to 255 -32,768 to 32,768 -2,147,483,648 to 2,147,483,648 -2^63 to 2^63
6
Data Types TINYINT SMALLINT INT BIGINT Size 1 Byte 2 Bytes 4 Bytes
Range 0 to 255 -32,768 to 32,768 -2,147,483,648 to 2,147,483,648 -2^63 to 2^63 Bad Use Year PK of trans data Age Number of dependents PK of “click data” Birth month PK for a lookup table
7
Data Types TINYINT SMALLINT INT BIGINT Size 1 Byte 2 Bytes 4 Bytes
Range 0 to 255 -32,768 to 32,768 -2,147,483,648 to 2,147,483,648 -2^63 to 2^63 Bad Use Year PK of trans data Age Number of dependents PK of “click data” Birth month PK for a lookup table Good Use
8
Data Types CHAR VARCHAR NVARCHAR NVARCHAR (MAX) Size
1 byte per char. (fixed length) 2 bytes + 1 byte per char. 2 bytes + 2 bytes per char. Up to 2 GB Range 1 to 8000 char. 1 to 4000 char. 1 char to 2GB
9
Data Types CHAR VARCHAR NVARCHAR NVARCHAR (MAX) Size
1 byte per char. (fixed length) 2 b + 1 byte per char. 2 bytes + 2 byte per char. Up to 2 GB Range 1 to 8000 char. 1 to 4000 char. 1 char to 2GB Bad Use Order Notes Middle Name Gender (F/M) Middle Initial State Capitals Lookup Values Almost Everything
10
Data Types CHAR VARCHAR NVARCHAR NVARCHAR (MAX) Size
1 byte per char. (fixed length) 2 b + 1 byte per char. 2 bytes + 2 byte per char. Up to 2 GB Range 1 to 8000 char. 1 to 4000 char. 1 char to 2GB Bad Use Order Notes Middle Name Gender (F/M) Middle Initial State Capitals Lookup Values Almost Everything Good Use Capitals of the world suser_name() Notes that can go over 4000 characters
11
Data Types No Indexes on NVARCHAR (MAX)…EVER CHAR VARCHAR NVARCHAR
Size 1 byte per char. (fixed length) 2 b + 1 byte per char. 2 bytes + 2 byte per char. Up to 2 GB Range 1 to 8000 char. 1 to 4000 char. 1 char to 2GB Bad Use Order Notes Middle Name Gender (F/M) Middle Initial State Capitals Lookup Values Almost Everything Good Use Capitals of the world suser_name() Notes that can go over 4000 characters Max str. fields in index <= 900 Char <= (900) – (2 x no.fields) <= (450) – (2 x no.fields) Can’t touch this.
12
Data Types Hint: DateTime2 is the best thing since sliced bread
Homework: Compare date data types
13
Smart Keys MED15 – 000456 - F Smart Keys Are Not So Smart Pro
Easy to decipher Multiple pieces of information in one column Con Fragmented indexes Can get out of hand Breaks Codd’s rules MED15 – F
14
Bad Naming AEC AER IOU FICO Return Review Split Id Field1 Sheet!1
It Shouldn’t Be Aa Box Of Chocolates Generic columns named Column1…n Abbreviations Reserved Words Default Names Bit columns: Active vs isActive Spelling AEC AER IOU FICO Return Review Split Id Field1 Sheet!1 Sheet1$ YourNameHere From User Create UserColumn01 UserColumn02 … UserColumn08 AEC Average Efficiency Cost AER Annual Equivalent Return AER Annual Equivalent Rate Return Review
15
Named Value Pairs Long Distance Dating Pro Easy to insert
Easier for applications Con VARCHAR/NVARCHAR only Hard for reporting Lack of Foreign Keys Breaks Codd’s rules Maintenance Lack of Constraints Possible Dirty Data Rule 1: The information rule: All information in a relational database (including table and column names) is represented in only one way, namely as a value in a table.
16
Nullability To NULL Or Not To NULL, That Is The Question
Has a purpose – represents “no data” Column of type BIT should not be nullable Be careful with ISNULL function Rule 3: Systematic treatment of null values: The DBMS must allow each field to remain null (or empty). Specifically, it must support a representation of "missing information and inapplicable information" that is systematic, distinct from all regular values (for example, "distinct from zero or any other number", in the case of numeric values), and independent of data type. It is also implied that such representations must be manipulated by the DBMS in a systematic way. Rule 3: Systematic treatment of null values: The DBMS must allow each field to remain null (or empty). Specifically, it must support a representation of "missing information and inapplicable information" that issystematic, distinct from all regular values (for example, "distinct from zero or any other number", in the case of numeric values), and independent of data type. It is also implied that such representations must be manipulated by the DBMS in a systematic way.
17
Constraints Constrain the data, she’s goanna blow!
Protect against bad data Check Constraints Foreign Key Constraints Unique Constraints Check end date is after begin date Check a month is a month Check birthdate is not in the future
18
Indexes Resource: JasonStrate.com
She can’t take any more. She’s maxed at 900 bytes! Not all Primary Keys should be clustered Indexes Add Indexes to heavily used columns Have a good balance of indexes Resource: JasonStrate.com Surrogate primary keys vs natural keys How is the table used? Does it have more reads than writes? Is it meant for reporting? Is the data retrieved in large ranges or one row at a time?
19
Hard Coded Lists Danger Will Robinson!
Unable to change them efficiently Unable to validate against them outside of application or query Hard to maintain over time No referential integrity Dirty data will happen
20
Minimal Auditing CreatedBy CreatedOn UpdatedBy UpdatedOn
Who’s on first. When’s on second. Can provide quick insight as to when data was changed Can provide quick insight as to who changed the data Can provide insight to bugs Can be used by ETL processes Can be used for partitioning CreatedBy CreatedOn UpdatedBy UpdatedOn
21
Show Me The Demo
22
Objectives Data Types Smart Keys Naming Named Value Pairs Nullability
Constraints Indexes Hard Coded Lists Minimal Auditing
23
Questions?
24
Presentation Contact Online www.MickeyStuewe.com/Resources
Online MickeyStuewe.com Bigpass.sqlpass.org YouTube.com: Mickey Stuewe
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.