dell webinar 2014-06-24: subqueries for superheroes

43
Experts’ Perspective: Subqueries for Superheroes Tracy McKibben DBA Supervisor, Pearson VUE

Upload: tracy-mckibben

Post on 04-Jul-2015

308 views

Category:

Documents


1 download

DESCRIPTION

Slide deck from my Subqueries For Superheroes webinar, presented for Dell Software as part of their Experts Perspective series.

TRANSCRIPT

Page 1: Dell Webinar 2014-06-24: Subqueries For Superheroes

Experts’ Perspective:Subqueries for Superheroes

Tracy McKibben

DBA Supervisor, Pearson VUE

Page 2: Dell Webinar 2014-06-24: Subqueries For Superheroes

2 Dell Software

Your host

• Richard Douglas

• Sales Engineer / SQL Evangelist

• Maidenhead SQL User Group Leader

• Blog: http://SQL.RichardDouglas.co.uk

• Twitter: @SQLRich

• Email: [email protected]

Page 3: Dell Webinar 2014-06-24: Subqueries For Superheroes

3 Dell Software

Tracy McKibben

DBA Supervisor, Senior SQL Server DBA

Pearson 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 4: Dell Webinar 2014-06-24: Subqueries For Superheroes

4 Dell Software

Today’s Lineup

• What is a subquery?

• Correlated vs non-correlated

• Occupational hazards

• Subqueries in disguise

Page 5: Dell Webinar 2014-06-24: Subqueries For Superheroes

5 Dell Software

What is a subquery?

A query wrapped within, embedded within, or otherwise referenced by another query. Also known as an INNER query.

The lowly sidekicks of the SQL world.

Page 6: Dell Webinar 2014-06-24: Subqueries For Superheroes

6 Dell Software

Anatomy of a subquery

Which of these is a valid place to put a subquery?

SELECT ?

FROM ?

INNER JOIN ?

ON ?

WHERE ?

GROUP BY ?

HAVING ?

Page 7: Dell Webinar 2014-06-24: Subqueries For Superheroes

7 Dell Software

Anatomy of a subquery

SELECT <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 8: Dell Webinar 2014-06-24: Subqueries For Superheroes

8 Dell Software

Is this a subquery?

Page 9: Dell Webinar 2014-06-24: Subqueries For Superheroes

9 Dell Software

Is this a subquery?

Page 10: Dell Webinar 2014-06-24: Subqueries For Superheroes

10 Dell Software

Is this a subquery?

Page 11: Dell Webinar 2014-06-24: Subqueries For Superheroes

11 Dell Software

Is this a subquery?

Page 12: Dell Webinar 2014-06-24: Subqueries For Superheroes

12 Dell Software

Is this a subquery?

Page 13: Dell Webinar 2014-06-24: Subqueries For Superheroes

13 Dell Software

Is this a subquery?

Page 14: Dell Webinar 2014-06-24: Subqueries For Superheroes

14 Dell Software

Is this a subquery?

Page 15: Dell Webinar 2014-06-24: Subqueries For Superheroes

15 Dell Software

Is this a subquery?

Page 16: Dell Webinar 2014-06-24: Subqueries For Superheroes

16 Dell Software

Exercise Time!

Page 17: Dell Webinar 2014-06-24: Subqueries For Superheroes

17 Dell Software

Sometimes loopy

A subquery is either correlated or non-correlated.

What is the difference?

Page 18: Dell Webinar 2014-06-24: Subqueries For Superheroes

18 Dell Software

Sometimes loopy

A subquery is either correlated or non-correlated.

What is the difference?

A correlated subquery depends on the

outer query, looping through the outer

resultset, executing once for each row

in the outer query.

A non-correlated subquery stands

alone, only running once, independent

of the values in the outer query.

Page 19: Dell Webinar 2014-06-24: Subqueries For Superheroes

19 Dell Software

What’s the correlation?

Does this subquery stand alone (non-correlated), or does it need help (correlated)?

Page 20: Dell Webinar 2014-06-24: Subqueries For Superheroes

20 Dell Software

What’s the correlation?

Does this subquery stand alone (non-correlated), or does it need help (correlated)?

Page 21: Dell Webinar 2014-06-24: Subqueries For Superheroes

21 Dell Software

What’s the correlation?

Does this subquery stand alone (non-correlated), or does it need help (correlated)?

Page 22: Dell Webinar 2014-06-24: Subqueries For Superheroes

22 Dell Software

What’s the correlation?

Does this subquery stand alone (non-correlated), or does it need help (correlated)?

Page 23: Dell Webinar 2014-06-24: Subqueries For Superheroes

23 Dell Software

What’s the correlation?

Does this subquery stand alone (non-correlated), or does it need help (correlated)?

Page 24: Dell Webinar 2014-06-24: Subqueries For Superheroes

24 Dell Software

What’s the correlation?

Does this subquery stand alone (non-correlated), or does it need help (correlated)?

Page 25: Dell Webinar 2014-06-24: Subqueries For Superheroes

25 Dell Software

What’s the correlation?

Does this subquery stand alone (non-correlated), or does it need help (correlated)?

Page 26: Dell Webinar 2014-06-24: Subqueries For Superheroes

26 Dell Software

What’s the correlation?

Does this subquery stand alone (non-correlated), or does it need help (correlated)?

Page 27: Dell Webinar 2014-06-24: Subqueries For Superheroes

27 Dell Software

Exercise Time!

Page 28: Dell Webinar 2014-06-24: Subqueries For Superheroes

28 Dell Software

Occupational hazards

Subqueries offer a lot of power and flexibility.

Page 29: Dell Webinar 2014-06-24: Subqueries For Superheroes

29 Dell Software

Occupational hazards

Subqueries offer a lot of power and flexibility.

But

sometimes,

things go

terribly

wrong

Page 30: Dell Webinar 2014-06-24: Subqueries For Superheroes

30 Dell Software

COUNT vs EXISTS

Are 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 31: Dell Webinar 2014-06-24: Subqueries For Superheroes

31 Dell Software

NOT IN vs NULL

Be 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 32: Dell Webinar 2014-06-24: Subqueries For Superheroes

32 Dell Software

MAX vs RANK

MAX/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. or

Page 33: Dell Webinar 2014-06-24: Subqueries For Superheroes

33 Dell Software

Exercise Time!

Page 34: Dell Webinar 2014-06-24: Subqueries For Superheroes

34 Dell Software

Subqueries In Disguise

Some T-SQL constructs behave much like subqueries.

• non-indexed views

• common table expressions (CTE)

• user-defined functions

Be wary of performance problems.

Page 35: Dell Webinar 2014-06-24: Subqueries For Superheroes

35 Dell Software

Exercise Time!

Page 36: Dell Webinar 2014-06-24: Subqueries For Superheroes

36 Dell Software

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 37: Dell Webinar 2014-06-24: Subqueries For Superheroes

37 Dell Software

Join us at these upcoming SQL events!

SQL Community Corner

Date Location

June 25PASS DBA Virtual Chapter – “The Day After Tomorrow; Why You Need to Baseline”

July 12 SQLSaturday #312 - Sacramento 2014

July 17-19 SQLBits XII – Telford, UK

July 26 SQLSaturday #302 – Albany 2014

Page 38: Dell Webinar 2014-06-24: Subqueries For Superheroes

38 Dell Software

Visit our SQL Community on ToadWorld-www.toadworld.com/platforms/sql-server/default.aspx

Page 39: Dell Webinar 2014-06-24: Subqueries For Superheroes

Pg. 39© 2012 Quest Software Inc. All rights reserved.

Solution Area Product Description

Fast, flexible backup and

recovery with industry-leading

compression technology

Discover and resolve

performance issues in production

before they impact end users and

service levels

Deepest possible understanding

of database performance and

norms

Plan and develop applications

that deliver both functionality and

optimal performance

Backup and

Recovery

Performance

& Operations

Performance

Tuning

Development

Comprehensive schema, object,

security and change

managementAdministration

Community crowdsourcing for

SQL Server tracing and

performance information!

Community,

Knowledge,

Training

Page 40: Dell Webinar 2014-06-24: Subqueries For Superheroes

40 Dell Software

Page 41: Dell Webinar 2014-06-24: Subqueries For Superheroes

41 Dell Software

Page 42: Dell Webinar 2014-06-24: Subqueries For Superheroes

42 Dell Software

Questions?

Continue the conversation on Twitter via #DellSQL

Page 43: Dell Webinar 2014-06-24: Subqueries For Superheroes

Thank You!