subqueries for superheroes

Post on 15-Jun-2015

900 Views

Category:

Technology

14 Downloads

Preview:

Click to see full reader

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

SubqueriesFor 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...

What is a subquery?

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

The lowly sidekicks of the SQL world.

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

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

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!

Is this a subquery?

Is this a subquery?

Is this a subquery?

Is this a subquery?

Is this a subquery?

Is this a subquery?

Is this a subquery?

Is this a subquery?

Exercise time!

Sometimes loopyA subquery is either correlated or non-correlated.

What is the difference?

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.

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

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

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

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

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

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

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

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

Exercise time!

Occupational hazardsSubqueries offer a lot of power and flexibility.

Occupational hazardsSubqueries offer a lot of power and flexibility.

Butsometimes,things goterriblywrong

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()?

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.

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

Exercise time!

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.

Exercise time!

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

SubqueriesFor Superheroes

top related