Skip to content
0:00 / 0:00

#sqltuning#queryoptimization#subqueries

Rewriting SQL queries for Performance in 9 minutes

roughsealtd

4.7K subscribers • 1 video on Hallway

YouTube
  • Recommended by 1 person: 1 on Stack Overflow and its sister sites.

Intermediate. Assumes working knowledge.

83K views17 years ago1 recommendation

Databases and dataSQLPerformance and optimization

How to analyze and rewrite slow SQL subqueries into more efficient joins to improve database performance.

ENGLISH CAPTIONS AVAILABLE - SPANISH SUBTITLES - Full transcript (with some screenshots) available for a small fee at stores.lulu.com/konagora/. A brief example about how you can improve query performance by analyzing and rewriting queries.

Why it's here

1 person recommends this

What people say. Recommended for learning how to refactor nested SQL subqueries to resolve drastic performance slowdowns.

RolandoMySQLDBAon Database Administrators Stack Exchange • Recommends

On “MySQL subquery slows down drastically, but they work fine independently”

In Query 3, you are basically executing a subquery for every row of mybigtable against itself. To avoid this, you need to make two major changes: MAJOR CHANGE #1 : Refactor the Query Here is your original query Select count(*) as total from mybigtable where account_id=123 and email IN (select distinct email from mybigtable where account_id=345) You could try select count(*) EmailCount from ( select tbl123.email from (select email from mybigtable where account_id=123) tbl123 INNER JOIN (select distinct email from mybigtable where account_id=345) tbl345 using (email) ) A; or maybe the count per email select email,count(*) EmailCount from ( select tbl123.email from (sel…

RolandoMySQLDBAon Database Administrators Stack Exchange • Recommends

On “Unexpected extremely long query time (~5 minutes using nested WHEN-INs)”

Let's start with your original query SELECT * FROM reports WHERE report_name IN ( SELECT report_name FROM reports WHERE region IN ( SELECT distinct region FROM macroregions WHERE macroregion IN ( SELECT distinct macroregion FROM users WHERE callsign = '$thisuser' ) ) ) You can gather keys only (in stages), then join the keys with the reports table. Here is my new proposed query SELECT reports.* FROM ( SELECT rpts.report_name FROM ( SELECT DISTINCT regions.region FROM (SELECT DISTINCT macroregion FROM users WHERE callsign = '$thisuser') users INNER JOIN (SELECT DISTINCT macroregion,region FROM regions) regions USING (macroregion) ) regionkeys INNE…