Scott Marlowe <scott.marlowe@xxxxxxxxx> writes: > On Tue, Sep 8, 2009 at 8:12 AM, Eugene Morozov<eugene@xxxxxxxxxxxxxxxx> wrote: > OK, you need to look a little deeper at what's happening here. The > pgsql query planner looks at a lot of things to decide if to use seq > scan or and index. If you look at your row estimates versus actual > rows returned, you'll see they're off, sometimes by quite a bit. > Particularly the ones near the top of your query plans. There are a > few things you can do to help out here. Increase default stats target > and re-analyse, increase effective_cache_size to reflect the actual > size of data being cached by your OS / filesystem / pgsql, and then > lowering random_page_cost. Thanks to all who answered. Your answers were really helpful, I've split the query in two (couldn't make Django to use JOIN here) and was able to speed it up by a factor of 10! Eugene -- Sent via pgsql-performance mailing list (pgsql-performance@xxxxxxxxxxxxxx) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance