Practical uses of new T-SQL functionality in SQL Server 2012 & Windows Azure SQL Database Tobias Ternström Lead Program Manager SQL Database Systems
Practical uses of new T-SQL functionality in SQL Server 2012 & Windows Azure SQL DatabaseTobias TernströmLead Program ManagerSQL Database Systems
Overview• Key generation• Exceptions & Messages• Calculations & Aggregations• Common tasks• Updates to Windows Azure SQL Database
Key generation• Use cases• Unique keys across multiple tables
Customers & Employees Contacts• Client-side/distributed-system key generation
Prep files with related items for import
• Options• GUIDs• Table to manage the keys• NEW: Sequences
Sequence Generators
• New Database Object, similar to the IDENTITY property
• Separates number-generation from table/column
• ANSI SQL standard compliant implementation
CREATE SEQUENCE [ schema_name . ] sequence_name
[ AS { <data_type> } ]
[ START WITH <constant> ]
[ INCREMENT BY <constant> ]
[ MINVALUE <constant> | NO MINVALUE ]
[ MAXVALUE <constant> | NO MAXVALUE ]
[ CYCLE | NO CYCLE ]
[ CACHE [ <constant> ] | NO CACHE ]
NEXT VALUE FOR
[ schema_name . ] sequence_name
[ OVER (<over_order_by_clause>) ]
EXEC sp_sequence_get_range …
• Guaranteed to be globally unique
• Can be made sequential• Caveats!
• Really hard to guess…(when not sequential)
• Use any data type for keys(for ex. strings)
• Can guarantee a continuous range of values (no wholes)• Including ranges
• Serializes number generation across transactions
• Supports all integer types, starting from TINYINT up to DECIMAL(38, 0)
• Supports range reservation
• Caching “knob” for performance tuning
• Large; requires 16 bytes storage
• Many systems don’t natively support GUIDs
• Causes fragmentation• Sequential generation
cannot be guaranteed even with NEWSEQUENTIALID()
• Access to the identifier is serialized across transactions• Performance issue
• Not native RDBMS functionality, more code
• Cannot guarantee a continuous range of values• E.g. can “lose”
values because of dirty shutdowns or rollbacks
GUIDs
Pros
Cons
Separate table Sequences
Exceptions & Messages• Use cases:• Send progress updates to the application• Log events• Raise & handle exceptions
• Options:• RAISERROR• xp_logevent• Logging table• NEW: THROW
New error handling with THROW• THROW <number>, <message>, <state>;• always aborts the batch• Batch aborts if not in SET XACT_ABORT ON where it
Transaction-aborts• Does not automatically use sys.messages• is even spelled correctly!
• re-THROW• BEGIN CATCH
…; THROW;END CATCH
• Flexible• Well known
• Logs events without sending a message to the client
• Multiple behaviors, easy to get it wrong
• Not supported on SQL Database
RAISERROR
Pros
Cons
xp_logevent
• Flexible, supports the attributes you specify
• Transactions need to be managed so log records are not rolled back
Logging table• Easy to use• Similar
behavior to other programming languages
• Re-throw capabilities
• Less flexible than RAISERROR
THROW
Calculations & Aggregations• Use cases:• Include aggregations in details
Cumulative sum, averages etc.• Reference other rows in a calculation
Finding gaps, trends, etc.• Options:• Imperative programming patterns• Correlated sub queries• NEW: Window Functions
Calculations across rows• Referencing same row is
simple• Transaction cost is $1.5/tran., what
was the total amount?• Amount + 1.5 AS TotalAmount
• What about other rows?• Current bank balance?• Current account balance?• Number of days since last tran.?• Amount of previous transaction?
TranId AccountId TranDate Amount
1 12011-12-19 120
2 22011-12-20 500
3 32011-12-21 430
4 12011-12-26 95
5 22011-12-27 50
6 32011-12-28 25
7 12012-01-02 250
8 22012-01-03 25
9 32012-01-04 5000
10 12012-01-09 75
3 trn. avg.
Window Functions - Different frame sizes
Calculate the average amount of the last 3 transactions
TranId AccountId TranDate Amount
1 1 2011-12-19 120
2 2 2011-12-20 500
3 3 2011-12-21 430
4 1 2011-12-26 95
5 2 2011-12-27 50
6 3 2011-12-28 25
7 1 2012-01-02 250
8 2 2012-01-03 25
9 3 2012-01-04500
0
10 1 2012-01-09 75
120.000
310.000
350.000
341.666
191.666
56.666
108.333
100.000
1758.3331700.000
The FRAMEWindow
PARTITIONThe FRAMEThe FRAMEThe FRAMEThe FRAMEThe FRAMEThe FRAMEWindow FRAME
Window Functions
Multiple partitions, e.g. for calculations for different accounts
BalanceTranId AccountId TranDate Amount
1 1 2011-12-19 120
4 1 2011-12-26 95
7 1 2012-01-02 250
10 1 2012-01-09 75
13 1 2012-01-16 125
16 1 2012-01-23 175
2 2 2011-12-20 500
5 2 2011-12-27 50
8 2 2012-01-03 25
11 2 2012-01-10 125
120
215
465
540
665
840
500
550
575
700
Window PARTITIONs
Window PARTITIONsThe FRAMEWindow FRAME
Window Functions• Aggregation• AVG, CHECKSUM_AGG, COUNT, COUNT_BIG, MIN, MAX, SUM, STDEV,
STDEVP, VAR, VARP• User-defined CLR Aggregates (excl. windowing)
• Analytical• ROW_NUMBER, NTILE, RANK, DENSE_RANK, CUME_DIST,
PERCENT_RANK
• Distribution• PERCENTILE_CONT, PERCENTILE_DISC
• Offset• LAG, LEAD, FIRST_VALUE, LAST_VALUE
• Approach that is commonly used in other programming languages
• Follows the SQL set based paradigm
• Improved optimization
• Easy to write• Following the SQL
set based paradigm• Not set based• Only individual
queries are optimized, not the operation as a whole
• Many lines of code Higher risk for bugs
• Bad performance due to plan with N2 complexity solution
• Non-trivial to write
• Not always the optimal solution
• Not all queries can be easily rewritten to leverage window functions
Imperative approach
Pros
Cons
Correlated sub queries Window Functions
Common tasks• Use cases:• Transform data to and from strings• Constructing date instances• Handle conversion errors• Paging
• Options:• Write yourself• NEW: Additional scalar functions
New Scalar Functions• New conversion functions for all types:• TRY_CONVERT(data_type[(length)], expression [,style])• TRY_CAST(expression AS data_type[(length)])
• New conversion functions to and from strings:• FORMAT(value, format [,culture])• PARSE(string_value AS data_type [USING culture])• TRY_PARSE(string_value AS data_type [USING culture])
• Other functions:• IIF(boolean_expr, true_value, false_value)• CHOOSE(index, val1, val2,... [,valN])• CONCAT(val1, val2,… [,val N])
New Scalar Functions• New date & time related functions:• EOMONTH(date [, months_to_add])• DATEFROMPARTS(year, month, day)• TIMEFROMPARTS(hour, minutes, seconds, fractions, scale)• DATETIME2FROMPARTS(year, month, day ,hour, minutes,
seconds, fractions, scale)• DATETIMEFROMPARTS(year, month, day, hour, minutes,
seconds, milliseconds)• SMALLDATETIMEFROMPARTS(year, month, day, hour, minutes)• DATETIMEOFFSETFROMPARTS (year, month, day, hour, minute,
seconds, fractions, hour_offset, minute_offset,
precision)
Query Constructs / Dynamic SQL enhancements
• OFFSET / FETCH• Support for paging result sets
• Enforce contract for Result Sets for EXEC
SELECT ...ORDER BY ... OFFSET <expr> ROWS FETCH NEXT <expr> ROWS ONLY
EXECUTE <proc|clr proc|remote proc|function> [WITH <execute_option>[,...n ]]{ RESULT SETS {UNDEFINED|NONE|(<result_sets_definition>)} }<result_sets_definition> ::= { <result_set_definition> | AS OBJECT [<object_location>.] {table_name | view_name | tvf} | AS TYPE [schema_name.]table_type_name | AS FOR XML | (…) [,...n ]}
Updates to Windows Azure SQL DatabaseThis year
• Recursive Triggers• Common Default Options
on CREATE TABLE & INDEX
• Support for SQL Database as a Linked Server• From SQL Server
• DBCC SHOW_STATISTICS• FW Rules @ DB Level
Coming• KILL <spid>• WITH XMLNAMESPACES• ORIGINAL_LOGIN,
HOST_NAME, HOST_ID• DDL Triggers:
EVENTDATA() • sys.column_type_usages,
sys.parameter_type_usages
Questions?