X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 901409DCD2A for ; Wed, 30 Nov 2005 16:41:26 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 13123-07 for ; Wed, 30 Nov 2005 16:41:25 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id E23839DCD40 for ; Wed, 30 Nov 2005 16:41:21 -0400 (AST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.1/8.13.1) with ESMTP id jAUKfMte000816; Wed, 30 Nov 2005 15:41:22 -0500 (EST) To: "Brad Might" cc: pgsql-performance@postgresql.org Subject: Re: Select with grouping plan question In-reply-to: References: Comments: In-reply-to "Brad Might" message dated "Wed, 30 Nov 2005 13:21:07 -0600" Date: Wed, 30 Nov 2005 15:41:22 -0500 Message-ID: <815.1133383282@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.004 required=5 tests=[AWL=0.004] X-Spam-Score: 0.004 X-Spam-Level: X-Archive-Number: 200511/560 X-Sequence-Number: 15817 "Brad Might" writes: > This seems to me to be an expensive plan and I'm wondering if there's a > way to improve it or a better way to do what I'm trying to do here (get > a count of distinct values for each record_id and map that value to the > entity type) entity_type_id_mapping is 56 rows > volume_node_entity_data_values is approx 500,000,000 rows vq_record_id > has approx 11,000,000 different values vq_entity_type is a value in > entity_type_id_mapping.entity_type Hmm, what Postgres version is that? And have you ANALYZEd entity_type_id_mapping lately? I'd expect the planner to realize that there cannot be more than 56 output groups, which ought to lead it to prefer a hashed aggregate over the sort+group method. That's what I get in a test case with a similar query structure, anyway. If you're stuck on an old PG version, it might help to do the aggregation first and then join, ie select ... from (select count(vq_entity_value) as vcount, vq_entity_type from data_schema.volume_queue_entities group by vq_entity_type) qe, volume_8.entity_type_id_mapping emap where qe.vq_entity_type = emap.entity_type; regards, tom lane