Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 07D246323A4 for ; Mon, 21 Jun 2010 19:07:08 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 14828-08 for ; Mon, 21 Jun 2010 22:06:59 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from www.postnewspapers.com.au (www.postnewspapers.com.au [202.4.227.11]) by mail.postgresql.org (Postfix) with ESMTP id DDD956302CB for ; Mon, 21 Jun 2010 19:07:00 -0300 (ADT) Received: from mail.postnewspapers.com.au (202-89-185-120.static.dsl.amnet.net.au [202.89.185.120]) by www.postnewspapers.com.au (Postfix) with ESMTP id BCA2079240; Tue, 22 Jun 2010 06:06:56 +0800 (WST) Received: from localhost (localhost [127.0.0.1]) by mail.postnewspapers.com.au (Postfix) with ESMTP id AADDBAF5ED; Tue, 22 Jun 2010 06:06:43 +0800 (WST) X-Virus-Scanned: Debian amavisd-new at postnewspapers.com.au Received: from mail.postnewspapers.com.au ([127.0.0.1]) by localhost (mail.postnewspapers.com.au [127.0.0.1]) (amavisd-new, port 10024) with LMTP id Qbqe13IU097e; Tue, 22 Jun 2010 06:06:38 +0800 (WST) Received: from [10.1.1.8] (unknown [124.150.49.56]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "Craig Ringer", Issuer "POST Certificate Authority" (verified OK)) by mail.postnewspapers.com.au (Postfix) with ESMTPSA id B12BEAF5EB; Tue, 22 Jun 2010 06:06:37 +0800 (WST) Message-ID: <4C1FE277.5030603@postnewspapers.com.au> Date: Tue, 22 Jun 2010 06:06:47 +0800 From: Craig Ringer User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.9) Gecko/20100423 Lightning/1.0b1 Thunderbird/3.0.4 MIME-Version: 1.0 To: "Sergio Charpinel Jr." CC: pgsql-performance@postgresql.org Subject: Re: Low perfomance SUM and Group by large databse References: In-Reply-To: X-Enigmail-Version: 1.0.1 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.79 tagged_above=-10 required=5 tests=BAYES_50=0.8, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201006/370 X-Sequence-Number: 39226 On 21/06/10 22:42, Sergio Charpinel Jr. wrote: > Hi, > > I'm getting low performance on SUM and GROUP BY queries. > How can I improve my database to perform such queries. > -> Sort (cost=3499863.27..3523695.89 rows=9533049 > width=50) (actual time=165468.022..168908.828 rows=9494165 loops=1) > Sort Key: ip_src, port_src, ip_dst, port_dst, > tcp_flags, ip_proto > -> Seq Scan on acct_2010_25 (cost=0.00..352648.10 > rows=9533049 width=50) (actual time=0.038..50860.391 rows=9494165 loops=1) > Filter: ((stamp_inserted >= '2010-06-20 > 10:10:00'::timestamp without time zone) AND (stamp_inserted < > '2010-06-21 10:10:00'::timestamp without time zone)) Provide an index on at least (ip_src,port_src,ip_dst,port_dst). If you frequently do other queries that only want some of that information you could create several individual indexes for those columns instead, as Pg will combine them for a query, but that is much less efficient than an index across all four columns. CREATE INDEX ip_peers_idx ON acct_2010_25(ip_src,port_src,ip_dst_port_dst); Every index added costs you insert/update/delete speed, so try to find the smallest/simplest index that gives you acceptable performance. > Another one just summing bytes (still low): > > => explain analyze SELECT ip_src, port_src, ip_dst, port_dst, tcp_flags, > ip_proto,SUM("bytes") FROM "acct_2010_25" WHERE > "stamp_inserted">='2010-06-20 10:10' AND "stamp_inserted"<'2010-06-21 > 10:10' GROUP BY ip_src, port_src, ip_dst, port_dst, tcp_flags, ip_proto > LIMIT 50 OFFSET 0; Same deal. You have no suitable index, so Pg has to do a sequential scan of the table. Since you appear to query on stamp_inserted a lot, you should index it. -- Craig Ringer