Download presentation
Presentation is loading. Please wait.
Published byEmory Lewis Modified over 9 years ago
1
TSQL Improvements & Data Types SQL Server 2008 TSQL Improvements & Data Types Presented by Tarek Ghazali IT Technical Specialist Microsoft SQL Server MVP, MCTS Microsoft Web Development MCP ITIL V3 Foundation Certified © 2009 Tarek Ghazali. All rights reserved.
2
Overview New Date and Time Data Types New Date and Time Data Types Table-Valued Parameters Table-Valued Parameters T-SQL Assign and Increment Operators T-SQL Assign and Increment Operators Row Constructors Row Constructors Grouping Sets Grouping Sets MERGE statement MERGE statement Dependency Views Dependency Views Performance Enhancements Performance Enhancements
3
New Date and Time Data Types Larger Value Space Larger Value Space –Current DATETIME - 1753-9999 Years –Current DATETIME - 0.00333 Second Accuracy –New Date Types - 0001-9999 Years –New Date/Time Types - Precisions to 100 nanoseconds
4
New Date and Time Data Types (Cont.) SQL Server 2008 extends date/time support SQL Server 2008 extends date/time support Variable Precision Saves Space Variable Precision Saves Space Separate Date and Time Saves Space Separate Date and Time Saves Space ANSI Compatible ANSI Compatible
5
Date/Time Types Compatibility New Data Types Use Same T-SQL Functions New Data Types Use Same T-SQL Functions –DATENAME (datepart, date) –DATEPART (datepart,date) –DATEDIFF (datepart, startdate, enddate) –DATEADD (datepart, number, date) Datepart can also be microsecond, nanosecond, TZoffset Datepart can also be microsecond, nanosecond, TZoffset
6
Date/Time Types Compatibility (Cont.) –MONTH –DAY –YEAR –CONVERT extensions
7
Date Time Library Extensions Higher precision date/time uses Higher precision date/time uses –SYSDATETIME –SYSUTCDATETIME –SYSDATETIMEOFFSET Original date/time uses Original date/time uses –GETDATE, GETUTCDATE, CURRENT_TIMESTAMP –ISDATE(datetime/smalldatetime)
8
Date Time Library Extensions (Cont.) Special functions for DATETIMEOFFSET Special functions for DATETIMEOFFSET –SWITCHOFFSET(datetimeoffset, timezone) –TODATETIMEOFFSET(datetime, timezone)
9
Localization Functions All Date/Time Types Support Localization All Date/Time Types Support Localization –SET DATEFIRST –SET DATEFORMAT –SET LANGUAGE - affects some date/time functions –@@DATEFIRST –@@LANGUAGE
10
Date/Time and Strings Dates are input and output in string format Dates are input and output in string format String (varchar) can be output through CAST/CONVERT String (varchar) can be output through CAST/CONVERT CONVERT has extensions for date/time types CONVERT has extensions for date/time types Dates are input in a variety of formats Dates are input in a variety of formats –Some of language-sensitive, some not
11
Table-Valued Parameters Inserts into structures with 1-n cardinality problematic Inserts into structures with 1-n cardinality problematic –One order -> N order line items –"N" is variable and can be large –Don't want to force a new order for every 20 line items
12
Table-Valued Parameters (Cont.) One database round-trip / line item slows things down One database round-trip / line item slows things down –No ARRAY data type in SQL Server –XML composition/decomposition used as an alternative Table-valued parameters solve this problem Table-valued parameters solve this problem
13
Common challenge: Passing list of values to SP/FN Problem: No ARRAY data type SQL Server 2005 @p = '1,2,3,4,5,…' Parsing string of delimited valuesShredding XML SQL Server 2008 temp table outside the SP Table Value Parameter
14
Table Types SQL Server has table variables SQL Server has table variables –DECLARE @t TABLE (id int); SQL Server 2008 adds strongly typed table variables SQL Server 2008 adds strongly typed table variables –CREATE TYPE mytab AS TABLE (id int); –DECLARE @t mytab; Parameters must use strongly typed table variables Parameters must use strongly typed table variables
15
Table Variables are Input Only Declare and initialize TABLE variable Declare and initialize TABLE variable –DECLARE @t mytab; –INSERT @t VALUES (1), (2), (3); –EXEC myproc @t;
16
Table Variables are Input Only (Cont.) Procedure must declare variable READONLY Procedure must declare variable READONLY –CREATE PROCEDURE usetable ( – @t mytab READONLY...) –AS – INSERT INTO lineitems SELECT * FROM @t; – UPDATE @t SET... -- no!
17
T-SQL Syntax Enhancements Single statement declare and initialize Single statement declare and initialize –DECLARE @i int = 4; Increment Operators: SET @i += 1; Increment Operators: SET @i += 1; Row constructors Row constructors –DECLARE @t TABLE (id int, name varchar(20)); –INSERT INTO @t VALUES – (1, 'Fred'), (2, 'Jim'), (3, 'Sue');
18
Grouping Sets Grouping Sets allow multiple GROUP BY clauses in a single SQL statement Grouping Sets allow multiple GROUP BY clauses in a single SQL statement –Multiple, arbitrary, sets of subtotals –Single read pass for performance –Nested subtotals provide ever better performance Grouping Sets are an ANSI-standard Grouping Sets are an ANSI-standard –COMPUTE BY is deprecated
19
Common challenge: Many grouping sub-totals required from the same table SQL Server 2005SQL Server 2008 SELECT a, sum(q) FROM T GROUP BY a UNION ALL SELECT a, b, sum(q) FROM T GROUP BY a, b UNION ALL SELECT a, b, c, sum(q) FROM T GROUP BY a, b, c SELECT a, sum(q) FROM T GROUP BY a UNION ALL SELECT a, b, sum(q) FROM T GROUP BY a, b UNION ALL SELECT a, b, c, sum(q) FROM T GROUP BY a, b, c SELECT a, b, c, sum(q) FROM T GROUP BY GROUPING SETS ( (a), (b), (a, b, c) ) SELECT a, b, c, sum(q) FROM T GROUP BY GROUPING SETS ( (a), (b), (a, b, c) )
20
GROUPING SETS, ROLLUP, and CUBE SQL Server 2008 - ANSI-syntax ROLLUP and CUBE SQL Server 2008 - ANSI-syntax ROLLUP and CUBE –Pre-2008 non-ANSI syntax is deprecated WITH ROLLUP produces n+1 different groupings of data WITH ROLLUP produces n+1 different groupings of data –where n is the number of columns in GROUP BY
21
GROUPING SETS, ROLLUP, & CUBE (Cont.) WITH CUBE produces 2^n different groupings WITH CUBE produces 2^n different groupings –where n is the number of columns in GROUP BY GROUPING SETS provide a "halfway measure" GROUPING SETS provide a "halfway measure" –More groupings than ROLLUP, but less than CUBE Grouping Sets are visible in query plan Grouping Sets are visible in query plan
22
GROUPING_ID and GROUPING Grouping Sets can produce non- homogeneous sets Grouping Sets can produce non- homogeneous sets –Grouping set includes NULL values for group members –Need to distinguish by grouping and NULL values
23
GROUPING_ID and GROUPING (Cont.) GROUPING (column expression) returns 0 or 1 GROUPING (column expression) returns 0 or 1 –Is this a group based on column expr. or NULL value? GROUPING_ID (a,b,c) is a bitmask GROUPING_ID (a,b,c) is a bitmask –GROUPING_ID bits are set based on column expressions a, b, and c
24
MERGE Statement Multiple set operations in a single SQL statement Multiple set operations in a single SQL statement Uses multiple sets as input Uses multiple sets as input –MERGE target USING source ON... Operations can be INSERT, UPDATE, DELETE Operations can be INSERT, UPDATE, DELETE
25
MERGE Statement (Cont.) Operations based on Operations based on –WHEN MATCHED –WHEN [TARGET] NOT MATCHED –WHEN SOURCE NOT MATCHED ANSI SQL 2006 compliant - with extensions ANSI SQL 2006 compliant - with extensions
26
What is to merge data? Merged DataSource Update Insert Delete
27
MERGE Statement SQL Server 2005: SQL Server 2005: Multiple DML Statements: Multiple DML Statements: SQL Server 2008: A Single DML Statement BEGIN TRAN COMMIT UPDATE tbl INNER JOIN s INSERT tbl LEFT OUTER JOIN DELETE tbl RIGHT JOIN s MERGE tbl USING s ON tbl.ID = s.ID WHEN MATCHED THEN UPDATE WHEN NOT MATCHED THEN INSERT WHEN NOT MATCHED BY SOURCE THEN DELETE;
28
More on MERGE MERGE statement can reference a $action column MERGE statement can reference a $action column –Used when MERGE used with OUTPUT clause Multiple WHEN clauses possible Multiple WHEN clauses possible –For MATCHED and SOURCE NOT MATCHED –Only one WHEN clause for TARGET NOT MATCHED
29
More on MERGE (Cont.) MERGE can be used with any table source MERGE can be used with any table source A MERGE statement causes triggers to be fired once A MERGE statement causes triggers to be fired once Rows affected includes total rows affected by all clauses Rows affected includes total rows affected by all clauses
30
MERGE Performance MERGE statement is transactional MERGE statement is transactional –No explicit transaction required One Pass Through Tables One Pass Through Tables –At most a full outer join –Matching rows = when matched –Left-outer join rows = when target not matched –Right-outer join rows = when source not matched
31
MERGE and Determinism UPDATE using a JOIN is non- deterministic UPDATE using a JOIN is non- deterministic –If more than one row in source matches ON clause, either/any row can be used for the UPDATE MERGE is deterministic MERGE is deterministic –If more than one row in source matches ON clause, –its an error
32
Keeping Track of Dependencies New dependency views replace sp_depends New dependency views replace sp_depends Dependency views are kept in sync as changes occur Dependency views are kept in sync as changes occur sys.dm_sql_referenced_entities sys.dm_sql_referenced_entities –Lists all named entities that an object references –Example: which objects does this stored procedure use?
33
Keeping Track of Dependencies (Cont.) sys.dm_sql_referencing_entities sys.dm_sql_referencing_entities –Lists all named entities that use an object –Example: which objects use this table? Can see references at OBJECT, DATABASE DDL TRIGGER, SERVER DDL TRIGGER level Can see references at OBJECT, DATABASE DDL TRIGGER, SERVER DDL TRIGGER level
34
Performance Enhancements MERGE and GROUPING SETS offer improvements MERGE and GROUPING SETS offer improvements –Less scans through table Table-valued parameters offer improvements Table-valued parameters offer improvements –Less round trips to database
35
Performance Enhancements (Cont.) Improvements for data warehouse queries Improvements for data warehouse queries –Earlier predicate filtering –Multiple bitmap iterators per query Plan Guide Improvements Plan Guide Improvements –Easier to create plan guides –Plan guides on DML statements
36
Review New SQL types increase the utility, improve storage requirements of date time type series New SQL types increase the utility, improve storage requirements of date time type series Strongly typed table-valued parameters help the database round trip problem Strongly typed table-valued parameters help the database round trip problem Grouping Sets allow arbitrary group by clauses for subtotals and totals Grouping Sets allow arbitrary group by clauses for subtotals and totals
37
Review (Cont) MERGE statement allows set-to-set comparison and multiple deterministic operations MERGE statement allows set-to-set comparison and multiple deterministic operations –ANSI standard compliance with extensions Object reference tracking makes schema and procedural code versioning less error-prone Object reference tracking makes schema and procedural code versioning less error-prone
38
Demo
39
Resources & Questions Microsoft Resources: Microsoft Resources: –http://msdn.microsoft.com/sqlserver/ http://msdn.microsoft.com/sqlserver/ –http://www.microsoft.com/sql/community http://www.microsoft.com/sql/community Contact me: Contact me: –tghazali@sqlmvp.com Download Presentation: Download Presentation: –http://www.sqlmvp.com http://www.sqlmvp.com
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.