0% found this document useful (0 votes)
36 views27 pages

Proper Datatype Usage Guaranteed Better Performance and Accuracy

This document contains a presentation by Vern Rabe on proper data type usage in SQL Server. It discusses the importance of using the correct data types for performance and accuracy. It provides details on the various data type categories in SQL Server including numeric, character, temporal, binary, and other types. It highlights issues to watch out for such as implicit conversions, precision of decimal operations, and proper use of functions. The presentation aims to help users choose the right data types while avoiding deprecated types and implicit conversions that can hurt performance or lead to unexpected results.

Uploaded by

B4H TV
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views27 pages

Proper Datatype Usage Guaranteed Better Performance and Accuracy

This document contains a presentation by Vern Rabe on proper data type usage in SQL Server. It discusses the importance of using the correct data types for performance and accuracy. It provides details on the various data type categories in SQL Server including numeric, character, temporal, binary, and other types. It highlights issues to watch out for such as implicit conversions, precision of decimal operations, and proper use of functions. The presentation aims to help users choose the right data types while avoiding deprecated types and implicit conversions that can hurt performance or lead to unexpected results.

Uploaded by

B4H TV
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Vern Rabe

RabeData, LLC

Proper DataType Usage =


Guaranteed Better
Performance and Accuracy
#1000
Oregon
Are you staying up to speed
with Power Platform?
Check out Microsoft’s “Dashboard in a Day”, “App in a Day” and other
brand new “in a Day” trainings

Hosted every week by Microsoft Partners around the world, including


Marquam

Ask your preferred local Microsoft Partner or sign up at marquam.com/events


or [email protected]
Please provide feedback on the
presentation
https://www.surveymonkey.com/r/PSKT9VC
Vern Rabe

Portland, OR independent consultant, trainer


MCTA, MCDBA, MCSE, MCITP
Leader Oregon PASS chapter
Working with/focusing on SQL Server since 1993,
starting with SQL Server 4.21a
My first PC had a 10MB hard drive
What We’ll Cover

Why do we care?
Performance, accuracy
Data Type by Data type
When to use which
Functions and Nuggets
What to use, what not to use, some workarounds
Why Important?

Accuracy •

Performance •
SARGable (Implicit Conversion) •
Implicit Conversion
Data Type Precedence

1. user-defined data types (highest) 16.int


2. sql_variant 17.smallint
3. xml 18.tinyint
4. datetimeoffset 19.bit
5. datetime2 20.ntext
6. datetime 21.text
7. smalldatetime 22.image
8. date 23.rownumber
9. time 24.uniqueidentifier
10.float 25.nvarchar
11.real 26.nchar
27.varchar
12.decimal
28.char
13.money
29.varbinary
14.smallmoney
30.binary (lowest)
15.bigint
Data Type Categories

Numeric
Character
Temporal
Binary
Other
Numeric Category

bit : 1/8 – 1 byte : 0 or 1


tinyint : 1 byte : 0 to 255
smallint : 2 bytes : -32,768 to 32,767
int : 4 bytes : -2,147,483,648 to 2,147,483,647
bigint : 8 bytes : -9,223,372,036,854,775,808 to
9,223,372,036,854,775,808
decimal, numeric : 5 – 17 bytes : maximum precision of 38
smallmoney : 4 bytes : -214,748.3648 to 214,748.3647
money : 8 bytes : -922,337,203,685,477.5808 to
922,337,203,685,477.5807
float : 4 – 8 bytes : approx ± 2 * 10 ±308
real : 4 bytes : approx ± 2 * 10 ±38
Numeric Data Type Issues

Float/real are imprecise/approximate •


Money has rounding surprises •
Precision and Scale of
Decimal Operation Results

Operation Result precision Result scale *


max(s1, s2) + max(p1-
e1 + e2 max(s1, s2)
s1, p2-s2) + 1
max(s1, s2) + max(p1-
e1 - e2 max(s1, s2)
s1, p2-s2) + 1
e1 * e2 p1 + p2 + 1 s1 + s2
p1 - s1 + s2 + max(6, s1
e1 / e2 max(6, s1 + p2 + 1)
+ p2 + 1)
e1 { UNION | EXCEPT | max(s1, s2) + max(p1-
max(s1, s2)
INTERSECT } e2 s1, p2-s2)
min(p1-s1, p2 -s2) +
e1 % e2 max(s1, s2)
max( s1,s2 )
* The result precision and scale have an absolute maximum of 38. When
a result precision is greater than 38, the corresponding scale is reduced
to prevent the integral part of a result from being truncated.
Numeric Data Type Issues

Float and Real are imprecise/approximate •


Money has rounding surprises •
More rounding surprises •
Character Category

char(N) : N bytes : 8000 characters


nchar(N) : 2 * N bytes : 4000 characters
varchar(N) : data length + 2 bytes : 8000 characters
varchar(MAX) : data length + 2 bytes : ≈ 2,000,000,000
characters
nvarchar(N) : 2 * data length + 2 bytes : 4000 characters
nvarchar(MAX) : 2 * data length + 2 bytes :
≈1,000,000,000 characters)
text : data length : ≈ 2,000,000,000 characters
ntext : 2 * data length : ≈ 1,000,000,000 characters
Character Category

char(N) : N bytes : 8000 characters


nchar(N) : 2 * N bytes : 4000 characters
varchar(N) : data length + 2 bytes : 8000 characters
varchar(MAX) : data length + 2 bytes : ≈ 2,000,000,000
characters
nvarchar(N) : 2 * data length + 2 bytes : 4000 characters
nvarchar(MAX) : 2 * data length + 2 bytes :
≈1,000,000,000 characters)
text : data length : ≈ 2,000,000,000 characters
ntext : 2 * data length : ≈ 1,000,000,000 characters
Size Matters

(var)char(small) vs. (var)char(big) – difference? •


Domain integrity
(var)char (without specifying size) •
(var)char(n) - When n is not specified in a data definition
or variable declaration statement, the default length is 1.
When n is not specified with the CAST function, the
default length is 30
varchar(n) vs. char(n)
varchar(n) vs. varchar(MAX)
Domain integrity
Temporal Category

date : 3 bytes : 0001-01-01 to 9999-12-31


time : 5 bytes : 00:00:00.0000000 to 23:59:59.9999999
datetime2 : 6 – 8 bytes : 0001-01-01 to 9999-12-31,
100ns precision
datetimeoffset : 10 bytes : 0001-01-01 to 9999-12-31,
100ns precision
smalldatetime : 4 bytes : 1900-01-01 to 2079-06-06,
precision of 1 minute
datetime : 8 bytes : 1753-01-01 to 9999-12-31,
precision of 3⅓ millisecond (.000, .003, .007, .010 sec)
Temporal Category

date : 3 bytes : 0001-01-01 to 9999-12-31


time : 5 bytes : 00:00:00.0000000 to 23:59:59.9999999
datetime2 : 6 – 8 bytes : 0001-01-01 to 9999-12-31,
100ns precision
datetimeoffset : 10 bytes : 0001-01-01 to 9999-12-31,
100ns precision
smalldatetime : 4 bytes : 1900-01-01 to 2079-06-06,
precision of 1 minute
datetime : 8 bytes : 1753-01-01 to 9999-12-31,
precision of 3⅓ millisecond (.000, .003, .007, .010 sec)
Temporal Examples

Date literals •
‘1/2/11’?
‘01-02-2011’?
‘2011-01-02’?
Using BETWEEN to identify day •
Adding days •
What date is 0 or “” (empty string)? •
Eliminating time •
Binary Category

binary(N) : N bytes : 8000 bytes


varbinary(N) : data length + 2 bytes : 8000 bytes
varbinary(MAX) : data length + 2 bytes : ≈
2,000,000,000 bytes
image : ≈ 2,000,000,000 bytes
Binary Category

binary(N) : N bytes : 8000 bytes


varbinary(N) : data length + 2 bytes : 8000 bytes
varbinary(MAX) : data length + 2 bytes : ≈
2,000,000,000 bytes
image : ≈ 2,000,000,000 bytes
Other Category

cursor
timestamp/rowversion : 8 bytes
hierarchyid
uniqueidentifier : 16 bytes
sql_variant
xml
Spacial Geometry Types
Spacial Geography Types
table
Other Category

cursor
timestamp/rowversion : 8 bytes
hierarchyid
uniqueidentifier : 16 bytes
sql_variant
xml
table
Functions
ISNUMERIC •
TRY_CONVERT / TRY_CAST •
Watch out for empty strings to temporal
ISDATE •
Doesn’t cover entire date range
Doesn’t support full resolution of datetime2
ISNULL vs. COALESCE •
ISNULL returns data type of first argument
COALESCE returns data type of highest precedence
Temporal Functions •
CURRENT_TIMESTAMP
GETDATE()
GETUTCDATE()
SYSDATETIME()
SYSUTCDATETIME()
Summary
Pick data types with consideration
Size matters
Domain constraint
Avoid deprecated data types
Beware of implicit conversions
Not SARGable
Unexpected results
Use functions with caution
Please provide feedback on the
presentation
https://www.surveymonkey.com/r/PSKT9VC
Thanks. Questions?

Vern
Rabe

[email protected] @VernRabe

You might also like