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 > Rewrite of a nested, nested,nested subselect

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-27-11, 16:55
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
Rewrite of a nested, nested,nested subselect

I was wondering if someone might have some ideas on how to rewrite this subselect, so it is a bit easier to understand. I have already told the developers to remove the GROUP BY as its unnecessary.
Code:
select a bunch of columns
   from some tables
where B_SYS_ID IN (select B_SYS_ID 
                     from B_XREF_TB
                   WHERE B_ALT_ID IN (select B_ALT_ID
                                        from B_XREF_TB
                                      WHERE B_SYS_ID IN (select B_SYS_ID
                                                           from B_XREF_TB
                                                         WHERE B_ALT_ID = '948797930L')
                                      )
                   GROUP BY B_SYS_ID)
Dave Nance
Reply With Quote
  #2 (permalink)  
Old 09-27-11, 16:57
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
I forgot to mention, that portion of the query is the only indexable input for the query.
Dave Nance
Reply With Quote
  #3 (permalink)  
Old 09-27-11, 19:45
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Are there any unique column?

For example:
1) b_sys_id in b_xref_tb
2) b_alt_id in b_xref_tb
3) b_sys_id in one of outmost tables
Reply With Quote
  #4 (permalink)  
Old 09-28-11, 08:23
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
no. there is a table where b_sys_id is unique, but it is not part of the query right now. The idea is that the b_alt_id, is mostly unique, but it is not enforced by the database. The primary key of the B_XREF_TB is the combination of the two, B_SYS_ID, B_ALT_ID. I do have another index on the table that is the clustering index B_ALT_ID, B_SYS_ID as that is the way the table is most commonly queried.

My original thought when just the subselect was sent to me was to replace it with an exists clause, until I got the rest of the query and saw that it is the only indexable input. That section of the query on its own, runs in a respectable amount of time, .23 seconds elapsed.
Other pieces of info I should have sent earlier. The database is on Z/OS, DB2 V9. Query is dynamic from a workstation, using DB2 Connect gateway.
Dave Nance
Reply With Quote
  #5 (permalink)  
Old 09-28-11, 08:37
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
I think the query can be rewritten like the following.

Wheather it may perform well or not, it is easier to understand(at least for me).
Indexes (b_sys_id , b_alt_id) and (b_alt_id , b_sys_id) on B_XREF_TB might be usefull.

Code:
select a bunch of columns
 from  some tables
 where B_SYS_ID
       IN (select x3.B_SYS_ID 
            from  B_XREF_TB x1
            INNER JOIN
                  B_XREF_TB x2
              ON  x2.B_SYS_ID = x1.B_SYS_ID
            INNER JOIN
                  B_XREF_TB x3
              ON  x3.B_ALT_ID = x2.B_ALT_ID
            WHERE x1.B_ALT_ID = '948797930L'
          )
;
Reply With Quote
  #6 (permalink)  
Old 09-28-11, 09:18
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
Tonkuma,
Thanks. That is a bit easier to comprehend and performance is, exactly the same.

Dave Nance
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