SlideShare a Scribd company logo
By Abhishek Sur
                     Microsoft MVP, Client App Dev
Follow us at Twitter @abhi2434 & @sqlservergeeks
Why SQL Server 2012 ?
 Always On
 Contained Databases
 Column Store Indexes
 Visual Studio integration with Management Studio
 TSQL Enhancements




                                                     2
3
The Concept
RowStore Page
 Int               Varchar     Datetime        float
                                                             • ColumnStore reduces
                                                               redundancy of data and
                                                               compression as the data type
                                                               remains same for the entire page.
                                                             • All data for a single column could
                                                               be retrieved from a non clustered
Columnstore Page                                               index page.
Int    Int   Int     Int     float   float   float   float
                                                             Limitation
                                                             • 1 Table can have one
                                                               columnstore index.
                                                                                              4
5
Introducing SQL Server 2012 Transact-SQL
Improvements
•   Query Pagination
•   OVER Clause Windowing
•   Sequences
•   Metadata Discovery
•   Enhanced Function Library
•   General T-SQL-Related Enhancements
Query Pagination
• Common application requirement to retrieve rows
  − For a given page
  − For a given page length
− Our Options ???
  − Retrieve all and filter in application end
  − Retrieve data using Row_NUMBER function
  − Any other options ?

− Denali gives better option for pagination
OVER Clause Windowing
• Some existing queries do not optimize well
• Example: Details of orders and days since previous order of each product

-- Traditional approach

SELECT rs.ProductKey,rs.OrderDateKey,rs.SalesOrderNumber,
rs.OrderDateKey- (SELECT TOP 1 prev.OrderDateKey
FROM dbo.FactResellerSales AS prev
WHERE rs.ProductKey=prev.ProductKey
AND prev.OrderDateKey<=rs.OrderDateKey
AND prev.SalesOrderNumber<rs.SalesOrderNumber
ORDER BY prev.OrderDateKey DESC,
prev.SalesOrderNumber DESC)
AS DaysSincePrevOrder
FROM dbo.FactResellerSales AS rs
ORDER BY rs.ProductKey,rs.OrderDateKey,rs.SalesOrderNumber;
OVER Clause Windowing
• Some existing queries do not optimize well
• Example: Details of orders and days since previous order of each product

-- Windowed approach

SELECT ProductKey,OrderDateKey,SalesOrderNumber,
OrderDateKey-LAG(OrderDateKey)
OVER (PARTITION BY ProductKey
ORDER BY OrderDateKey,SalesOrderNumber)
AS DaysSincePrevOrder
FROM dbo.FactResellerSales AS rs
ORDER BY ProductKey,OrderDateKey,SalesOrderNumber;
OVER Clause Windowing
• Some existing queries do not optimize well
• Example: Details of orders and days since previous order of each product
Sequences
•   User-defined object
•   Not tied to any particular table
•   Not restricted to being used in a single table
•   Eases migration from other database engines
•   Does not support Transaction
Metadata Discovery
• Applications need to determine metadata associated with code batches or
  stored procedures
• New functionality provided as both system stored procedures and via DMVs
    − DMVs make the metadata easy to consume in a relational query
    − Additional DMV option for passing an object ID (helpful for stored procedures or triggers)
System Stored Procedure                        Equivalent DMV
sp_describe_first_result_set                   sys.dm_exec_describe_first_result_set
                                               sys.dm_exec_describe_first_result_set_for_object


• EXECUTE supports a WITH RESULT SETS clause
Enhanced Function Library
• New functions added

 Conversion    Date and Time             Logical and String   Analytic
 PARSE         DATEFROMPARTS             CHOOSE               CUME_DIST
 TRY_PARSE     TIMEFROMPARTS             IIF                  PERCENTILE_DIST
 TRY_CONVERT   DATETIMEFROMPARTS         CONCAT               PERCENTILE_CONT
               DATETIME2FROMPARTS        FORMAT               PERCENT_RANK
               SMALLDATETIMEFROMPARTS                         FIRST_VALUE
               DATETIMEOFFSETFROMPARTS                        LAST_VALUE
               EOMONTH                                        LEAD
                                                              LAG

• LOG function now supports an optional base
General T-SQL-Related Enhancements
• THROW statement
  − Can throw user-errors
  − Can rethrow any errors in a CATCH block (including system errors)


• Extended Events supports many new events
  − Mostly relating to memory page allocation
WWW.KOLKATAGEEKS.COM

WWW.SQLSERVERGEEKS.COM

More Related Content

SQL Server2012 Enhancements

  • 1. By Abhishek Sur Microsoft MVP, Client App Dev Follow us at Twitter @abhi2434 & @sqlservergeeks
  • 2. Why SQL Server 2012 ?  Always On  Contained Databases  Column Store Indexes  Visual Studio integration with Management Studio  TSQL Enhancements 2
  • 3. 3
  • 4. The Concept RowStore Page Int Varchar Datetime float • ColumnStore reduces redundancy of data and compression as the data type remains same for the entire page. • All data for a single column could be retrieved from a non clustered Columnstore Page index page. Int Int Int Int float float float float Limitation • 1 Table can have one columnstore index. 4
  • 5. 5
  • 6. Introducing SQL Server 2012 Transact-SQL Improvements • Query Pagination • OVER Clause Windowing • Sequences • Metadata Discovery • Enhanced Function Library • General T-SQL-Related Enhancements
  • 7. Query Pagination • Common application requirement to retrieve rows − For a given page − For a given page length − Our Options ??? − Retrieve all and filter in application end − Retrieve data using Row_NUMBER function − Any other options ? − Denali gives better option for pagination
  • 8. OVER Clause Windowing • Some existing queries do not optimize well • Example: Details of orders and days since previous order of each product -- Traditional approach SELECT rs.ProductKey,rs.OrderDateKey,rs.SalesOrderNumber, rs.OrderDateKey- (SELECT TOP 1 prev.OrderDateKey FROM dbo.FactResellerSales AS prev WHERE rs.ProductKey=prev.ProductKey AND prev.OrderDateKey<=rs.OrderDateKey AND prev.SalesOrderNumber<rs.SalesOrderNumber ORDER BY prev.OrderDateKey DESC, prev.SalesOrderNumber DESC) AS DaysSincePrevOrder FROM dbo.FactResellerSales AS rs ORDER BY rs.ProductKey,rs.OrderDateKey,rs.SalesOrderNumber;
  • 9. OVER Clause Windowing • Some existing queries do not optimize well • Example: Details of orders and days since previous order of each product -- Windowed approach SELECT ProductKey,OrderDateKey,SalesOrderNumber, OrderDateKey-LAG(OrderDateKey) OVER (PARTITION BY ProductKey ORDER BY OrderDateKey,SalesOrderNumber) AS DaysSincePrevOrder FROM dbo.FactResellerSales AS rs ORDER BY ProductKey,OrderDateKey,SalesOrderNumber;
  • 10. OVER Clause Windowing • Some existing queries do not optimize well • Example: Details of orders and days since previous order of each product
  • 11. Sequences • User-defined object • Not tied to any particular table • Not restricted to being used in a single table • Eases migration from other database engines • Does not support Transaction
  • 12. Metadata Discovery • Applications need to determine metadata associated with code batches or stored procedures • New functionality provided as both system stored procedures and via DMVs − DMVs make the metadata easy to consume in a relational query − Additional DMV option for passing an object ID (helpful for stored procedures or triggers) System Stored Procedure Equivalent DMV sp_describe_first_result_set sys.dm_exec_describe_first_result_set sys.dm_exec_describe_first_result_set_for_object • EXECUTE supports a WITH RESULT SETS clause
  • 13. Enhanced Function Library • New functions added Conversion Date and Time Logical and String Analytic PARSE DATEFROMPARTS CHOOSE CUME_DIST TRY_PARSE TIMEFROMPARTS IIF PERCENTILE_DIST TRY_CONVERT DATETIMEFROMPARTS CONCAT PERCENTILE_CONT DATETIME2FROMPARTS FORMAT PERCENT_RANK SMALLDATETIMEFROMPARTS FIRST_VALUE DATETIMEOFFSETFROMPARTS LAST_VALUE EOMONTH LEAD LAG • LOG function now supports an optional base
  • 14. General T-SQL-Related Enhancements • THROW statement − Can throw user-errors − Can rethrow any errors in a CATCH block (including system errors) • Extended Events supports many new events − Mostly relating to memory page allocation