Re: SubQuery Performance

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

-- 
Sent via pgsql-performance mailing list (pgsql-performance@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-performance


[Postgresql General]     [Postgresql PHP]     [PHP Users]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Yosemite]

  Powered by Linux