On 24/05/11 12:14, panam wrote: > Hi, > > In my DB, there is a large table containing messages and one that contains > message boxes. > Messages are assigned to boxes via a child parent link m->b. > In order to obtain the last message for a specific box, I use the following > SQL: > > SELECT m1.id FROM message m1 LEFT JOIN message m2 ON (m1.box_id = m2.box_id > AND m1.id < m2.id) WHERE m2.id IS NULL AND m1.box_id = id; > > This worked quite well for a long time. But now, suddenly the performance of > this query drastically degraded as new messages were added. > If these new messages are removed again, everything is back to normal. If > other messages are removed instead, the problem remains, so it does not seem > to be a memory issue. I fear I have difficulties to understand what is going > wrong here. The usual cause is that the statistics for estimated row counts cross a threshold that makes the query planner think that a different kind of plan will be faster. If the query planner is using bad information about the performance of the storage, then it will be making bad decisions about which approach is faster. So the usual thing to do is to adjust seq_page_cost and random_page_cost to more closely reflect the real performance of your hardware, and to make sure that effective_cache_size matches the real amount of memory your computer has free for disk cache use. Newer versions of PostgreSQL always include query planning and statistics improvements too. BTW, it can be really helpful to paste your query plans into http://explain.depesz.com/ , which will provide an easier to read visual analysis of the plan. This will only work with query plans that haven't been butchered by mail client word wrapping, so I can't do it for you, but if you paste them there and post the links that'd be really handy. Also have a look at http://wiki.postgresql.org/wiki/SlowQueryQuestions I found the plans you posted a bit hard to read. Not your fault; it's stupid mail clients. Maybe depesz.com needs to be taught to de-munge the damage done to plans by common mail clients. -- Craig Ringer -- Sent via pgsql-performance mailing list (pgsql-performance@xxxxxxxxxxxxxx) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance