pg.ddx.io pgsql-performance@postgresql.org mailing list archivehelp / color / mirror / Atom feed
From: A. Kretschmer <andreas.kretschmer@schollglas.com>
To: pgsql-performance@postgresql.org
Subject: Re: SubQuery Performance
Date: Thu, 26 Aug 2010 07:24:20 +0200
Message-ID: <20100826052419.GB25058@a-kretschmer.de> (raw)
In-Reply-To: <6107e604-2694-4a32-b625-d9870a4c0df3@y11g2000yqm.googlegroups.com>
References: <6107e604-2694-4a32-b625-d9870a4c0df3@y11g2000yqm.googlegroups.com>
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
view thread (2+ messages)
Message-ID: <20100826052419.GB25058@a-kretschmer.de> Permalink: ../20100826052419.GB25058@a-kretschmer.de/ Also on: postgresql.org/message-id/20100826052419.GB25058@a-kretschmer.de ·
reply
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Reply to all the recipients using the --to and --cc options: reply via email To: pgsql-performance@postgresql.org Cc: andreas.kretschmer@schollglas.com Subject: Re: SubQuery Performance In-Reply-To: <20100826052419.GB25058@a-kretschmer.de> * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox
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