473,480 Members | 1,737 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Help performace question DB2 UDB v 8.1.9 Linux

The following query runs very slowly compared to other queries on my
system. If I read the explain plan and its data correctly, it is due to
a table scan on animals caused by the join separated from the others by
space. There is an index on both dam_bhid and recip_bhid. I have tried
converting the on condition to a case statement to no good effect.
Likewise reordering the conditions. Do you have any suggestions.

The semantics of the join is that an1 has a calf on the ground if there
is a calf who she is the dam of when there is no recipient for that calf
or there is a calf of which she was the recipient. A calf is less than
283 days old and unweaned.

select
an1.bhid,
an1.breed_1,
an1.pct_1,
ap_donor.herd_id as donor_id,
ap_sire.herd_id as sire_id,
ap_dam.herd_id as dam_id,
loc.namex as loc_name,
lh.start_date as as_of_date,
case
when wn.WEIGH_DATE is null
then ap_calf.herd_id
else ''
end AS calf_id,
case
when wn.WEIGH_DATE is null
then char(an_calf.BIRTH_DATE, usa)
else ''
end AS calf_birth_date,
case
when wn.weigh_date is null
then an_calf.sex
else ''
end AS calf_sex,
ap_calf_sire.herd_id as calf_sire_id,
col_def.COLOR_DESC as calf_color,
char((select max(act_recov_date)
from $schema.flushes
WHERE donor_bhid=an1.bhid), usa)
AS last_flush_date,
char(proj_mate.BREED_TO_DATE, usa)as proj_breed_date,
proj_mate_detail.herd_id as proj_breed_mate

from $schema.animals an1
left outer
join $schema.ANIMALS_PRIV ap_donor
on ap_donor.bhid = an1.BHID
and ap_donor.herd_owner_id = $empid
left outer
join $schema.animals_priv ap_sire
on ap_sire.bhid = an1.SIRE_BHID
and ap_sire.herd_owner_id = $empid
left outer
join $schema.ANIMALS_PRIV ap_dam
on ap_dam.bhid = an1.DAM_BHID
and ap_dam.herd_owner_id = $empid
left outer
join $schema.locations_hist lh
on an1.bhid = lh.bhid
and lh.end_date is null
left outer
join $schema.locations loc
on loc.loc_id = lh.loc_id

-- The problem is here

left outer
join $schema.animals an_calf
on days(current_date) -
days(an_calf.birth_date) < 280
and an_calf.ACTIVEX = 'Y'
and ((an_calf.dam_bhid = an1.bhid AND
an_calf.recip_bhid = 0)
OR an_calf.RECIP_BHID = an1.bhid)

-- end problem

left outer
join $schema.animals_priv ap_calf
on ap_calf.HERD_OWNER_ID = $empid
and ap_calf.bhid = an_calf.bhid
left outer
join $schema.weaning wn
on wn.bhid = an_calf.bhid
left outer
join $schema.ANIMALS_PRIV ap_calf_sire
on an_calf.SIRE_BHID=ap_calf_sire.bhid
and ap_calf_sire.herd_owner_id = $empid
left outer
join $schema.COLOR_DEFN col_def
on col_def.BH_COLOR_CODE=an_calf.COLOR
left outer
join $schema.ANIMALS_PRIV_$empid proj_mate
on proj_mate.bhid = an1.bhid
left outer
join $schema.ANIMALS_PRIV_$empid proj_mate_detail
on proj_mate_detail.BHID=
proj_mate.BREED_TO_BHID
where
an1.BHID in
(select bhid
from $schema.animal_sets
where set_name = '$setname'
and userid='$setuser')
Apr 11 '06 #1
5 1440
Bob Stearns wrote:
left outer
join $schema.animals an_calf
on days(current_date) -
days(an_calf.birth_date) < 280
and an_calf.ACTIVEX = 'Y'
and ((an_calf.dam_bhid = an1.bhid AND
an_calf.recip_bhid = 0)
OR an_calf.RECIP_BHID = an1.bhid)

There are two potential problems. The first is the OR which pretty much
makes teh last AND predicate useless for index exploitation.
The second is the expression on days which disallows index exploitation
on birth_date and, worse, forces an NLJOIN.
What about a small rewrite:
an_calf.birthdate > current date - 280 days
Now DB2 has a clear startkey for an index scan over birth date.

Cheers
Serge

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Apr 12 '06 #2
Serge Rielau wrote:
Bob Stearns wrote:
left outer
join $schema.animals an_calf
on days(current_date) -
days(an_calf.birth_date) < 280
and an_calf.ACTIVEX = 'Y'
and ((an_calf.dam_bhid = an1.bhid AND
an_calf.recip_bhid = 0)
OR an_calf.RECIP_BHID = an1.bhid)


There are two potential problems. The first is the OR which pretty much
makes teh last AND predicate useless for index exploitation.
The second is the expression on days which disallows index exploitation
on birth_date and, worse, forces an NLJOIN.
What about a small rewrite:
an_calf.birthdate > current date - 280 days
Now DB2 has a clear startkey for an index scan over birth date.

Cheers
Serge

Thank you. Creating the index and changing the query as you indicated
changed the time from over a minute to less than 2 seconds.

How could I convince the optimizer to do what I would do in "procedural
code", which uses the dam_bhid and recip_bhid indices to minimize the
time required (in no actual language that I know of):
found = -1
while (SELECT * FROM animals WHERE dam_bhid=an1.bhid) {
if(recip_bhid=0} continue
if(activex<>'Y') continue
if(birth_date < current_date-280 days) continue
found = bhid
break
}
if(found=-1) {
while (SELECT * FROM animals WHERE recip_bhid=an1.bhid) {
if(activex<>'Y') continue
if(birth_date < current_date-280 days) continue
found = bhid
break
}
}
if(found=-1) {nomatch)
else (matched the current record, with bhid=found}
Apr 12 '06 #3
Bob Stearns wrote:
Serge Rielau wrote:
Bob Stearns wrote:
left outer
join $schema.animals an_calf
on days(current_date) -
days(an_calf.birth_date) < 280
and an_calf.ACTIVEX = 'Y'
and ((an_calf.dam_bhid = an1.bhid AND
an_calf.recip_bhid = 0)
OR an_calf.RECIP_BHID = an1.bhid)
There are two potential problems. The first is the OR which pretty
much makes teh last AND predicate useless for index exploitation.
The second is the expression on days which disallows index
exploitation on birth_date and, worse, forces an NLJOIN.
What about a small rewrite:
an_calf.birthdate > current date - 280 days
Now DB2 has a clear startkey for an index scan over birth date.

Cheers
Serge

Thank you. Creating the index and changing the query as you indicated
changed the time from over a minute to less than 2 seconds.

How could I convince the optimizer to do what I would do in "procedural
code", which uses the dam_bhid and recip_bhid indices to minimize the
time required (in no actual language that I know of):


CREATE FUNCTION purplecow(...) RETURNS VARCHAR(10)
READS SQL DATA
BEGIN ATOMIC found = -1 DECLARE found INTEGER; while (SELECT * FROM animals WHERE dam_bhid=an1.bhid) {
if(recip_bhid=0} continue
if(activex<>'Y') continue
if(birth_date < current_date-280 days) continue
found = bhid
break
} SET found = (SELECT bhid FROM animals
WHERE dam_bhid=an1.bhid
AND recip_bhid <> 0
AND activex='Y'
AND birth_date >= current_date-280 days
FETCH FIRST ROW ONLY) if(found=-1) {
while (SELECT * FROM animals WHERE recip_bhid=an1.bhid) {
if(activex<>'Y') continue
if(birth_date < current_date-280 days) continue
found = bhid
break
}
} SET found = (SELECT bhid FROM animals
WHERE recip_bhid=an1.bhid
AND activex = 'Y'
AND birth_date >= current_date - 280 days
AND found IS NULL
FETCH FIRST ROW ONLY); if(found=-1) {nomatch)
else (matched the current record, with bhid=found}

RETURN CASE WHEN found IS NULL THEN 'No match'
ELSE CHAR(found) END;
END
@

VALUES purplecow();

If you want to take the optimizer out of the equation SQL Functions
rock. Need to be careful with DPF, but in serial they contribute their
fair share to our TPC-C results.

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Apr 12 '06 #4
Serge Rielau wrote:

CREATE FUNCTION purplecow(...) RETURNS VARCHAR(10)
READS SQL DATA
BEGIN ATOMIC

DECLARE found INTEGER;

SET found = (SELECT bhid FROM animals
WHERE dam_bhid=an1.bhid
AND recip_bhid <> 0
AND activex='Y'
AND birth_date >= current_date-280 days
FETCH FIRST ROW ONLY)

SET found = (SELECT bhid FROM animals
WHERE recip_bhid=an1.bhid
AND activex = 'Y'
AND birth_date >= current_date - 280 days
AND found IS NULL
FETCH FIRST ROW ONLY);

RETURN CASE WHEN found IS NULL THEN 'No match'
ELSE CHAR(found) END;
END
@

VALUES purplecow();

If you want to take the optimizer out of the equation SQL Functions
rock. Need to be careful with DPF, but in serial they contribute their
fair share to our TPC-C results.

Cheers
Serge

Could I easily return the entire row found rather than its key? It would
dave 1 join. How do I use it in the middle of a long JOIN sequence?

from an1
..
..
..
left outer join values purplecow(an1.bhid)
as tx(bhid)
on tx.bhid is not null
left outer join animals an_calf
on an_calf.bhid=tx.bhid
..
..
..

is my best guess, but I'm sure that's not quite right
Apr 13 '06 #5
Bob Stearns wrote:
Serge Rielau wrote:

CREATE FUNCTION purplecow(...) RETURNS VARCHAR(10)
READS SQL DATA
BEGIN ATOMIC

DECLARE found INTEGER;

SET found = (SELECT bhid FROM animals
WHERE dam_bhid=an1.bhid
AND recip_bhid <> 0
AND activex='Y'
AND birth_date >= current_date-280 days
FETCH FIRST ROW ONLY)

SET found = (SELECT bhid FROM animals
WHERE recip_bhid=an1.bhid
AND activex = 'Y'
AND birth_date >= current_date - 280 days
AND found IS NULL
FETCH FIRST ROW ONLY);

RETURN CASE WHEN found IS NULL THEN 'No match'
ELSE CHAR(found) END;
END
@

VALUES purplecow();

If you want to take the optimizer out of the equation SQL Functions
rock. Need to be careful with DPF, but in serial they contribute their
fair share to our TPC-C results.

Cheers
Serge

Could I easily return the entire row found rather than its key? It would
dave 1 join. How do I use it in the middle of a long JOIN sequence?

from an1
.
.
.
left outer join values purplecow(an1.bhid)
as tx(bhid)
on tx.bhid is not null
left outer join animals an_calf
on an_calf.bhid=tx.bhid
.
.
.

is my best guess, but I'm sure that's not quite right

If you want to return the entire row you sue a TABLE function.
Simply change the function to:
RETURNS TABLE (pk INT, C1 INT, ...)

and the return to:
RETURN VALUES (......);
...
of course you will also have to extend the SET statements
SET (...) = (SELECT ....)

take a look here for examples:
http://www-128.ibm.com/developerwork...dm-0411rielau/

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Apr 13 '06 #6

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

Similar topics

1
3714
by: chris | last post by:
Does anyone have any tips for comparing large amounts of data? The data in question is around 1000 lines of up to 400 delimited key value pairs. Each line has a unique identifier stored in one of...
3
1513
by: TPJ | last post by:
"The advantage of xrange() over range() is minimal (since xrange() still has to create the values when asked for them) except when a very large range is used on a memory-starved machine or when all...
5
1523
by: Lada 'Ray' Lostak | last post by:
Dear list, First of all I want to say sory, if my question was answered somewhere. Or if it is my fault. If so, please, give me link/hint. My own search fails :( I have experimence with MySql,...
4
1579
by: Lan W via DBMonster.com | last post by:
Hi, How to change the Db2 server's IP address? I have DB2 UDB 7.1 installed on AIX 5.2 machine and DB2 UDB 8.1 installed on Linux machine. Thanks! Lan --
7
1256
by: trialproduct2004 | last post by:
Hi all I am having application which has three classes. in one class say class1, i am having arraylist where i am adding and removing string. there is one method in class1 which is creating...
19
2249
by: Tony Johansson | last post by:
Hello!! Which have best performance between i++ or ++i. Is it exact the same or is it some very small difference in performace betwwwn these two. //Tony
1
7452
by: crabbie_upk | last post by:
Hi, Need help in tuning JAVA on LINUX. From http://www.trutek.com/index.php?id=173 I understand that on Linux operating system with 4GB memory, JVM can be run with maximum memory of 3800m. But...
3
1984
by: Veeru71 | last post by:
We have got 2 DB2-UDB databases (DB1 & DB2) running on separate instances( Inst1 & Inst2). DB1 has got Schema1 and DB2 has got Schema2. We would like to setup some kind of replication to...
4
4500
by: Dave | last post by:
I have a global.asax file with Application_Start defined and create some static data there and in another module used in the asp.net application and I realize that static data is shared amongst...
7
2063
by: Peted | last post by:
Hi, im hoping someone cane provide or point to a definitive accurate explantion of dotnet compilation when run and the best way to optimise peformace when dotnet code is run first time and...
0
7039
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
6904
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...
0
7037
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,...
1
6735
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...
0
5326
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,...
0
4476
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...
0
1296
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 ...
1
558
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
176
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.