fhg
Volume 9, Number 22 -- July 8, 2009

Subqueries vs. Joins

Published: July 8, 2009

Hey, Ted:

Please settle a disagreement. Another developer claims that it is bad to use an SQL subquery if the same results can be achieved with a join. He says that database management systems can optimize joins better than they can optimize subqueries, which means that joins run faster than subqueries. Is this true? I seem to have heard somewhere that the query engine converts a subquery to a join when possible.

--Dee


If the other developer is experienced with database management systems other than DB2, than he may be correct. However, when it comes to DB2 for i, you are correct.

For the benefit of other readers, let's first define the term subquery. A subquery is a query inside another query. Probably the most common place to put a subquery is in the WHERE clause of a SELECT statement, like this:

select cusnum, lstnam, city, state
  from qcustcdt
 where state in
        (select abbr
           from states
          where region = 'W')
 order by 1

The STATES table has three columns (fields):

  • Two-character state abbreviation
  • State name
  • Region code

The subquery (the inner query) builds a list of state abbreviations from the rows (records) that have a region code of "W". The outer query selects customers who live in the states in the list.

Sometimes a query with a join yields the same results.

select cus.cusnum, cus.lstnam, cus.city, cus.state
  from qcustcdt as cus
  join states as st
    on cus.state = st.abbr
 where st.region = 'W'
 order by 1

In this case, the system joins the two tables on common two-digit state abbreviation and selects the customers in region W. But both queries return the same data.

I sent Dee's question to Dan Cruikshank of IBM, and this is his response:

Both query optimizers (CQE and SQE) attempt to implement SQL subqueries as join composites (convert a subquery to a join.) This allows the optimizer (especially SQE) to take advantage of DB2 for i's extensive join technology (i.e., look-ahead predicate generation).

This does not mean that you should write joins instead of subqueries. That would not bode well for the many third-party SQL code generator products that use subqueries instead of joins. In fact, even though DB2 for i does not support updateable join syntax, the optimizer will attempt to convert a searched UPDATE containing scalar subselects, subqueries, or both to an updateable join under the covers.

Self-referencing UPDATEs (i.e., the table being updated is included as part of the subquery) may result in a copy being made of the table being updated. This would certainly cause an increase in memory and disk demand, although I would suspect that this is more of a one time only type scenario.

Because CQE has more limitations than SQE, IBM recommends avoiding the CQE optimizer if you can. Review the reason why the query was dispatched to the CQE optimizer and correct it. This information is captured by the database monitor and can be displayed using the Visual Explain tool or the System i Navigator Database Monitor Analysis dashboard.

The two most common reasons for CQE are:

  1. Specifying a DDS Logical File on the FROM clause of a query
  2. The presence of a select/omit logical file when the QAQQINI setting for IGNORE_DERIVED_INDEXES is *NO

The bottom line: if you like subqueries, use them.

--Ted




                     Post this story to del.icio.us
               Post this story to Digg
    Post this story to Slashdot


Sponsored By
HELP/SYSTEMS

                                                 SEQUEL
                                                 IBM® System i® Data Access Made Easy

                                              · Complete management access to critical data
                                              · Easy to use by IT and end users
                                              · Automated data access and display
                                              · Comprehensive BI package: reports, tables,
                                                 key performance indicators, and dashboards
                                              · System i-centric for real-time data analysis
                                              · Expert support and training

Click here for a FREE Information Kit!


Senior Technical Editor: Ted Holt
Technical Editor: Joe Hertvik
Contributing Technical Editors: Erwin Earley, Brian Kelly, Michael Sansoterra
Publisher and Advertising Director: Jenny Thomas
Advertising Sales Representative: Kim Reed
Contact the Editors: To contact anyone on the IT Jungle Team
Go to our contacts page and send us a message.

Sponsored Links

ARCAD Software:  FREE Webinar, July 15, Efficient and Secure Critical Data Change Management
10ZiG Technology:  BOSaNOVA, Inc. is now 10ZiG Technology
COMMON:  Celebrate our 50th anniversary at annual conference, May 2 - 6, 2010, in Orlando


 

IT Jungle Store Top Book Picks

Easy Steps to Internet Programming for AS/400, iSeries, and System i: List Price, $49.95
The iSeries Express Web Implementer's Guide: List Price, $49.95
The System i RPG & RPG IV Tutorial and Lab Exercises: List Price, $59.95
The System i Pocket RPG & RPG IV Guide: List Price, $69.95
The iSeries Pocket Database Guide: List Price, $59.00
The iSeries Pocket SQL Guide: List Price, $59.00
The iSeries Pocket Query Guide: List Price, $49.00
The iSeries Pocket WebFacing Primer: List Price, $39.00
Migrating to WebSphere Express for iSeries: List Price, $49.00
Getting Started With WebSphere Development Studio Client for iSeries: List Price, $89.00
Getting Started with WebSphere Express for iSeries: List Price, $49.00
Can the AS/400 Survive IBM?: List Price, $49.00
Chip Wars: List Price, $29.95


 
The Four Hundred
Midrange Shops Shift Priorities This Year

The Best of Times for IBM to Support All Its i Customers

What We Can Learn from iManifest

As I See It: Oh the Jobs They Are a-Changin'

Storage Hardware and Software Take Their Lumps in Q1

Four Hundred Stuff
Infor Sheds More Light on 'Flex' Upgrade and Migration Programs

Thales Key Manager Lowers Barriers to Encryption

iSecurity Experts: New Name, Familiar Face, Services a Priority

iBridge Traverses System i-Windows Divide for ACOM

IBM Lotus Brings Connections to the Cloud

Four Hundred Monitor
Four Hundred Monitor's
Full iSeries Events Calendar

System i PTF Guide
July 4, 2009: Volume 11, Number 27

June 27, 2009: Volume 11, Number 26

June 20, 2009: Volume 11, Number 25

June 13, 2009: Volume 11, Number 24

June 6, 2009: Volume 11, Number 23

May 30, 2009: Volume 11, Number 22

TPM at The Register
IBM finances vSphere-x64 server bundles

Niagara Falls to power next Yahoo! data centre

Dell cracks open EMEA PC services

US employers cut 467,000 jobs in June

Unisys to build its own stealthy cloud

Oracle slips notes to Wall Street

Canonical offers tech support for clouds

Sun's VirtualBox 3.0 exits betaland

Forrester re-slashes 2009 IT spending forecast

Cisco cuddles all clouds but one

Red Hat inks cloud partnership with Amazon

Sun hardens OpenSolaris for EC2

US IT staff salaries and benefits shrink

Bull waves red flag at HPC with blade supers

THIS ISSUE SPONSORED BY:

ProData Computer Services
Help/Systems
WorksRight Software


Printer Friendly Version


TABLE OF CONTENTS
Microsoft Virtualization for the i Guys, Revisited

Subqueries vs. Joins

Admin Alert: Fine Tuning User Access with Application Administration

Four Hundred Guru

BACK ISSUES

From the IT Jungle Forums
PHP CLI Call

Perl, PHP, and/or ZendCore

batch printing PDF files from RPG program

Using db2_connect in PHP on iSeries

How to return value from CL program?

ADO.NET/IBM.Data.DB2.iSeries/ iDB2Connection

Order by alias names




 
Subscription Information:
You can unsubscribe, change your email address, or sign up for any of IT Jungle's free e-newsletters through our Web site at http://www.itjungle.com/sub/subscribe.html.

Copyright © 1996-2009 Guild Companies, Inc. All Rights Reserved.
Guild Companies, Inc., 50 Park Terrace East, Suite 8F, New York, NY 10034

Privacy Statement