473,654 Members | 3,071 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5095
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(correlatio n 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******** *************@o 13g2000cwo.goog legroups.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(correlatio n 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******** *************@o 13g2000cwo.goog legroups.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
563
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 parent query, the subquery is inlined for each one. This means multiple redundant executions of the subquery. I recall there was a way to defeat this optimization involving introducing an extra subquery layer somewhere. But I'm failing to be able...
8
19588
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 couple of tables in my database using INNER JOINS and the WHERE clause to specify the required constraints. However, I also want to read two fields from a *single* record from a table called 'Locations' and then apply one of these field's values...
7
2365
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 which a part may be composed. I have a table of parts and their subparts. The problem is that each of those subparts may be composed of smaller component parts. The subpart would then be listed in the Part field linked to each of its subparts in...
5
6580
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 following SQL statement. INSERT INTO OrderItems (ClientID, ProductID, OrderHeaderID, Quantity, Dispatched, BackOrdered) SELECT ClientID, ProductID, 1371 AS OrderHeaderID, Quantity, Dispatched, BackOrdered FROM Basket WHERE RequisitionID = 1369 The...
22
3344
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 (subquery)'. PG always seems to want to do a sequential scan of the bar table. I wish there were a way of telling PG, "use the index on baz in your plan, because I know that the subquery will return very few results". Where it really matters, I have...
3
2467
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 System.Xml.Serialization, i think that can solve my problem. But i tried using the Deserialize method for converting the XML to the Corresponding Object, neither i get error nor i get any luck for converting it to Object.
11
3213
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). This comes up as too complex using nested IIf, then, else. I tried a lookup table but here the problem I found was that unless the average test score matches exactly then no grade is returned. For example a score of 11.0 returns grade 4b but a...
32
12504
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 Set wsName = DBEngine.Workspaces(0) Set dbName = wsName.OpenDatabase(CurrentProject.FullName) Set rsName = dbName.OpenRecordset("SQL Statement")
5
8025
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 in 'dbo.sourceTable'. It does, however, certainly exist in 'dbo.myTable'. Breaking the statement down, we have:
1
4153
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 allows me to filter based on variables in a single table. I would like to have a search form where I can select multiple variables (from various linked tables) to filter by, and return results based on this multi-table filter. Allen Browne...
0
8296
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8497
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8598
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7310
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5627
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4299
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2721
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1928
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.