DB2 Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDB2 Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old December 5th, 2006, 03:11 AM
ad_rain ad_rain is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2006
Posts: 0 ad_rain User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 38 m 51 sec
Reputation Power: 0
Query Optimization with COALESCE function

Hi All,

I had this SQL query given below which was taking about 1,888,869.5 timerons as per the DB2 Access Plan.

---------------------------------------------------------
Original Query
---------------------------------------------------------
SELECT DISTINCT
G.DCACARLINECODE AS DCACARLINECODE,
H.DCAMARKETCODE AS DCAMARKETCODE,
D.DCACALYEAR AS DCACALENDARYEAR
FROM AWVPS105.DCATLOADPLTGRP AS A,
AWVPS105.DCARPBSPLT AS B,
AWVPS105.DCARPLTGRPLUKUP AS C,
AWVPS105.DCAMMYCYBKT AS D,
AWVPS105.DCARSTVAMEPLT AS E,
AWVPS105.DCARCLPBS AS G,
AWVPS105.DCARPBSMT AS H
WHERE A.DCAPLANNO = 3500
AND B.DCAPLANNO = A.DCAPLANNO
AND E.DCAPLANNO = B.DCAPLANNO
AND G.DCAPLANNO = E.DCAPLANNO
AND H.DCAPLANNO = G.DCAPLANNO
AND D.DCACYMYBUCKID = A.DCACYMYBUCKID
AND D.DCAMODELYEAR = B.DCAMODELYEAR
AND D.DCAMODELYEAR = H.DCAMODELYEAR
AND B.DCAPLTCODE = E.DCAPLTCODE
AND C.DCAPLANTDESC = E.DCAPLANTDESC
AND A.DCALOADPLTGRPID = C.DCALOADPLTGRPID
AND G.DCAPCBSCODE = B.DCAPCBSCODE
AND G.DCAPCBSCODE = H.DCAPCBSCODE
ORDER BY
G.DCACARLINECODE,
H.DCAMARKETCODE,
DCACALENDARYEAR
-----------------------------------------------------------

I modified the query as below which is taking about 1,178.4 timerons as per the DB2 Access Plan.

-----------------------------------------------------------
Modified query
-----------------------------------------------------------

SELECT DISTINCT
G.DCACARLINECODE AS DCACARLINECODE,
H.DCAMARKETCODE AS DCAMARKETCODE,
D.DCACALYEAR AS DCACALENDARYEAR
FROM AWVPS105.DCATLOADPLTGRP AS A,
AWVPS105.DCARPBSPLT AS B,
AWVPS105.DCARPLTGRPLUKUP AS C,
AWVPS105.DCAMMYCYBKT AS D,
AWVPS105.DCARSTVAMEPLT AS E,
AWVPS105.DCARCLPBS AS G,
AWVPS105.DCARPBSMT AS H
WHERE A.DCAPLANNO = 3500
AND B.DCAPLANNO = A.DCAPLANNO
AND E.DCAPLANNO = B.DCAPLANNO
AND G.DCAPLANNO = E.DCAPLANNO
AND H.DCAPLANNO = G.DCAPLANNO
AND D.DCACYMYBUCKID = A.DCACYMYBUCKID
AND D.DCAMODELYEAR = B.DCAMODELYEAR
AND D.DCAMODELYEAR = H.DCAMODELYEAR
AND B.DCAPLTCODE = E.DCAPLTCODE
AND C.DCAPLANTDESC = E.DCAPLANTDESC
AND A.DCALOADPLTGRPID = C.DCALOADPLTGRPID
AND G.DCAPCBSCODE = B.DCAPCBSCODE
AND G.DCAPCBSCODE = H.DCAPCBSCODE

AND H.DCAMARKETCODE = COALESCE(H.DCAMARKETCODE,H.DCAMARKETCODE)
AND C.DCALOADPLTGRPID = COALESCE(C.DCALOADPLTGRPID,C.DCALOADPLTGRPID)
AND G.DCACARLINECODE = COALESCE(G.DCACARLINECODE,G.DCACARLINECODE)
AND D.DCACALYEAR = COALESCE(D.DCACALYEAR,D.DCACALYEAR)

ORDER BY
G.DCACARLINECODE,
H.DCAMARKETCODE,
DCACALENDARYEAR
-----------------------------------------------------------

I have used the coalesce statements on columns which are being compared e.g. C.DCALOADPLTGRPID
and columns part of some primary key which are not being used in the query e.g. H.DCAMARKETCODE.

Could anyone explain to me with details why the modified query executes faster?

Thanks in Advance,
Adrain

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDB2 Development > Query Optimization with COALESCE function


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway