subqueries for superheroes

36
Subqueries For Superheroes

Upload: tracy-mckibben

Post on 15-Jun-2015

900 views

Category:

Technology


14 download

DESCRIPTION

A quick look at how T-SQL subqueries can be used to improve query performance, as well as some ways in which they can cause problems.

TRANSCRIPT

Page 1: Subqueries For Superheroes

SubqueriesFor Superheroes

Page 2: Subqueries For Superheroes

Tracy McKibbenDBA Supervisor, Senior SQL Server DBAPearson VUE

Blog: realsqlguy.com

Twitter: @RealSQLGuy

I’m not saying I’m Batman, I’m just saying that nobody has ever seen me and Batman in the same room together...

Page 3: Subqueries For Superheroes

What is a subquery?

Page 4: Subqueries For Superheroes

What is a subquery?A query wrapped within another query. Also known as an INNER query.

The lowly sidekicks of the SQL world.

Page 5: Subqueries For Superheroes

Anatomy of a subqueryWhich of these is a valid place to put a subquery?

SELECT ?FROM ?INNER JOIN ? ON ?WHERE ?GROUP BY ?HAVING ?

Page 6: Subqueries For Superheroes

Anatomy of a subquerySELECT <a subquery can go here>FROM <or here>INNER JOIN <or here> ON <or here>WHERE <or here>GROUP BY <or here>HAVING <or here>

It’s, like, the ultimate superpower!

Page 7: Subqueries For Superheroes

Is this a subquery?

Page 8: Subqueries For Superheroes

Is this a subquery?

Page 9: Subqueries For Superheroes

Is this a subquery?

Page 10: Subqueries For Superheroes

Is this a subquery?

Page 11: Subqueries For Superheroes

Is this a subquery?

Page 12: Subqueries For Superheroes

Is this a subquery?

Page 13: Subqueries For Superheroes

Is this a subquery?

Page 14: Subqueries For Superheroes

Is this a subquery?

Page 15: Subqueries For Superheroes

Exercise time!

Page 16: Subqueries For Superheroes

Sometimes loopyA subquery is either correlated or non-correlated.

What is the difference?

Page 17: Subqueries For Superheroes

Sometimes loopyA subquery is either correlated or non-correlated.

What is the difference?

A correlated subquery depends on theouter query, looping through the outerresultset, executing once for each rowin the outer query.

A non-correlated subquery standsalone, only running once, independent of the values in the outer query.

Page 18: Subqueries For Superheroes

What’s the correlation?Does this subquery stand alone (non-correlated), or does it need help (correlated)?

Page 19: Subqueries For Superheroes

What’s the correlation?Does this subquery stand alone (non-correlated), or does it need help (correlated)?

Page 20: Subqueries For Superheroes

What’s the correlation?Does this subquery stand alone (non-correlated), or does it need help (correlated)?

Page 21: Subqueries For Superheroes

What’s the correlation?Does this subquery stand alone (non-correlated), or does it need help (correlated)?

Page 22: Subqueries For Superheroes

What’s the correlation?Does this subquery stand alone (non-correlated), or does it need help (correlated)?

Page 23: Subqueries For Superheroes

What’s the correlation?Does this subquery stand alone (non-correlated), or does it need help (correlated)?

Page 24: Subqueries For Superheroes

What’s the correlation?Does this subquery stand alone (non-correlated), or does it need help (correlated)?

Page 25: Subqueries For Superheroes

What’s the correlation?Does this subquery stand alone (non-correlated), or does it need help (correlated)?

Page 26: Subqueries For Superheroes

Exercise time!

Page 27: Subqueries For Superheroes

Occupational hazardsSubqueries offer a lot of power and flexibility.

Page 28: Subqueries For Superheroes

Occupational hazardsSubqueries offer a lot of power and flexibility.

Butsometimes,things goterriblywrong

Page 29: Subqueries For Superheroes

COUNT vs EXISTSAre there any Green Lantern symbols in this collection?

How do you know?

Did you count them, or did you simply see green and say “yes”?

Did you do this:WHERE COUNT() > 0?

or, did you do this:WHERE EXISTS()?

Page 30: Subqueries For Superheroes

NOT IN vs NULLBe careful of NULL values returned by inner query or value list in a NOT IN clause. NULL has no value and can produce surprising results.

Always returns an empty resultset.

Page 31: Subqueries For Superheroes

MAX vs RANKMAX/MIN are often used in a subquery to determine the oldest/greatest/most recent row in the outer query.

RANK/OVER and other windowing functions are more efficient. defeats

Page 32: Subqueries For Superheroes

Exercise time!

Page 33: Subqueries For Superheroes

Subqueries In DisguiseSome T-SQL constructs behave much like subqueries.

● non-indexed views● common table

expressions (CTE)● user-defined functions

Be wary of performance problems.

Page 34: Subqueries For Superheroes

Exercise time!

Page 35: Subqueries For Superheroes

The conclusion● correlated vs non-correlated - know the

difference and potential impacts● multiple ways to get the same data, but not all

perform well● know new language features like RANK● don’t count unless you need a number● be wary of NULL values and hidden subqueries

Page 36: Subqueries For Superheroes

SubqueriesFor Superheroes