Received: from localhost (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id C7B4A64FD84 for ; Fri, 21 Nov 2008 15:07:59 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 53287-04 for ; Fri, 21 Nov 2008 15:07:57 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from 27.mail-out.ovh.net (27.mail-out.ovh.net [91.121.30.210]) by mail.postgresql.org (Postfix) with SMTP id 40DA664FC49 for ; Fri, 21 Nov 2008 15:07:53 -0400 (AST) Received: (qmail 10218 invoked by uid 503); 21 Nov 2008 20:25:49 -0000 Received: from b7.ovh.net (HELO mail177.ha.ovh.net) (213.186.33.57) by 27.mail-out.ovh.net with SMTP; 21 Nov 2008 20:25:49 -0000 Received: from b0.ovh.net (HELO queue-out) (213.186.33.50) by b0.ovh.net with SMTP; 21 Nov 2008 19:07:37 -0000 Received: from par69-8-88-161-102-87.fbx.proxad.net (HELO soyouz) (88.161.102.87) by ns0.ovh.net with SMTP; 21 Nov 2008 19:07:36 -0000 Date: Fri, 21 Nov 2008 20:08:27 +0100 To: Andrus , tv@fuzzy.cz Subject: Re: Hash join on int takes 8..114 seconds From: PFC Cc: "Richard Huxton" , pgsql-performance@postgresql.org Content-Type: text/plain; format=flowed; delsp=yes; charset=utf-8 MIME-Version: 1.0 References: <49253FE1.3070706@archonet.com> <0205611B528C4C2CBA99AFC64E57D6E8@andrusnotebook> <75A918DFCF25470FAF96B3FE3D25A38B@andrusnotebook> <63239.89.102.139.23.1227284110.squirrel@sq.gransy.com> Content-Transfer-Encoding: 7bit Message-ID: In-Reply-To: User-Agent: Opera Mail/9.62 (Linux) X-Ovh-Tracer-Id: 501869883787904646 X-Ovh-Remote: 88.161.102.87 (par69-8-88-161-102-87.fbx.proxad.net) X-Ovh-Local: 213.186.33.20 (ns0.ovh.net) X-Spam-Check: DONE|H 0.5/N X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200811/187 X-Sequence-Number: 31918 > log file seems that mostly only those queries are slow: > > SELECT ... > FROM dok JOIN rid USING (dokumnr) > JOIN ProductId USING (ProductId) > WHERE rid.ProductId LIKE :p1 || '%' AND dok.SaleDate>=:p2 > > :p1 and :p2 are parameters different for different queries. > > dok contains several years of data. :p2 is usually only few previous > months > or last year ago. > SELECT column list contains fixed list of known columns from all tables. > > How to create index or materialized view to optimize this types of > queries ? > I would remove some granularity, for instance create a summary table (materialized view) by month : - date (contains the first day of the month) - product_id - total quantity, total price sold in given month You get the idea. If your products belong to categories, and you make queries on all the products in a category, it could be worth making a summary table for categories also.