473,387 Members | 1,502 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,387 software developers and data experts.

UNION ALL and OR Predicate optimising vastly differently ?

Hi,
(re: DB2 LUW v8.1 fp8 optimisation Level2)

Can anyone explain why the following difference have wildly different
plans - this seems very fundamental)

select p.part_number,
sp.supplier_part_number
from jabs.supplier_part sp
inner join jabs.part p on p.oid = sp.part_oid
where sp.supplier_part_number = 'LM339AN'

union all

select p.part_number,
sp.supplier_part_number
from jabs.supplier_part sp
inner join jabs.part p on p.oid = sp.part_oid
where p.part_number = 'LM339AN'

Optimises to a plan of cost = 110 (and runs in 100ms) (plan is index
scans & nested loop joins)

The equivelant (correct me if I'm wrong!)

select p.part_number,
sp.supplier_part_number
from jabs.supplier_part sp
inner join jabs.part p on p.oid = sp.part_oid
where
sp.supplier_part_number = 'LM339AN'
or
p.part_number = 'LM339AN'

optimises to a plan of cost = 340,000 (and takes minutes to run)(plan
is 2x full table access and hash join)

Why the big difference, aren't they equivalent ? - does this look like
an optimiser bug?

Nov 15 '06 #1
4 2487
PaulR wrote:
Hi,
(re: DB2 LUW v8.1 fp8 optimisation Level2)
Opt level 2 is below what is recommended for OLTP (we recommend 3).
The lower the opt level the less hard the optimizer will try
Increase the opt level to 3, 5, or 7 and see what it does.

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

WAIUG Conference
http://www.iiug.org/waiug/present/Fo...Forum2006.html
Nov 16 '06 #2
Hi, Paul.

Is it that you think that the rewrite facility should be smart enough
to see the equivalence and transform the OR into a UNION, or is it that
you think the access path should (be able to) include an index AND
across tables' indexes?

FWIW, I've seen the same behavior. Actually, it would be more accurate
to say that I'd taken advantage of it, when I was asked to tune a query
with an OR condition on columns from two different tables. The
(well-known) solution was to try converting the OR to UNION, which
worked well in my case.

I guess you're wondering why, if this kind of transformation is so
well-known, it wasn't done in this case?

--Jeff

PaulR wrote:
Hi,
(re: DB2 LUW v8.1 fp8 optimisation Level2)

Can anyone explain why the following difference have wildly different
plans - this seems very fundamental)

select p.part_number,
sp.supplier_part_number
from jabs.supplier_part sp
inner join jabs.part p on p.oid = sp.part_oid
where sp.supplier_part_number = 'LM339AN'

union all

select p.part_number,
sp.supplier_part_number
from jabs.supplier_part sp
inner join jabs.part p on p.oid = sp.part_oid
where p.part_number = 'LM339AN'

Optimises to a plan of cost = 110 (and runs in 100ms) (plan is index
scans & nested loop joins)

The equivelant (correct me if I'm wrong!)

select p.part_number,
sp.supplier_part_number
from jabs.supplier_part sp
inner join jabs.part p on p.oid = sp.part_oid
where
sp.supplier_part_number = 'LM339AN'
or
p.part_number = 'LM339AN'

optimises to a plan of cost = 340,000 (and takes minutes to run)(plan
is 2x full table access and hash join)

Why the big difference, aren't they equivalent ? - does this look like
an optimiser bug?
Nov 16 '06 #3
Thanks Serge,

I have tried Level3 - it does generate a "different" plan, but ...

cost = 388,000 execution=30+secs

i.e still not as good as the same UNION query where

cost=110 execution=200ms

We have found in our ("OLTP") environment at above Level 2, the
compilation
times are too long to be used. NB. although we are OLTP , we do have
many very complex queries/views - don't ask why ! :-)
Serge Rielau wrote:
PaulR wrote:
Hi,
(re: DB2 LUW v8.1 fp8 optimisation Level2)
Opt level 2 is below what is recommended for OLTP (we recommend 3).
The lower the opt level the less hard the optimizer will try
Increase the opt level to 3, 5, or 7 and see what it does.

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

WAIUG Conference
http://www.iiug.org/waiug/present/Fo...Forum2006.html
Nov 16 '06 #4
Hi, thanks for your reply.
jefftyzzer wrote:
Hi, Paul.

Is it that you think that the rewrite facility should be smart enough
to see the equivalence and transform the OR into a UNION, or is it that
you think the access path should (be able to) include an index AND
across tables' indexes?
PRI don't really mind if it rewrites or not, but I would expect
it to find the
PRmuch lower cost plan, it's not a complicated query.
FWIW, I've seen the same behavior. Actually, it would be more accurate
to say that I'd taken advantage of it, when I was asked to tune a query
with an OR condition on columns from two different tables. The
(well-known) solution was to try converting the OR to UNION, which
worked well in my case.

I guess you're wondering why, if this kind of transformation is so
well-known, it wasn't done in this case?
PRYes, I'm just amazed the optimizer hasn't coped well with such
a simple
PRquery. I have raised a call with IBM, I'm hoping there is
fixpak fix.
>
--Jeff

PaulR wrote:
Hi,
(re: DB2 LUW v8.1 fp8 optimisation Level2)

Can anyone explain why the following difference have wildly different
plans - this seems very fundamental)

select p.part_number,
sp.supplier_part_number
from jabs.supplier_part sp
inner join jabs.part p on p.oid = sp.part_oid
where sp.supplier_part_number = 'LM339AN'

union all

select p.part_number,
sp.supplier_part_number
from jabs.supplier_part sp
inner join jabs.part p on p.oid = sp.part_oid
where p.part_number = 'LM339AN'

Optimises to a plan of cost = 110 (and runs in 100ms) (plan is index
scans & nested loop joins)

The equivelant (correct me if I'm wrong!)

select p.part_number,
sp.supplier_part_number
from jabs.supplier_part sp
inner join jabs.part p on p.oid = sp.part_oid
where
sp.supplier_part_number = 'LM339AN'
or
p.part_number = 'LM339AN'

optimises to a plan of cost = 340,000 (and takes minutes to run)(plan
is 2x full table access and hash join)

Why the big difference, aren't they equivalent ? - does this look like
an optimiser bug?
Nov 16 '06 #5

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

Similar topics

2
by: Susanne Bandi | last post by:
Hello I've experienced that DB2 unfortunately does not eliminate subselects of a UnionAll-View if the predicate's content is not hardcoded but derived from a base-table with a noncorrelated...
12
by: Susan Bricker | last post by:
For those of you who have been following my posts - they all pertain to a Dog Competition Organization's Database. There are three classes that the dogs can participate: NOVICE, OPEN, and...
56
by: ccwork | last post by:
Hi all, Here is a sample code segment: .... typedef PACKED struct { union { PACKED struct { char red:1;
1
by: Alex Satrapa | last post by:
Is there any (simple? har!) way to optimise a particular SQL query? At this stage, I'm more interested in making the query more readable, so I've started hunting down references about relational...
5
by: BillCo | last post by:
I'm having a problem with a union query, two simple queries joined with a union statement. It's created in code based on parameters. Users were noticing some inconsistant data and when I analysed...
50
by: Mikhail Teterin | last post by:
Hello! The sample program below is compiled fine by gcc (with -Wall), but rejected by Sun's SUNWspro compiler (version 6 update 2). The point of contention is, whether a value for one of the...
7
by: Duncan Smith | last post by:
Suppose I have the following XML doc: <root> <tag id="1"> <number value="1"/> </tag> <tag id="2"> <something/> </tag> <tag id="3">
5
by: wugon.net | last post by:
question: db2 LUW V8 UNION ALL with table function month() have bad query performance Env: db2 LUW V8 + FP14 Problem : We have history data from 2005/01/01 ~ 2007/05/xx in single big...
18
by: Bryan Parkoff | last post by:
I hate using struct / union with dot between two words. How can I use one word instead of two words because I want the source code look reading clear. three variables are shared inside one...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.