Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
Database Servers
DB2InformixIngresMS SQLOraclePervasive.SQLPostgreSQLProgressSybase
Desktop Databases
FileMakerFoxProMS AccessParadox
General
General DB TopicsDatabase Theory
Related Topics
Java Development.NET DevelopmentVB DevelopmentMore Topics ...

Database Forum / DB2 Topics / March 2006

Tip: Looking for answers? Try searching our database.

Query performance

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
yvrca - 29 Mar 2006 16:30 GMT
I have a table with over 97M rows and a primary key:
(px int,
sx int
effectivedate date)

I need to extract rows for each px and sx for the latest date.  We have
a view :
select a.px, a.sx, a.effectivedate, a.cost, a.suggretailprice,
a.cost_pst,
   a.marginpercent, a.quantity
 from mx.productcost a
 where a.effectivedate = (
     select max(b.effectivedate)
       from mx.productcost b
       where a.px = b.px
         and a.sx = b.sx);

Access plan is:
Access Plan

Step Operation                           Object name
  Object type     Cost
---- ----------------------------
-------------------------                      -----------
-------------
1    Table access full                    PRODUCTCOST
   Table     6320322.50
2    Index scan (no table access)   SYSIBM.SQL031013180223470 Index
36.96
3    Group rows
                              36.96
4    Filter data
                                 36.96
5    Nested loop join
                             1183707136.00
6    Data operation complete
                        1183707136.00

I have tried indexes of (px asc, sx asc, effectivedate desc) and (px
asc, sx asc, effectivedate asc) with 'allow reverse scan'  but all give
very poor performance (no results over many hours).

Any suggestions on an efficient way to get at the latest rows of the
key?

thanks a lot in advance.
Brian Tkatch - 29 Mar 2006 17:25 GMT
Perhaps you could change the WHERE clause to use IN (GROUP BY)?

select px, sx, effectivedate, cost, suggretailprice, cost_pst,
marginpercent, quantity
from mx.productcost where (px, sx, effectivedate)
IN (select px, sx, max(effectivedate)  from mx.productcost group by px,
sx)

B.
tuarek - 29 Mar 2006 17:30 GMT
try this

select d.px, d.sx, d.effectivedate, d.cost, d.suggretailprice,
d.cost_pst,  d.marginpercent, d.quantity
 from mx.productcost d,
 (select max(b.effectivedate), a.px, a.sx
  from mx.productcost b , mx.productcost a
  group by a.px,  a.sx
 where a.px = b.px and a.sx = b.sx) as c
 where c.px = d.px and c.sx = d.sx
yvrca - 29 Mar 2006 20:33 GMT
I will test this.  also along with:

select d.px, d.sx, d.effectivedate, d.cost, d.suggretailprice,
      d.cost_pst,  d.marginpercent, d.quantity
 from mx.productcost d,
 (select max(a.effectivedate), a.px, a.sx
    from mx.productcost a
   group by a.px,  a.sx ) as c
 where c.px = d.px and c.sx = d.sx

which give a lower plan cost.

> try this
>
[quoted text clipped - 6 lines]
>   where a.px = b.px and a.sx = b.sx) as c
>   where c.px = d.px and c.sx = d.sx
yvrca - 30 Mar 2006 22:02 GMT
I ran this for 4 hours with no results.

thanks for you help.
yvrca - 30 Mar 2006 22:01 GMT
Thanks Brian:

your query ran in under 2 hours.

> Perhaps you could change the WHERE clause to use IN (GROUP BY)?
>
[quoted text clipped - 5 lines]
>
> B.
Brian Tkatch - 30 Mar 2006 22:12 GMT
Wow, glad i could help. :)

B.
gspk@yahoo.com - 31 Mar 2006 21:26 GMT
You may try the following,

select     a.px, a.sx, a.effectivedate, a.cost, a.suggretailprice,
    a.cost_pst, a.marginpercent, a.quantity
from (
    select     px, sx, effectivedate, cost, suggretailprice,
        cost_pst, marginpercent, quantity,
        rank () over (partition by px, sx order by effectivedate desc) as rnk

    from mx.productcost)  a
where rnk = 1;

Note: I have not tested the query. But usually performs much better as
it doesn't require the self join. Also, it does the plain table/index
scan.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.