pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
SubQuery Performance
2+ messages / 2 participants
[nested] [flat]

* SubQuery Performance
@ 2010-08-26 01:18  mike <mail4mz@gmail.com>
  0 siblings, 1 reply; 2+ messages in thread

From: mike @ 2010-08-26 01:18 UTC (permalink / raw)
  To: pgsql-performance

Hi All,

I have a poor performance SQL as following. The table has about 200M
records, each employee have average 100 records. The query lasts about
3 hours. All I want is to update the flag for highest version of each
client's record. Any suggestion is welcome!

Thanks,

Mike


====SQL===========
update empTbl A
set flag=1
where
rec_ver =
( select max(rec_ver)
from empTbl
where empNo = A.empNo)



===Table empTbl=====

empTbl
{
int empNo;
int flag;
char[256] empDesc;
int rec_ver;
}



^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: SubQuery Performance
@ 2010-08-26 05:24  A. Kretschmer <andreas.kretschmer@schollglas.com>
  parent: mike <mail4mz@gmail.com>
  0 siblings, 0 replies; 2+ messages in thread

From: A. Kretschmer @ 2010-08-26 05:24 UTC (permalink / raw)
  To: pgsql-performance

In response to mike :
> Hi All,
> 
> I have a poor performance SQL as following. The table has about 200M
> records, each employee have average 100 records. The query lasts about
> 3 hours. All I want is to update the flag for highest version of each
> client's record. Any suggestion is welcome!
> 
> Thanks,
> 
> Mike
> 
> 
> ====SQL===========
> update empTbl A
> set flag=1
> where
> rec_ver =
> ( select max(rec_ver)
> from empTbl
> where empNo = A.empNo)
> 
> 
> 
> ===Table empTbl=====
> 
> empTbl
> {
> int empNo;
> int flag;
> char[256] empDesc;
> int rec_ver;
> }

Try this:

update empTbl A set flag=1 from (select empno, max(rec_ver) as rec_ver from empTbl group by empno) foo where (a.empno,a.rec_ver) = (foo.empno, foo.rec_ver);

You should create an index on empTbl(empNo,rec_ver).

Please show us the EXPLAIN ANALYSE <query> for both selects.


Regards, Andreas
-- 
Andreas Kretschmer
Kontakt:  Heynitz: 035242/47150,   D1: 0160/7141639 (mehr: -> Header)
GnuPG: 0x31720C99, 1006 CCB4 A326 1D42 6431  2EB0 389D 1DC2 3172 0C99



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2010-08-26 05:24 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2010-08-26 01:18 SubQuery Performance mike <mail4mz@gmail.com>
2010-08-26 05:24 ` A. Kretschmer <andreas.kretschmer@schollglas.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox