Data Types Do Matter Start local instance of SQL Start ZoomIt

Slides:



Advertisements
Similar presentations
Project Management Database and SQL Server Katmai New Features Qingsong Yao
Advertisements

Module 4: Creating Data Types and Tables. Overview Creating Data Types Creating Tables Generating Column Values Generating Scripts.
Phonegap Bridge – File System CIS 136 Building Mobile Apps 1.
Copyright ©2014 Pearson Education, Inc. Chapter 6 Physical Design Chapter6.1.
Database Fundamentals
DATABASES AND SQL. Introduction Relation: Relation means table(data is arranged in rows and columns) Domain : A domain is a pool of values appearing in.
Introduction to SQL  SQL or sequel  It is a standardised language with thousands of pages in the standard  It can be in database system through GUI,
Working with Data Types February 7, 2015 John Deardurff Website:
Module 2 Working with Data Types. Module Overview Using Data Types Working with Character Data Converting Data Types Working with Specialized Data Types.
Overview of SQL Server Alka Arora.
Module 4: Data Objects. Overview Tables are the main objects that store data Indexes, views, stored programs and other objects are the support structures.
Said Salomon Unitrin Direct Insurance T-SQL Date and Time Functions Said Salomon.
Chapter 7 SQL HUANG XUEHUA. SQL SQL server2005 introduction Install components  management studio.
Using New Data Types in 2008 Andrew Couch UK Access User Group ASC associates
SQL Data Definition Language (DDL) Using Microsoft SQL Server 1SDL Data Definition Language (DDL)
IT:Network:Applications.  “Business runs on databases…” ◦ Understatement!  Requirements  Installation  Creating Databases  SIMPLE query ◦ Just enough.
11 3 / 12 CHAPTER Databases MIS105 Lec15 Irfan Ahmed Ilyas.
Module 3: Creating Data Types and Tables. Overview Working with Data Types Working with Tables Generating Column Values Generating Scripts.
Christopher M. Pascucci.NET Programming: Databases & ADO.NET.
Data Types Lesson 4. Skills Matrix Table A table stores your data. Tables are relational in that they are organized as rows and columns (a matrix). Each.
Advanced Web 2012 Lecture 3 Sean Costain What is a Database? Sean Costain 2012 A database is a structured way of dealing with structured information.
SQL Server 2005 Implementation and Maintenance Chapter 3: Tables and Views.
Sql DDL queries CS 260 Database Systems.
IMS 4212: Data Modeling—Attributes and Domains 1 Dr. Lawrence West, Management Dept., University of Central Florida Attributes and Domains.
Best Practices in SQL Programming. Do not use irrelevant datatype  VARCHAR instead of DATETIME  CHAR(N) instead of VARCHAR(N)  etc.
INTRODUCING SQL SERVER 2012 COLUMNSTORE INDEXES Exploring and Managing SQL Server 2012 Database Engine Improvements.
SQL SERVER DAYS 2011 Indexing Internals Denny Cherry twitter.com/mrdenny.
Creating E/R Diagrams with SQL Server Management Studio, Writing SQL Queries D0ncho Minkov Telerik School Academy schoolacademy.telerik.com Technical Trainer.
14 New T-SQL Functions By Sam Nasr, MCAD, MCTS. MVP Nasr Information Systems February 8, 2014.
What is your Character Data Type? March 5, 2016 John Deardurff Website:
Doing fast! Optimizing Query performance with ColumnStore Indexes in SQL Server 2012 Margarita Naumova | SQL Master Academy.
Standards and Conventions
14 New T-SQL Functions By Sam Nasr, MCAD, MCT, MCTS NIS
Silent Killers Lurking in Your Schema Mickey Stuewe
Querying with Transact-SQL
Creating Database Objects
Moving Your SQL Database to SQL Azure And Setting up Data Sync
DB storage architectures: Rows, Columns, LSM trees
Tuning Transact-SQL Queries
Managing Tables, Data Integrity, Constraints by Adrienne Watt
Database Management System
Module 2: Creating Data Types and Tables
Accidental DBA Developer Edition
Lecture 6 Data Model Design (continued)
Data Definition and Data Types
Lesson 7 Managing Data Creating a database with Web Matrix.
Attributes and Domains
Ouch! Our Data Type Choices Did THAT?
SQL Server 2016 JSON Support FOR Data Warehousing
SQL Implementation & Administration
Working with SQL Server 2016 Data Types
14 T-SQL Functions You May Not Know
Migrating a Disk-based Table to a Memory-optimized one in SQL Server
Proper DataType Usage = Guaranteed Better Performance and Accuracy
SQL Server TVP Performance Gotcha
What is your Character Data Type?
Working with Data Types
Database systems Lecture 2 – Data Types
Shaving of Microseconds
A JSON’s Journey through SQL Server
PT2520 Unit 5: Physical Design
Adding Lightness Better Performance through Compression
14 T-SQL Functions You May Not Know
Attributes and Domains
Creating Database Objects
DB storage architectures: Rows and Columns
Introduction to SQL Server and the Structure Query Language
Working with SQL Server 2016 Data Types
Presentation transcript:

Data Types Do Matter Start local instance of SQL Start ZoomIt Adjust font size in SSMS - PresenterOn Open all queries 1 – Garbage in/Garbage Out 2 – Storage – Just Numbers 3 – Storage – Just Characters 4 – Storage – A Date’s just a date 5 – Performance-SARGability 5 – Performance-PageIO Angela Henry, VP of Reporting & Analytics, SQL Data Partners

OregonSQL User Group www.OregonSQL.org Meetings Every 2nd Wednesday, 6:00PM – 9:00PM 1515 SW 5th Ave, Suite 900, Portland

Support Our Sponsors

Angela Henry SQL Data Partners Certifications/Awards Community Leader MCSE: Business Intelligence Data Platform MVP Community Leader Angela Henry She is also the local group leader for the PASS Triad SQL User Group in Greensboro, NC. SQL Data Partners Ask about audience make up – App Devs, DBAs, t-sql devs, etc. Avid Swimmer /in/angelahenrydba In her spare time you can probably find her in or at the pool, she's an avid US Masters Swimmer, Coach and Instructor. @SQLSwimmer SQLSwimmer.Wordpress.com

Overview Data Quality Storage Performance Data Quality  Garbage In/Garbage Out  Incorrect Query Results

SQL Server Data Types user-defined data types real text sql_variant decimal image xml money timestamp datetimeoffset smallmoney uniqueidentifier datetime2 bigint nvarchar (including nvarchar(max)) datetime int nchar smalldatetime smallint varchar (including varchar(max)) date tinyint char time bit varbinary (including varbinary(max)) float ntext binary SQL Server has 35 data types – No we are NOT going to talk about all of them!

Data Quality Character Data Types - Everyone’s favorite For Temporal Data (Dates/Times) For Numbers

Demo Data Quality

Garbage In = Garbage Out Data Quality Comparison Operators Sorting Garbage In = Garbage Out

Storage They’re just numbers, right?! It’s just string data, who cares? A date’s a date

Just Numbers Data type Range Storage bigint -2^63 to 2^63-1 -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (6 commas!!) 8 Bytes int -2^31 to 2^31-1 (-2,147,483,648) to (2,147,483,647) 4 Bytes smallint -2^15 to 2^15-1 (-32,768) to (32,767) 2 Bytes tinyint 0 to 255 1 Byte

Demo Just Numbers

Not Just Numbers Data type Range Storage Impact bigint -2^63 to 2^63-1 -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (6 commas!!) 8 Bytes 200% > int 400% > smallint 800% > tinyint int -2^31 to 2^31-1 (-2,147,483,648) to (2,147,483,647) 4 Bytes 200% > smallint 400% > tinyint smallint -2^15 to 2^15-1 (-32,768) to (32,767) 2 Bytes 200% > tinyint tinyint 0 to 255 1 Byte

Just String Data Data Type Range Storage Char Fixed Length 1 to 8000 Length of data Varchar Variable Length 1 to 8000 or MAX  2^31 - 1 Bytes (2GB) + 2 Bytes nChar Fixed Length 1 to 4000 2 * Length of data nVarchar Variable Length 1 to 4000 or MAX  2^31 – 1 Bytes (2GB)

Demo Just String Data

Not Just String Data Data Type Range Storage Impact Char Fixed Length 1 to 8000 Length of data 200% > varchar with same lengths Varchar Variable Length 1 to 8000 or MAX --> 2^31 - 1 Bytes (2GB) + 2 Bytes nChar Fixed Length 1 to 4000 2 * Length of data 200% > nvarchar with same lengths nVarchar Variable Length 1 to 4000 Or MAX  2^31 – Bytes (2GB)

A Date’s A Date Data Type Range Storage Accuracy Datetime2 0001-01-01 thru 9999-12-31 6-8 Bytes 100 Nanoseconds Datetime 1753-01-01 thru 9999-12-31 8 Bytes Milliseconds rounded to .000, .003 or .007 SmallDatetime 1900-01-01 thru 2079-06-06 4 Bytes 1 Minute Date 3 Bytes 1 Day The only data type that is actually just a date is Date. All the others have a Time piece associated with it. Datetime2 was added in SQL Server 2008

Demo A Date’s A Date

Not All Dates Are Created Equal Data Type Range Storage Accuracy Impact Datetime2 0001-01-01 thru 9999-12-31 6-8 Bytes 100 Nanoseconds Up to 230% > Date Datetime 1753-01-01 thru 9999-12-31 8 Bytes Milliseconds rounded to .000, .003 or .007 230% > Date SmallDatetime 1900-01-01 thru 2079-06-06 4 Bytes 1 Minute 30% > Date Date 3 Bytes 1 Day

And The Winner Is.. ~ 10MM Rows Data Types Data Only Data With Index Date  Datetime2 50MB 100MB TinyInt  BigInt 70MB 140MB Varchar(255)  Char(255) 2.5GB 5GB

Performance Execution Plans I/O

Execution Plans SARGability - 'SARG' stands for Search ARGument, and it means that the predicate can be executed using an index seek Predicate is <expression> <operator> <expression> DateAdd(day, 1, myDateCol) = @myDate is NOT SARGable myDateCol = @myDateVariable IS SARGable when they are the same data type –Gail Shaw, @SQLInTheWind SARGable means you want to be able to use an index which can only happen if your column is by itself on one side of the operator, e.g., no functions are used

Conversion Precedence 1 user-defined data types (highest) 11 real 21 text 2 sql_variant 12 decimal 22 image 3 xml 13 money 23 timestamp 4 datetimeoffset 14 smallmoney 24 uniqueidentifier 5 datetime2 15 bigint 25 nvarchar (including nvarchar(max) ) 6 datetime 16 int 26 nchar 7 smalldatetime 17 smallint 27 varchar (including varchar(max) ) 8 date 18 tinyint 28 char 9 time 19 bit 29 varbinary (including varbinary(max) ) 10 float 20 ntext 30 binary (lowest) When an operator combines two expressions of different data types, the rules for data type precedence specify that the data type with the lower precedence is converted to the data type with the higher precedence. Makes sense since a smallint may not be able to be converted to a tinyint, it may be > 255 or < 0. Straight out of Books Online

Performance - Execution Plans Demo Performance - Execution Plans

Fat Tables Original Table Fat Table CREATE TABLE Demo.SuppliersOriginal( SupplierID int NOT NULL, SupplierName nvarchar(100) NOT NULL, SupplierCategoryID int NOT NULL, PrimaryContactPersonID int NOT NULL, AlternateContactPersonID int NOT NULL, DeliveryMethodID int NULL, DeliveryCityID int NOT NULL, PostalCityID int NOT NULL, SupplierReference nvarchar(20) NULL, BankAccountName nvarchar(50) NULL, BankAccountBranch nvarchar(50) NULL, BankAccountCode nvarchar(20) NULL, BankAccountNumber nvarchar(20) NULL, BankInternationalCode nvarchar(20) NULL, PaymentDays int NOT NULL, InternalComments nvarchar(max) NULL, PhoneNumber nvarchar(20) NOT NULL, FaxNumber nvarchar(20) NOT NULL, WebsiteURL nvarchar(256) NOT NULL, DeliveryAddressLine1 nvarchar(60) NOT NULL, DeliveryAddressLine2 nvarchar(60) NULL, DeliveryPostalCode nvarchar(10) NOT NULL, PostalAddressLine1 nvarchar(60) NOT NULL, PostalAddressLine2 nvarchar(60) NULL, PostalPostalCode nvarchar(10) NOT NULL, LastEditedBy int NOT NULL) CREATE TABLE Demo.SuppliersFat( SupplierID int NOT NULL, SupplierName char(255) NOT NULL, SupplierCategoryID int NOT NULL, PrimaryContactPersonID int NOT NULL, AlternateContactPersonID int NOT NULL, DeliveryMethodID int NULL, DeliveryCityID int NOT NULL, PostalCityID int NOT NULL, SupplierReference char(255) NULL, BankAccountName char(255) NULL, BankAccountBranch char(255) NULL, BankAccountCode char(255) NULL, BankAccountNumber char(255) NULL, BankInternationalCode char(255) NULL, PaymentDays int NOT NULL, InternalComments char(2000) NULL, PhoneNumber char(25) NOT NULL, FaxNumber char(255) NOT NULL, WebsiteURL char(256) NOT NULL, DeliveryAddressLine1 char(255) NOT NULL, DeliveryAddressLine2 char(255) NULL, DeliveryPostalCode char(255) NOT NULL, PostalAddressLine1 char(255) NOT NULL, PostalAddressLine2 char(255) NULL, PostalPostalCode char(255) NOT NULL, LastEditedBy int NOT NULL) Original table takes an approx. max of 2,000 bytes Fat tables takes an approx. max of 5,887 bytes

Demo Performance - IO

Impact on Performance Implicit Data Type Conversions Table Scans/Index Scans More Pages Mean More IO Just because you don’t do the conversion, doesn’t mean it doesn’t happen! No one wants table scans! Page Splits take time and are resource intensive operations! SSDs have helped eliminate some of this, but they are not a silver bullet!

References How Can Data-Type Choice Effect Performance by Paul Randal What is a SARGible Predicate by Gail Shaw Varchar(max) forum Thread SQL Server Documentation

Support Our Sponsors

@SQLSwimmer ahenry@sqldatapartners.com