Re: planer chooses very bad plan

From: "Pierre C" <lists(at)peufeu(dot)com>
To: Corin <wakathane(at)gmail(dot)com>, pgsql-performance(at)postgresql(dot)org
Subject: Re: planer chooses very bad plan
Date: 2010-04-12 10:06:41
Message-ID: op.va1npfh6eorkce@immo.peufeu.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance


> 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

If you need very fast performance on this query, you need to be able to
use the index for ordering.

Note that the following query will only optimize the first page of results
in the case you want to display BOTH sent and received telegrams.

- Create an index on (recipient_id, id) WHERE NOT recipient_deleted
- Create an index on (user_id, id) WHERE NOT user_deleted
- Drop redundant indexes (recipient_id) and (user_id)

SELECT * FROM (
SELECT * FROM "telegrams" WHERE recipient_id=508933 AND
recipient_deleted=FALSE ORDER BY id DESC LIMIT 10
UNION ALL
SELECT * FROM "telegrams" WHERE user_id=508933 AND user_deleted=FALSE
ORDER BY id DESC LIMIT 10
) AS foo ORDER BY id DESC LIMIT 10;

These indexes will also optimize the queries where you only display the
inbox and outbox, in which case it will be able to use the index for
ordering on any page, because there will be no UNION.

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message Helio Campos Mello de Andrade 2010-04-12 10:23:20 significant slow down with various LIMIT
Previous Message RD黄永卫 2010-04-12 06:35:32 How to diagnose a “context-switching ” storm problem ?