Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Data Bases > IBM DB2 > Re: Subquery de...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 5 Topic 8879 of 9369
Post > Topic >>

Re: Subquery decorrelation

by Serge Rielau <srielau@[EMAIL PROTECTED] > May 13, 2008 at 06:41 AM

Desmodromic wrote:
> On May 13, 10:25 am, Desmodromic <davies...@[EMAIL PROTECTED]
> wrote:
>> The following query has a high cost (over 4 million), uses a nested
>> loop join and runs much slower than I would like. Does anyone know if
>> is possible to rewrite this query more efficiently (probably without
>> the correlated subquery)? If so, could you please show me how.
>>
>>  SELECT *
>>   FROM p1
>>   WHERE recdt = (
>>       SELECT MAX ( recdt )
>>         FROM p2
>>         WHERE p1.script = p2.script
>>           AND p1.param = p2.param
>>           AND p1.hostname = p2.hostname
>>           AND p1.item = p2.item
>>           AND p2.flags NOT IN ( ? ) )
>>     AND p1.script = ?
>>     AND p1.param = ?
>>     AND p1.hostname = ?
>>     AND p1.flags NOT IN ( ? );
>>
>> Thanks in advance.
> 
> OK, I've spent a bit more time on it and have rewritten it as:
> 
> select *
> from p1
> where (recdt, script, param, hostname, item)
> in (select max(recdt), script, param, hostname, item
>     from p2
>     where script = ?
>     AND param = ?
>     AND hostname = ?
>     AND flags NOT IN ( ? )
>     group by script, param, hostname, item)
> AND flags NOT IN ( ? );
> 
> Cost is down to about 8000 and it runs in 5 seconds. I'd still welcome
> any further rewrite advice.
Try an OLAP function (may or may not be a winner):
SELECT * FROM (SELECT MAX(recdt) OVER () AS max, p2.*
   FROM p2 WHERE p1.script = ?
     AND p1.param = ?
     AND p1.hostname = ?
     AND p1.flags NOT IN ( ? )) AS X WHERE recdt = max;

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




 5 Posts in Topic:
Subquery decorrelation
Desmodromic <davies_ms  2008-05-12 19:25:13 
Re: Subquery decorrelation
Desmodromic <davies_ms  2008-05-13 02:25:38 
Re: Subquery decorrelation
Serge Rielau <srielau@  2008-05-13 06:41:43 
Re: Subquery decorrelation
ChrisC <cunningham.cb@  2008-05-13 13:36:15 
Re: Subquery decorrelation
Desmodromic <davies_ms  2008-05-13 18:11:48 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue Oct 14 1:45:45 CDT 2008.