Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id BAC2B1337B2F for ; Fri, 4 Feb 2011 12:35:07 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 26617-10 for ; Fri, 4 Feb 2011 16:35:00 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from smtp2.peak6.com (smtp2.peak6.com [38.98.137.9]) by mail.postgresql.org (Postfix) with ESMTP id AC58C1336FA7 for ; Fri, 4 Feb 2011 12:34:59 -0400 (AST) X-AuditID: 0a02cb1e-b7bf8ae00000096d-47-4d4c2ab2c71a Message-ID: <4D4C2AB1.3050401@peak6.com> Date: Fri, 4 Feb 2011 10:34:57 -0600 From: Shaun Thomas Reply-To: sthomas@peak6.com Organization: Peak6 User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.7) Gecko/20100720 Lightning/1.0b2 Thunderbird/3.1.1 MIME-Version: 1.0 To: "pgsql-performance@postgresql.org" Subject: Re: Really really slow select count(*) References: <4D4C14A3.9000409@peak6.com> In-Reply-To: Content-Type: text/plain; charset="UTF-8"; format=flowed content-transfer-encoding: 7bit X-Brightmail-Tracker: AAAAAQAAAZE= X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-10 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201102/199 X-Sequence-Number: 42337 On 02/04/2011 10:17 AM, felix wrote: > > How big is this table when it's acting all bloated and ugly? > > 458MB Wow! There's no way a table with 300k records should be that big unless it's just full of text. 70-seconds seems like a really long time to read half a gig, but that might be because it's fighting for IO with other processes. For perspective, we have several 1-2 million row tables smaller than that. Heck, I have a 11-million row table that's only 30% larger. > are updates of the where id IN (1,2,3,4) generally not efficient ? > how about for select queries ? Well, IN is notorious for being inefficient. It's been getting better, but even EXISTS is a better bet than using IN. We've got a lot of stuff using IN here, and we're slowly phasing it out. Every time I get rid of it, things get faster. > I actually just added most of those yesterday in an attempt to improve > performance. priority and agent_priority were missing indexes and that > was a big mistake. Haha. Well, that can always be true. Ironically one of the things you actually did by creating the indexes is create fast lookup values to circumvent your table bloat. It would help with anything except sequence scans, which you saw with your count query. > ok, > built True is in the minority. Ok, in that case, use a partial index. If a boolean value is only 1% of your table or something, why bother indexing the rest anyway? CREATE INDEX fastadder_fastadderstatus_built ON fastadder_fastadderstatus WHERE built; But only if it really is the vast minority. Check this way: SELECT built, count(1) FROM fastadder_fastadderstatus GROUP BY 1; We used one of these to ignore a status that was over 90% of the table, where the other statuses combined were less than 10%. The index was 10x smaller and much faster than before. If you know both booleans are used together often, you can combine them into a single index, again using a partial where it only indexes if both values are true. Much smaller, much faster index if it's more selective than the other indexes. -- Shaun Thomas OptionsHouse | 141 W. Jackson Blvd. | Suite 800 | Chicago IL, 60604 312-676-8870 sthomas@peak6.com ______________________________________________ See http://www.peak6.com/email_disclaimer.php for terms and conditions related to this email