473,698 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_par t_number
from jabs.supplier_p art sp
inner join jabs.part p on p.oid = sp.part_oid
where sp.supplier_par t_number = 'LM339AN'

union all

select p.part_number,
sp.supplier_par t_number
from jabs.supplier_p art 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_par t_number
from jabs.supplier_p art sp
inner join jabs.part p on p.oid = sp.part_oid
where
sp.supplier_par t_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 2505
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_par t_number
from jabs.supplier_p art sp
inner join jabs.part p on p.oid = sp.part_oid
where sp.supplier_par t_number = 'LM339AN'

union all

select p.part_number,
sp.supplier_par t_number
from jabs.supplier_p art 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_par t_number
from jabs.supplier_p art sp
inner join jabs.part p on p.oid = sp.part_oid
where
sp.supplier_par t_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+se cs

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_par t_number
from jabs.supplier_p art sp
inner join jabs.part p on p.oid = sp.part_oid
where sp.supplier_par t_number = 'LM339AN'

union all

select p.part_number,
sp.supplier_par t_number
from jabs.supplier_p art 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_par t_number
from jabs.supplier_p art sp
inner join jabs.part p on p.oid = sp.part_oid
where
sp.supplier_par t_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
1827
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 Subselect. Any plans to enhance this for the future? Any workaround suggestions? Thanks in advance Susanne
12
2905
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 UTILITY. I want to produce a report of the top 10 average scores for each class for each year.
56
10447
by: ccwork | last post by:
Hi all, Here is a sample code segment: .... typedef PACKED struct { union { PACKED struct { char red:1;
1
2947
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 theory to see if I can use relational algebra to manipulate the query into a "shorter" form (I believe the mathematical term is "simplify"). Since I've got somewhere between two days and two lifetimes' worth of reading ahead of me, would it be...
5
2279
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 the query produced and opened it from a MS Query it started giving strange results. The first query when run alone returns 22 records, some of which have identical values in all fields. This is 100% correct. The second query returns nothing....
50
6485
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 union's types can be passed to a function directly -- without creating a separate variable of the union type and assigning the appropriate field of it. Is gcc being too liberal, or is this behavior simply part of a newer
7
1725
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
3843
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 table, we try separate this big table into twelve tables and create a view
18
6078
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 variable. I manipulate to change 8-bit data before it causes to change 16-bit data and 32-bit data. For example. union {
0
8674
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8604
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,...
0
9157
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9028
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8895
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
7728
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...
0
5860
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
4369
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3046
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

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.