Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YTd7L-0007fO-QK for pgsql-sql@arkaria.postgresql.org; Thu, 05 Mar 2015 21:15:39 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YTd7L-0001I8-8Q for pgsql-sql@arkaria.postgresql.org; Thu, 05 Mar 2015 21:15:39 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1YTd7K-0001Fl-A3 for pgsql-sql@postgresql.org; Thu, 05 Mar 2015 21:15:38 +0000 Received: from smtprelay0134.b.hostedemail.com ([64.98.42.134] helo=smtprelay.b.hostedemail.com) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YTd7G-0006RU-FA for pgsql-sql@postgresql.org; Thu, 05 Mar 2015 21:15:36 +0000 Received: from filter.hostedemail.com (10.5.19.248.rfc1918.com [10.5.19.248]) by smtprelay02.b.hostedemail.com (Postfix) with ESMTP id DBBCDD3817; Thu, 5 Mar 2015 21:15:33 +0000 (UTC) X-Session-Marker: 616C76686572726540616C76682E6E6F2D69702E6F7267 X-Spam-Summary: 50, 0, 0, , d41d8cd98f00b204, alvherre@alvh.no-ip.org, :::, RULES_HIT:41:355:379:599:967:968:973:988:989:1260:1263:1277:1311:1312:1313:1314:1345:1359:1437:1515:1516:1518:1519:1534:1541:1593:1594:1595:1596:1711:1730:1747:1777:1792:1801:2393:2525:2560:2563:2682:2685:2828:2859:2895:2933:2937:2939:2942:2945:2947:2951:2954:3022:3138:3139:3140:3141:3142:3353:3865:3866:3867:3868:3870:3871:3873:3874:3934:3936:3938:3941:3944:3947:3950:3953:3956:3959:4250:4605:4659:5007:6261:7903:9010:9025:9121:10004:10400:10848:11232:11233:11256:11257:11658:11914:12517:12519:12663:13069:13095:13311:13357:13894:13895:14093:14097:21060:21067:21080:21088, 0, RBL:none, CacheIP:none, Bayesian:0.5, 0.5, 0.5, Netcheck:none, DomainCache:0, MSF:not bulk, SPF:fn, MSBL:0, DNSBL:none, Custom_rules:0:0:0 X-HE-Tag: dime87_13645403df531 X-Filterd-Recvd-Size: 2602 Received: from alvin.alvh.no-ip.org (unknown [181.43.7.13]) (Authenticated sender: alvherre@alvh.no-ip.org) by omf11.b.hostedemail.com (Postfix) with ESMTPA; Thu, 5 Mar 2015 21:15:33 +0000 (UTC) Received: by alvin.alvh.no-ip.org (Postfix, from userid 1000) id 72DAACA0; Thu, 5 Mar 2015 18:16:01 -0300 (CLST) Date: Thu, 5 Mar 2015 18:16:01 -0300 From: Alvaro Herrera To: Andreas Joseph Krogh Cc: "pgsql-sql@postgresql.org" Subject: Re: Schema for caching message-count in folders using triggers Message-ID: <20150305211601.GW3291@alvh.no-ip.org> References: MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.5.23 (2014-03-12) X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org Andreas Joseph Krogh wrote: > Hi all.   I'm facing a problem with my current schema for email where folders > start containing several 100K of messages and count(*) in them taks noticeable > time. This schema is accessible from IMAP and a web-app so lots of queries of > the type "list folders with message count" are performed.   So, I'm toying with > this idea of caching the message-count in the folder-table itself. You can do this better by keeping a table with per-folder counts and deltas. There is one main row which keeps the total value at some point in time. Each time you insert a message, add a "delta" entry with value 1; each time you remove, add a delta with value -1. You can do this with a trigger on insert/update/delete. This way, there is no contention because there are no updates. To figure out the total value, just add all the values (the main plus all deltas for that folder).