If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Which SQL is better on perfomance point of view in DB2 & how conditional where clause

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-13-08, 02:23
damodharan damodharan is offline
Registered User
 
Join Date: Jan 2008
Posts: 3
Question Which SQL is better on perfomance point of view in DB2 & how conditional where clause

Hi All,

I have two table. table details are

table one(Cust_det) has two fields (Cust_cin and cust_name).
table two(cust_add_det) has cust_cin and cust_address fields.

To join above two table which sql is better on perfomance point of view?
and how conditional where clause works?

select A.cust_cin,B.cust_address from Cust_det A,cust_add_det B where
A.Cust_cin = ? and A.cust_cin=B.cust_cin

or

select A.cust_cin,B.cust_address from Cust_det A,cust_add_det B where
A.cust_cin=B.cust_cin and A.Cust_cin = ?
Reply With Quote
  #2 (permalink)  
Old 01-13-08, 02:27
damodharan damodharan is offline
Registered User
 
Join Date: Jan 2008
Posts: 3
Question which sql is better on perfomance in db2 and how conditional where clause works?

Hi All,

I have two table in DB2 (ver 8). table details are

table one(Cust_det) has two fields (Cust_cin and cust_name).
table two(cust_add_det) has cust_cin and cust_address fields.

To join above two table which sql is better on perfomance point of view?
and how conditional where clause works?

select A.cust_cin,B.cust_address from Cust_det A,cust_add_det B where
A.Cust_cin = ? and A.cust_cin=B.cust_cin

or

select A.cust_cin,B.cust_address from Cust_det A,cust_add_det B where
A.cust_cin=B.cust_cin and A.Cust_cin = ?
Reply With Quote
  #3 (permalink)  
Old 01-13-08, 03:15
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
It makes no difference in what order your list the predicates in the WHERE clause. DB2 will figure out which predicate to evalute first (whether to do the join first, or filter the rows first).
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #4 (permalink)  
Old 01-13-08, 07:07
damodharan damodharan is offline
Registered User
 
Join Date: Jan 2008
Posts: 3
Thanks for your rely, Am using db2 version 8. whether DB2 will figure out which predicate to evalute first in version 8?. it seems db2 wont support this future in verision 8.

Also how conditional where clause works? in case absence of figure out which predicate to evalute first by db2?

As a part of Performance tuning we are changing the where clause order. it showing some difference in time(exec time) after modified.. i think db2 ver 8 wont support this? Please correct me if am wrong.
Reply With Quote
  #5 (permalink)  
Old 01-13-08, 12:54
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
You are wrong. No matter what version you have, DB2 will decide the order of predicate evaluation, regardless of the order or the predicates in the WHERE clause. The only exception might be if you changed the query optimization level (default is 5) but even then I doubt it would make a difference.

I don't know what you mean by conditional WHERE clause. I suspect the SQL statement you posted is not the real one, which may account for the problem.

The primary factors that affect the decision that DB2 makes about which predicate to evaluate first are:

1. Physical condition of the rows and indexes with regard to clustering, freespace, disorganization, etc. This can be changed with a reorg command.

2. The statistics that DB2 has regarding the number of rows and the items contained in number 1 above. This can can be updated with runstats command.

Also, if you have any outer joins, ORDER BY, GROUP BY, etc, these could affect predicate order evaluation.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #6 (permalink)  
Old 01-13-08, 15:30
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Quote:
Originally Posted by damodharan
select A.cust_cin,B.cust_address from Cust_det A, cust_add_det B
where A.Cust_cin = ? and A.cust_cin=B.cust_cin

or

select A.cust_cin,B.cust_address from Cust_det A,cust_add_det B
where A.cust_cin=B.cust_cin and A.Cust_cin = ?
Actually, DB2 will even apply what is called "transitive closure", by adding the condition
Code:
B.cust_cin = ?
so the above query is essentially rewritten into
Code:
SELECT A.cust_cin,B.cust_address
FROM   (SELECT cust_cin
        FROM   cust_det
        WHERE  cust_cin = ?) A,
       (SELECT cust_address
        FROM   cust_add_det
        WHERE  cust_cin = ?) B
WHERE  A.cust_cin=B.cust_cin
which is completely symmetric in tables A and B.
Of course, DB2 will decide on the join method and the two table accesses, based on presence of indexes and on statistics (like size of tables), hence the implementation will most often not be symmetric in the two tables.

If you are having performance issues, they will rather be solved by database changes (like adding indexes or running RUNSTATS) rather than rewriting the query.
Unless of course your query contains other ingredients than in the query above, like e.g. scalar functions in the WHERE conditions, in which case a query rewrite might be very necessary...
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On