473,385 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Converting a correlated subquery to a non-correlated one

Hi!

Does anyone here know of a way to goad DB2 into converting a correlated
subquery to a non-correlated one? Does DB2 ever do such a conversion?

We have a query of the form
SELECT .. FROM A
WHERE EXISTS (SELECT 'X' FROM B, C
WHERE B.COL1 = A.COL1
AND B.COL2 = A.COL2
AND [ predicates that join B and C ]
AND C.COLP = 'LIT1'
AND C.COLQ = 'LIT2'
)

The access path for this involves a tablespace scan of A, and for each
row retrieved, execution of the subquery, which involves accessing B
first, and C next, and doing a nested loop join between them.

For this specific query, the most efficient order of table access
happens to be C first, B next, then A. The predicates on C.COLP and
C.COLQ eliminate most of the rows. Unfortunately, there doesn't appear
to be a way to get DB2 to choose this table order. I collected all the
detailed statistics that visual explain asked for, but DB2 doesn't
change its access path.

The query performs much better (1 second as opposed to 3 mins) when it
is rewritten this way:

SELECT ... FROM A
WHERE (COL1, COL2) IN (SELECT B.COL1, B.COL2
FROM B, C
WHERE [ predicates joining B and C ]
AND C.COLP = 'LIT1' AND C.COLQ = 'LIT2'
)

The subquery is evaulated only once: C is accessed first, and is joined
with B.

Is there a way to get DB2 to transform a correlated subquery to a
non-correlated one by itself?

Thanks and Regards,
Venkata

Nov 12 '05 #1
8 5076
Did you look at changing the optimization level? You also didn't state
what level you are presently using.

Phil Sherman

Venkata C wrote:
Hi!

Does anyone here know of a way to goad DB2 into converting a correlated
subquery to a non-correlated one? Does DB2 ever do such a conversion?

We have a query of the form
SELECT .. FROM A
WHERE EXISTS (SELECT 'X' FROM B, C
WHERE B.COL1 = A.COL1
AND B.COL2 = A.COL2
AND [ predicates that join B and C ]
AND C.COLP = 'LIT1'
AND C.COLQ = 'LIT2'
)

The access path for this involves a tablespace scan of A, and for each
row retrieved, execution of the subquery, which involves accessing B
first, and C next, and doing a nested loop join between them.

For this specific query, the most efficient order of table access
happens to be C first, B next, then A. The predicates on C.COLP and
C.COLQ eliminate most of the rows. Unfortunately, there doesn't appear
to be a way to get DB2 to choose this table order. I collected all the
detailed statistics that visual explain asked for, but DB2 doesn't
change its access path.

The query performs much better (1 second as opposed to 3 mins) when it
is rewritten this way:

SELECT ... FROM A
WHERE (COL1, COL2) IN (SELECT B.COL1, B.COL2
FROM B, C
WHERE [ predicates joining B and C ]
AND C.COLP = 'LIT1' AND C.COLQ = 'LIT2'
)

The subquery is evaulated only once: C is accessed first, and is joined
with B.

Is there a way to get DB2 to transform a correlated subquery to a
non-correlated one by itself?

Thanks and Regards,
Venkata


Nov 12 '05 #2
I apologize for not mentioning that we are running this query on DB2
for z/OS. Optimization level doesn't appear to be available as an
option on DB2 for z/OS.

Thanks,
Venkata

Nov 12 '05 #3
If there is an index (COL1, COL2) on table A.
This(correlation in FROM clause) might be another way:
SELECT A1.*
FROM (SELECT B.COL1, B.COL2
FROM B, C
WHERE [ predicates that join B and C ]
AND C.COLP = 'LIT1'
AND C.COLQ = 'LIT2'
) B1
, TABLE
(SELECT ...
FROM A
WHERE B1.COL1 = A.COL1
AND B1.COL2 = A.COL2
) A1

Nov 12 '05 #4
Assuming statistics are accurate, etc, I would suggest opening a performance
PMR.

"Venkata C" <ve*************@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
Hi!

Does anyone here know of a way to goad DB2 into converting a correlated
subquery to a non-correlated one? Does DB2 ever do such a conversion?

We have a query of the form
SELECT .. FROM A
WHERE EXISTS (SELECT 'X' FROM B, C
WHERE B.COL1 = A.COL1
AND B.COL2 = A.COL2
AND [ predicates that join B and C ]
AND C.COLP = 'LIT1'
AND C.COLQ = 'LIT2'
)

The access path for this involves a tablespace scan of A, and for each
row retrieved, execution of the subquery, which involves accessing B
first, and C next, and doing a nested loop join between them.

For this specific query, the most efficient order of table access
happens to be C first, B next, then A. The predicates on C.COLP and
C.COLQ eliminate most of the rows. Unfortunately, there doesn't appear
to be a way to get DB2 to choose this table order. I collected all the
detailed statistics that visual explain asked for, but DB2 doesn't
change its access path.

The query performs much better (1 second as opposed to 3 mins) when it
is rewritten this way:

SELECT ... FROM A
WHERE (COL1, COL2) IN (SELECT B.COL1, B.COL2
FROM B, C
WHERE [ predicates joining B and C ]
AND C.COLP = 'LIT1' AND C.COLQ = 'LIT2'
)

The subquery is evaulated only once: C is accessed first, and is joined
with B.

Is there a way to get DB2 to transform a correlated subquery to a
non-correlated one by itself?

Thanks and Regards,
Venkata

Nov 12 '05 #5

Tonkuma wrote:
If there is an index (COL1, COL2) on table A.
This(correlation in FROM clause) might be another way:
SELECT A1.*
FROM (SELECT B.COL1, B.COL2
FROM B, C
WHERE [ predicates that join B and C ]
AND C.COLP = 'LIT1'
AND C.COLQ = 'LIT2'
) B1
, TABLE
(SELECT ...
FROM A
WHERE B1.COL1 = A.COL1
AND B1.COL2 = A.COL2
) A1


That was a beautifully written query, though it needs the A(col1, col2)
index to be a unique one.

Now, if only I can get DB2 to do this transformation by itself!!
Unfortunately, the original query text, coming from PeopleSoft, cannot
be changed.

Thanks,
Venkata

Nov 12 '05 #6

Mark Yudkin wrote:
Assuming statistics are accurate, etc, I would suggest opening a performance
PMR.

"Venkata C" <ve*************@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
Hi!

Does anyone here know of a way to goad DB2 into converting a correlated
subquery to a non-correlated one? Does DB2 ever do such a conversion?

We have a query of the form
SELECT .. FROM A
WHERE EXISTS (SELECT 'X' FROM B, C
WHERE B.COL1 = A.COL1
AND B.COL2 = A.COL2
AND [ predicates that join B and C ]
AND C.COLP = 'LIT1'
AND C.COLQ = 'LIT2'
)

The access path for this involves a tablespace scan of A, and for each
row retrieved, execution of the subquery, which involves accessing B
first, and C next, and doing a nested loop join between them.

For this specific query, the most efficient order of table access
happens to be C first, B next, then A. The predicates on C.COLP and
C.COLQ eliminate most of the rows. Unfortunately, there doesn't appear
to be a way to get DB2 to choose this table order. I collected all the
detailed statistics that visual explain asked for, but DB2 doesn't
change its access path.

The query performs much better (1 second as opposed to 3 mins) when it
is rewritten this way:

SELECT ... FROM A
WHERE (COL1, COL2) IN (SELECT B.COL1, B.COL2
FROM B, C
WHERE [ predicates joining B and C ]
AND C.COLP = 'LIT1' AND C.COLQ = 'LIT2'
)

The subquery is evaulated only once: C is accessed first, and is joined
with B.

Is there a way to get DB2 to transform a correlated subquery to a
non-correlated one by itself?

Thanks and Regards,
Venkata


We will probably do that. Thank you.

Venkata

Nov 12 '05 #7
I think that the A(col1, col2) index don't need to be a unique one.
In your original query, if there are multiple rows in table A of which
(COL1, COL2) values are same, all such rows will be selected.
But, if (B.COL1, B.COL2) is not unique, DISTINCT should be specified in
first subquery in my example.

SELECT A1.*
FROM (SELECT DISTINCT B.COL1, B.COL2
FROM B, C
WHERE [ predicates that join B and C ]
AND C.COLP = 'LIT1'
AND C.COLQ = 'LIT2'
) B1
, TABLE
(SELECT ...
FROM A
WHERE B1.COL1 = A.COL1
AND B1.COL2 = A.COL2
) A1

Nov 12 '05 #8
You are correct. My mistake.

I'm going to safe keep your version as an example of forcing join order
on DB2. Thank you.

Nov 12 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Greg Stark | last post by:
Postgresql 7.4b2 (approximately, compiled out of CVS) When I have a subquery that has a complex subquery as one of the result columns, and then that result column is used multiple times in the...
8
by: Andrew McNab | last post by:
Hi folks, I have a problem with an MS Access SQL query which is being used in an Access Report, and am wondering if anyone can help. Basically, my query (shown below) gets some records from a...
7
by: K. Crothers | last post by:
I administer a mechanical engineering database. I need to build a query which uses the results from a subquery as its input or criterion. I am attempting to find all of the component parts of...
5
by: Rod | last post by:
I have a client site where the code below has been working happily for at least four months. The site is using SQL Server 7. The code is ASP.NET Last week an error appeared related to the...
22
by: Kevin Murphy | last post by:
I'm using PG 7.4.3 on Mac OS X. I am disappointed with the performance of queries like 'select foo from bar where baz in (subquery)', or updates like 'update bar set foo = 2 where baz in...
3
by: Parvesh | last post by:
hi, I am using a webservice which Returns the Result in an XML string, The XML response i get i svery cumbersome to parse, But if i could convert it to the Corresponding Class using the...
11
by: Penfold | last post by:
I'd appreciate help converting student average test scores into grades. My problem is that I need to allocate one of about 20 grades (3a,3b,3c,4a,4b,4c etc through to 8c plus a couple of others)....
32
by: robert d via AccessMonster.com | last post by:
I'm looking at converting DAO to ADO in my app. All of my DAO connections are of the following structure: Dim wsName As DAO.Workspace Dim dbName As DAO.Database Dim rsName As DAO.Recordset ...
5
by: Anne | last post by:
Hello! Here is the statement in question: --STATEMENT A SELECT * FROM dbo.myTable WHERE colX in (SELECT colX FROM dbo.sourceTable) The problem with Statement A is that 'colX' does not exist...
1
by: jcf378 | last post by:
Hi all-- Does anyone have any insight as to how I might create a search form that allows a user to select criteria based on any related table in the whole database. The search form I have now only...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.