t-sql windowing functions - · pdf filet-sql windowing functions kathi kellenberger,...

46
T-SQL Windowing Functions Deep Dive Kathi Kellenberger, Teammate Linchpin People

Upload: phamngoc

Post on 06-Mar-2018

230 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: T-SQL Windowing Functions -   · PDF fileT-SQL Windowing Functions Kathi Kellenberger, Teammate Deep Dive Linchpin People

T-SQL WindowingFunctionsDeep DiveKathi Kellenberger, Teammate

Linchpin People

Page 2: T-SQL Windowing Functions -   · PDF fileT-SQL Windowing Functions Kathi Kellenberger, Teammate Deep Dive Linchpin People

Session Evaluations

ways to access

Go to

passsummit.com/evals

Download the GuideBook App

and search: PASS Summit 2014

Follow the QR code link displayed

on session signage throughout the

conference venue and in the

program guide

Submit by 11:59 PM ESTFriday Nov. 7 toWIN prizes

Your feedback is important and valuable.

Evaluation Deadline:

11:59 PM EST, Sunday Nov. 16

Page 3: T-SQL Windowing Functions -   · PDF fileT-SQL Windowing Functions Kathi Kellenberger, Teammate Deep Dive Linchpin People

Agenda

• Part 1: Overview of T-SQL Windowing Functions

• BREAK

• Part 2: Real World Examples

• Part 3: Query Tuning and Performance

Page 4: T-SQL Windowing Functions -   · PDF fileT-SQL Windowing Functions Kathi Kellenberger, Teammate Deep Dive Linchpin People

Agenda

• Part 1: Overview of T-SQL Windowing Functions• What are Windowing Functions?

• 2005 Features• Ranking

• Window Aggregates

• 2012 Enhancements• Framing

• Running totals

• Analytic functions

• Part 2: Real World Examples

• Part 3: Query Tuning and Performance

Page 5: T-SQL Windowing Functions -   · PDF fileT-SQL Windowing Functions Kathi Kellenberger, Teammate Deep Dive Linchpin People

Nothing to do with OS

Functions operate on a set, or window

Found only in SELECT and ORDER BY

OVER clause defines the window

Partitions – not the same as GROUP BY

What are Windowing Functions?

Page 6: T-SQL Windowing Functions -   · PDF fileT-SQL Windowing Functions Kathi Kellenberger, Teammate Deep Dive Linchpin People

ROW_NUMBER()A unique number over the window

RANK()Repeats number if ties

DENSE_RANK()Repeats number if ties

NTILE()Divides data into buckets

Ranking Functions

Page 7: T-SQL Windowing Functions -   · PDF fileT-SQL Windowing Functions Kathi Kellenberger, Teammate Deep Dive Linchpin People

ROW_NUMBER() Example

CustomerID OrderID Total ROW_NUMBER() OVER(ORDER BY OrderID)

1 101 100 1

2 102 40 2

2 103 11 3

3 104 432 4

1 105 2000 5

1 106 300 6

4 107 674 7

5 108 76 8

4 109 234 9

4 110 889 10

5 111 234 11

Page 8: T-SQL Windowing Functions -   · PDF fileT-SQL Windowing Functions Kathi Kellenberger, Teammate Deep Dive Linchpin People

ROW_NUMBER() Example - Partitioning

CustomerID OrderID TotalROW_NUMBER() OVER(PARTITION BY CustomerID

ORDER BY OrderID)

1 101 100 1

1 105 2000 2

1 106 300 3

2 102 40 1

2 103 11 2

3 104 432 1

4 107 674 1

4 109 234 2

4 110 889 3

5 108 76 1

5 111 234 2

Page 9: T-SQL Windowing Functions -   · PDF fileT-SQL Windowing Functions Kathi Kellenberger, Teammate Deep Dive Linchpin People

Ranking FunctionsDemo

Page 10: T-SQL Windowing Functions -   · PDF fileT-SQL Windowing Functions Kathi Kellenberger, Teammate Deep Dive Linchpin People

Your favorite aggregate functions

with no GROUP BY

Add aggregate functions to

non-aggregate query

Return details

Window Aggregates

Page 11: T-SQL Windowing Functions -   · PDF fileT-SQL Windowing Functions Kathi Kellenberger, Teammate Deep Dive Linchpin People

Window Aggregate Example

CustomerID OrderID Total SUM(Total) OVER()

1 101 100 4990

2 102 40 4990

2 103 11 4990

3 104 432 4990

1 105 2000 4990

1 106 300 4990

4 107 674 4990

5 108 76 4990

4 109 234 4990

4 110 889 4990

5 111 234 4990

Page 12: T-SQL Windowing Functions -   · PDF fileT-SQL Windowing Functions Kathi Kellenberger, Teammate Deep Dive Linchpin People

Window Aggregate Example -Partitioning

CustomerID OrderID Total SUM(Total) OVER(PARTITION BY CustomerID)

1 101 100 2400

1 105 2000 2400

1 106 300 2400

2 102 40 51

2 103 11 51

3 104 432 432

4 107 674 1797

4 109 234 1797

4 110 889 1797

5 108 76 310

5 111 234 310

Page 13: T-SQL Windowing Functions -   · PDF fileT-SQL Windowing Functions Kathi Kellenberger, Teammate Deep Dive Linchpin People

Window AggregatesDemo

Page 14: T-SQL Windowing Functions -   · PDF fileT-SQL Windowing Functions Kathi Kellenberger, Teammate Deep Dive Linchpin People

Framing

ORDER BY and Framing

to Window Aggregates

Analytic functions

2012 Enhancements

Page 15: T-SQL Windowing Functions -   · PDF fileT-SQL Windowing Functions Kathi Kellenberger, Teammate Deep Dive Linchpin People

Further define window

TermsUnbounded Preceding

Unbounded Following

Current Row

RANGE not fully implementedAlmost like ROWS, is the default

Framing: ROWS and RANGE

Page 16: T-SQL Windowing Functions -   · PDF fileT-SQL Windowing Functions Kathi Kellenberger, Teammate Deep Dive Linchpin People

ROWS BETWEEN UNBOUNDED

PRECEDING AND

CURRENT ROW

ROWS UNBOUNDED

PRECEDING

Framing

Page 17: T-SQL Windowing Functions -   · PDF fileT-SQL Windowing Functions Kathi Kellenberger, Teammate Deep Dive Linchpin People

ROWS BETWEEN CURRENT

ROW AND UNBOUNDED

FOLLOWING

Framing

Page 18: T-SQL Windowing Functions -   · PDF fileT-SQL Windowing Functions Kathi Kellenberger, Teammate Deep Dive Linchpin People

ROWS BETWEEN 2 PRECEDING AND

AND CURRENT ROW

Framing

Page 19: T-SQL Windowing Functions -   · PDF fileT-SQL Windowing Functions Kathi Kellenberger, Teammate Deep Dive Linchpin People

ROWS BETWEEN 5 PRECEDING AND

AND 2 FOLLOWING

Framing

Page 20: T-SQL Windowing Functions -   · PDF fileT-SQL Windowing Functions Kathi Kellenberger, Teammate Deep Dive Linchpin People

CustomerID OrderID Total UNBOUNDED PRECEDING AND CURRENT ROW

1 101 100

2 102 40

2 103 11

3 104 432

1 105 2000

1 106 300

4 107 674

5 108 76

4 109 234

4 110 889

5 111 234

Framing Example

Page 21: T-SQL Windowing Functions -   · PDF fileT-SQL Windowing Functions Kathi Kellenberger, Teammate Deep Dive Linchpin People

CustomerID OrderID Total SUM(Total) OVER(ORDER BY OrderID)

1 101 100 100

2 102 40 140

2 103 11 151

3 104 432 583

1 105 2000 2583

1 106 300 2883

4 107 674 3557

5 108 76 3633

4 109 234 3867

4 110 889 4756

5 111 234 4990

Framing Example

Page 22: T-SQL Windowing Functions -   · PDF fileT-SQL Windowing Functions Kathi Kellenberger, Teammate Deep Dive Linchpin People

Running TotalsDemo

Page 23: T-SQL Windowing Functions -   · PDF fileT-SQL Windowing Functions Kathi Kellenberger, Teammate Deep Dive Linchpin People

LAG() and LEAD()

FIRST_VALUE() and LAST_VALUE()

PERCENT_RANK() and

CUME_DIST()

PERCENTILE_DISC and

PERCENTILE_CONT

Analytic Functions

Page 24: T-SQL Windowing Functions -   · PDF fileT-SQL Windowing Functions Kathi Kellenberger, Teammate Deep Dive Linchpin People

CustomerID OrderID Total LAG(Total) OVER(ORDER BY OrderID)

1 101 100 NULL

2 102 40 100

2 103 11 40

3 104 432 11

1 105 2000 432

1 106 300 2000

4 107 674 300

5 108 76 674

4 109 234 76

4 110 889 234

5 111 234 889

LAG() Example

Page 25: T-SQL Windowing Functions -   · PDF fileT-SQL Windowing Functions Kathi Kellenberger, Teammate Deep Dive Linchpin People

CustomerID OrderID Total LEAD(Total) OVER(ORDER BY OrderID)

1 101 100 40

2 102 40 11

2 103 11 432

3 104 432 2000

1 105 2000 300

1 106 300 674

4 107 674 76

5 108 76 234

4 109 234 889

4 110 889 234

5 111 234 NULL

LEAD() Example

Page 26: T-SQL Windowing Functions -   · PDF fileT-SQL Windowing Functions Kathi Kellenberger, Teammate Deep Dive Linchpin People

CustomerID OrderID TotalFIRST_ROW(Total) OVER(ORDER BY

OrderID)

1 101 100 100

2 102 40 100

2 103 11 100

3 104 432 100

1 105 2000 100

1 106 300 100

4 107 674 100

5 108 76 100

4 109 234 100

4 110 889 100

5 111 234 100

FIRST_ROW()* Example

*Frame required!

Page 27: T-SQL Windowing Functions -   · PDF fileT-SQL Windowing Functions Kathi Kellenberger, Teammate Deep Dive Linchpin People

CustomerID OrderID TotalLAST_ROW(Total) OVER(ORDER BY

OrderID)

1 101 100 234

2 102 40 234

2 103 11 234

3 104 432 234

1 105 2000 234

1 106 300 234

4 107 674 234

5 108 76 234

4 109 234 234

4 110 889 234

5 111 234 234

LAST_ROW()* Example

*Frame required!

Page 28: T-SQL Windowing Functions -   · PDF fileT-SQL Windowing Functions Kathi Kellenberger, Teammate Deep Dive Linchpin People

LAG, LEAD, FIRST_VALUE, LAST_VALUE

Demo

Page 29: T-SQL Windowing Functions -   · PDF fileT-SQL Windowing Functions Kathi Kellenberger, Teammate Deep Dive Linchpin People

Average High Temp by Month STL

40 4355

6777

85 89 8881

6956

43

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec

Page 30: T-SQL Windowing Functions -   · PDF fileT-SQL Windowing Functions Kathi Kellenberger, Teammate Deep Dive Linchpin People

What is the Percent Rank?

40 43 4355 56

67 6977 81 85 88 89

Jan Feb Dec Mar Nov Apr Oct May Sept Jun Aug Jul

Rank = 4

Percent Rank = 27% (4-1)/(12-1)

Cumulative Distribution = 33% (4)/12

Page 31: T-SQL Windowing Functions -   · PDF fileT-SQL Windowing Functions Kathi Kellenberger, Teammate Deep Dive Linchpin People

What is the value at 50%, the median?

40 43 4355 56

67 6977 81 85 88 89

Jan Feb Dec Mar Nov Apr Oct May Sept Jun Aug Jul

Percentile Cont = 68 (67 + 69)/2

Percentile Disc = 67

Page 32: T-SQL Windowing Functions -   · PDF fileT-SQL Windowing Functions Kathi Kellenberger, Teammate Deep Dive Linchpin People

Percent FunctionsDemo

Page 33: T-SQL Windowing Functions -   · PDF fileT-SQL Windowing Functions Kathi Kellenberger, Teammate Deep Dive Linchpin People

Agenda

• Part 1: Overview of T-SQL Windowing Functions

• Part 2: Real World Examples• Islands and Gaps

• Fun with Row_Number

• Fun with window aggregates

• The stock problem

• Interesting ideas…

• Part 3: Query Tuning and Performance

Page 34: T-SQL Windowing Functions -   · PDF fileT-SQL Windowing Functions Kathi Kellenberger, Teammate Deep Dive Linchpin People

What is the Islands and Gaps Problem?

1,2,3,4,5,8,9,13,14,16,17

Page 35: T-SQL Windowing Functions -   · PDF fileT-SQL Windowing Functions Kathi Kellenberger, Teammate Deep Dive Linchpin People

Real World…Demo

Page 36: T-SQL Windowing Functions -   · PDF fileT-SQL Windowing Functions Kathi Kellenberger, Teammate Deep Dive Linchpin People

Session Evaluations

ways to access

Go to

passsummit.com/evals

Download the GuideBook App

and search: PASS Summit 2014

Follow the QR code link displayed

on session signage throughout the

conference venue and in the

program guide

Submit by 11:59 PM ESTFriday Nov. 7 toWIN prizes

Your feedback is important and valuable.

Evaluation Deadline:

11:59 PM EST, Sunday Nov. 16

Page 37: T-SQL Windowing Functions -   · PDF fileT-SQL Windowing Functions Kathi Kellenberger, Teammate Deep Dive Linchpin People

Agenda

• Part 1: Overview of T-SQL Windowing Functions

• Part 2: Real World Examples

• Part 3: Query Tuning and Performance• Execution plan operators

• Indexing

• Framing

• Large table comparisons

Page 38: T-SQL Windowing Functions -   · PDF fileT-SQL Windowing Functions Kathi Kellenberger, Teammate Deep Dive Linchpin People

Execution Plan Operators

Page 39: T-SQL Windowing Functions -   · PDF fileT-SQL Windowing Functions Kathi Kellenberger, Teammate Deep Dive Linchpin People

POC Index

Columns needed by WHERE

Partition by column

Order by column

Include Covering columns

Indexing

Page 40: T-SQL Windowing Functions -   · PDF fileT-SQL Windowing Functions Kathi Kellenberger, Teammate Deep Dive Linchpin People

Query Tuning Part 1Demo

Page 41: T-SQL Windowing Functions -   · PDF fileT-SQL Windowing Functions Kathi Kellenberger, Teammate Deep Dive Linchpin People

Default frame:

RANGE BETWEEN UNBOUNDED PRECEDING

AND CURRENT ROW

Specify ROWS for better performance

Subtle logic differences

Framing: Running Aggregates, Last_value, First_value

Page 42: T-SQL Windowing Functions -   · PDF fileT-SQL Windowing Functions Kathi Kellenberger, Teammate Deep Dive Linchpin People

Query Tuning Part 2Demo

Page 43: T-SQL Windowing Functions -   · PDF fileT-SQL Windowing Functions Kathi Kellenberger, Teammate Deep Dive Linchpin People

Subtotals – 30 million rows

1.75

0.50 0.5

1.75

0.500.75

1.75

0.501

Window Aggregate CTE Correlated Subquery

1 Calc 2 Calcs 3 Calcs

Page 44: T-SQL Windowing Functions -   · PDF fileT-SQL Windowing Functions Kathi Kellenberger, Teammate Deep Dive Linchpin People

Running Totals 7.5, 15, and 30 million rows

2 04 64

0.5

17

118

1

50.5

21.5

WF Default Frame WF Rows Join/SQ Cursor

7.5 M 15 M 30 M

Page 45: T-SQL Windowing Functions -   · PDF fileT-SQL Windowing Functions Kathi Kellenberger, Teammate Deep Dive Linchpin People

Big Adventure Script:

http://sqlblog.com/blogs/adam_machanic/archive/2011/10

/17/thinking-big-adventure.aspx

Itzik Ben-Gan’s book: Microsoft SQL Server 2012 High-

Performance T-SQL Using Window Functions

My blog: http://auntkathisql.com

My Book: Beginning T-SQL (Apress, 2014)

Resources

Page 46: T-SQL Windowing Functions -   · PDF fileT-SQL Windowing Functions Kathi Kellenberger, Teammate Deep Dive Linchpin People

Session Evaluations

ways to access

Go to

passsummit.com/evals

Download the GuideBook App

and search: PASS Summit 2014

Follow the QR code link displayed

on session signage throughout the

conference venue and in the

program guide

Submit by 11:59 PM ESTFriday Nov. 7 toWIN prizes

Your feedback is important and valuable.

Evaluation Deadline:

11:59 PM EST, Sunday Nov. 16