X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id BEEA08B9C8D for ; Sat, 5 Feb 2005 19:26:24 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 43010-04 for ; Sat, 5 Feb 2005 19:26:17 +0000 (GMT) Received: from ct.radiology.uiowa.edu (ct.radiology.uiowa.edu [129.255.60.186]) by svr1.postgresql.org (Postfix) with ESMTP id 162638B9B6C for ; Sat, 5 Feb 2005 19:26:16 +0000 (GMT) Received: from [192.168.1.11] (12-217-241-0.client.mchsi.com [12.217.241.0]) by ct.radiology.uiowa.edu (8.11.6/8.11.6) with ESMTP id j15JQB327678; Sat, 5 Feb 2005 13:26:11 -0600 Message-ID: <42051DD1.7020802@arbash-meinel.com> Date: Sat, 05 Feb 2005 13:26:09 -0600 From: John A Meinel User-Agent: Mozilla Thunderbird 1.0 (Windows/20041206) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Dirk Lutzebaeck Cc: pgsql-performance@postgresql.org Subject: Re: query produces 1 GB temp file References: <20050205182117.0125074@mail2.aeccom.com> In-Reply-To: <20050205182117.0125074@mail2.aeccom.com> X-Enigmail-Version: 0.89.5.0 X-Enigmail-Supports: pgp-inline, pgp-mime Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="------------enigC860ACFC206C9176765D21DA" X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests= X-Spam-Level: X-Archive-Number: 200502/81 X-Sequence-Number: 10410 This is an OpenPGP/MIME signed message (RFC 2440 and 3156) --------------enigC860ACFC206C9176765D21DA Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Dirk Lutzebaeck wrote: >Hi, > >here is a query which produces over 1G temp file in pgsql_tmp. This >is on pgsql 7.4.2, RHEL 3.0, XEON MP machine with 32GB RAM, 300MB >sort_mem and 320MB shared_mem. > >Below is the query and results for EXPLAIN and EXPLAIN ANALYZE. All >tables have been analyzed before. > >Can some please explain why the temp file is so huge? I understand >there are a lot of rows. > >Thanks in advance, > >Dirk > > ... > -> Nested Loop (cost=0.00..8346.73 rows=3 width=1361) (actual time=34.104..18016.005 rows=703677 loops=1) > > Well, there is this particular query where it thinks there will only be 3 rows, but in fact there are 703,677 of them. And the previous line: > -> Sort (cost=8346.75..8346.76 rows=3 width=1361) (actual time=75357.448..75499.263 rows=22439 loops=1) > > Seem to indicate that after sorting you still have 22,439 rows, which then gets pared down again down to 1000. I'm assuming that the sort you are trying to do is extremely expensive. You are sorting 700k rows, which takes up too much memory (1GB), which forces it to create a temporary table, and write it out to disk. I didn't analyze it a lot, but you might get a lot better performance from doing a subselect, rather than the query you wrote. You are joining 4 tables (bi, en, df AS ft, es) I don't know which tables are what size. In the end, though, you don't really care about the en table or es tables (they aren't in your output). So maybe one of you subselects could be: where bi.en = (select en from es where es.co = bi.co and es.spec=122293729); I'm pretty sure the reason you need 1GB of temp space is because at one point you have 700k rows. Is it possible to rewrite the query so that it does more filtering earlier? Your distinct criteria seems to filter it down to 20k rows. So maybe it's possible to do some sort of a distinct in part of the subselect, before you start joining against other tables. If you have that much redundancy, you might also need to think of doing a different normalization. Just some thoughts. Also, I thought using the "oid" column wasn't really recommended, since in *high* volume databases they aren't even guaranteed to be unique. (I think it is a 32-bit number that rolls over.) Also on a database dump and restore, they don't stay the same, unless you take a lot of extra care that they are included in both the dump and the restore. I believe it is better to create your own "id" per table (say SERIAL or BIGSERIAL). John =:-> --------------enigC860ACFC206C9176765D21DA Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.6 (Cygwin) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQFCBR3RJdeBCYSNAAMRAnJZAJ0bLab3EDS1v+wFzGP5bUKe8nYziACfcI7x 2Hnvwgpm8Na6rjAaWuGjeKU= =QyLG -----END PGP SIGNATURE----- --------------enigC860ACFC206C9176765D21DA--