Download presentation
Presentation is loading. Please wait.
Published byΤάνις Αντωνιάδης Modified over 6 years ago
1
Proper DataType Usage = Guaranteed Better Performance and Accuracy
Vern Rabe RabeData, LLC Proper DataType Usage = Guaranteed Better Performance and Accuracy
2
Vern Rabe Portland, OR independent consultant, trainer MCTA, MCDBA, MCSE, MCITP Co-VP 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
3
What We’ll Cover Why do we care? Data Type by Data type
Performance, accuracy Data Type by Data type When to use which Functions and Nuggets What to use, what not to use, some workarounds
4
Why Important? Accuracy • Performance •
SARGable (Implicit Conversion) •
5
Implicit Conversion Data Type Precedence
user-defined data types (highest) sql_variant xml datetimeoffset datetime2 datetime smalldatetime date time float real decimal money smallmoney bigint int smallint tinyint bit ntext text image rownumber uniqueidentifier nvarchar nchar varchar char varbinary binary (lowest)
6
Data Type Categories Numeric Character Temporal Binary Other
7
Numeric Category bit : 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, to 214, money : 8 bytes : -922,337,203,685, to 922,337,203,685, float : 4 – 8 bytes : approx ± 2 * 10 ±308 real : 4 bytes : approx ± 2 * 10 ±38
8
Numeric Data Type Issues
Float/real are imprecise/approximate • Money has rounding surprises •
9
Precision and Scale of Decimal Operation Results
Result precision Result scale * e1 + e2 max(s1, s2) + max(p1-s1, p2-s2) + 1 max(s1, s2) e1 - e2 e1 * e2 p1 + p2 + 1 s1 + s2 e1 / e2 p1 - s1 + s2 + max(6, s1 + p2 + 1) max(6, s1 + p2 + 1) e1 { UNION | EXCEPT | INTERSECT } e2 max(s1, s2) + max(p1-s1, p2-s2) e1 % e2 min(p1-s1, p2 -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.
10
Numeric Data Type Issues
Float and Real are imprecise/approximate • Money has rounding surprises • More rounding surprises •
11
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
12
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
13
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) Performance Rob Garrison blog –
14
Temporal Category date : 3 bytes : to time : 5 bytes : 00:00: to 23:59: datetime2 : 6 – 8 bytes : to , 100ns precision datetimeoffset : 10 bytes : to , 100ns precision smalldatetime : 4 bytes : to , precision of 1 minute datetime : 8 bytes : to , precision of 3⅓ millisecond (.000, .003, .007, .010 sec)
15
Temporal Category date : 3 bytes : to time : 5 bytes : 00:00: to 23:59: datetime2 : 6 – 8 bytes : to , 100ns precision datetimeoffset : 10 bytes : to , 100ns precision smalldatetime : 4 bytes : to , precision of 1 minute datetime : 8 bytes : to , precision of 3⅓ millisecond (.000, .003, .007, .010 sec)
16
Temporal Examples Date literals • Using BETWEEN to identify day •
‘1/2/11’? ‘ ’? ‘ ’? Using BETWEEN to identify day • Adding days • What date is 0 or “” (empty string)? • Eliminating time •
17
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
18
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
19
Other Category cursor timestamp/rowversion : 8 bytes hierarchyid uniqueidentifier : 16 bytes sql_variant xml table
20
Other Category cursor timestamp/rowversion : 8 bytes hierarchyid uniqueidentifier : 16 bytes sql_variant xml table
21
Functions ISNUMERIC • TRY_CONVERT / TRY_CAST • ISDATE •
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()
22
Summary Pick data types with consideration Avoid deprecated data types
Size matters Domain constraint Avoid deprecated data types Beware of implicit conversions Not SARGable Unexpected results Use functions with caution
23
Thanks. Questions? Vern Rabe @VernRabe
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.