Received: from maia.hub.org (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 99E8B63358A for ; Sun, 11 Apr 2010 18:26:58 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 58926-01-4 for ; Sun, 11 Apr 2010 21:26:42 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from serenity.inversion.ee (serenity.inversion.ee [194.204.33.112]) by mail.postgresql.org (Postfix) with ESMTP id EFD2A63542D for ; Sun, 11 Apr 2010 18:25:11 -0300 (ADT) Received: by serenity.inversion.ee (Postfix, from userid 108) id 49791E34636; Sun, 11 Apr 2010 23:10:53 +0300 (EEST) Received: from [IPv6:::1] (localhost [127.0.0.1]) by serenity.inversion.ee (Postfix) with ESMTP id D9DC1E34215; Sun, 11 Apr 2010 23:10:52 +0300 (EEST) Subject: Re: planer chooses very bad plan From: Hannu Krosing To: Corin Cc: pgsql-performance@postgresql.org In-Reply-To: <4BC23B3E.5020500@gmail.com> References: <4BC23B3E.5020500@gmail.com> Content-Type: text/plain; charset="UTF-8" Date: Mon, 12 Apr 2010 00:25:12 +0300 Message-ID: <1271021112.21800.21.camel@hvost> Mime-Version: 1.0 X-Mailer: Evolution 2.28.1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.163 tagged_above=-10 required=5 tests=AWL=-1.163 X-Spam-Level: X-Archive-Number: 201004/154 X-Sequence-Number: 38404 On Sun, 2010-04-11 at 23:12 +0200, Corin wrote: > Hi, > > I'm having a query where the planer chooses a very bad plan. > > explain analyze SELECT * FROM "telegrams" WHERE ((recipient_id=508933 > AND recipient_deleted=FALSE) OR (user_id=508933 AND user_deleted=FALSE)) > ORDER BY id DESC LIMIT 10 OFFSET 0 > > "Limit (cost=0.00..1557.67 rows=10 width=78) (actual > time=0.096..2750.058 rows=5 loops=1)" > " -> Index Scan Backward using telegrams_pkey on telegrams > (cost=0.00..156545.47 rows=1005 width=78) (actual time=0.093..2750.052 > rows=5 loops=1)" > " Filter: (((recipient_id = 508933) AND (NOT recipient_deleted)) > OR ((user_id = 508933) AND (NOT user_deleted)))" > "Total runtime: 2750.124 ms" You could check if creating special deleted_x indexes helps do CREATE INDEX tgrm_deleted_recipent_index ON telegrams(recipient_id) WHERE recipient_deleted=FALSE; CREATE INDEX tgrm_deleted_user_index ON telegrams(user_id) WHERE user_deleted=FALSE; (if on live system, use "CREATE INDEX CONCURRENTLY ...") -- Hannu Krosing http://www.2ndQuadrant.com PostgreSQL Scalability and Availability Services, Consulting and Training