X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id E1DA614B2869 for ; Wed, 11 Aug 2004 20:20:11 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 06433-05 for ; Wed, 11 Aug 2004 23:20:10 +0000 (GMT) Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by svr1.postgresql.org (Postfix) with ESMTP id 748F314B2866 for ; Wed, 11 Aug 2004 20:20:04 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.12.11/8.12.11) with ESMTP id i7BNK5bj021683; Wed, 11 Aug 2004 19:20:05 -0400 (EDT) To: "Jason Coene" Cc: "'Merlin Moncure'" , pgsql-performance@postgresql.org Subject: Re: Hardware upgrade for a high-traffic database In-reply-to: <200408112118.i7BLIR0E097522@mail.gotfrag.com> References: <200408112118.i7BLIR0E097522@mail.gotfrag.com> Comments: In-reply-to "Jason Coene" message dated "Wed, 11 Aug 2004 17:18:27 -0400" Date: Wed, 11 Aug 2004 19:20:04 -0400 Message-ID: <21682.1092266404@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200408/175 X-Sequence-Number: 7811 "Jason Coene" writes: > A good example, a comments table where users submit TEXT data. A common > query is to find the last 5 comments a user has submitted. The scan, while > using an index, takes a considerable amount of time (> 0.5 sec is about as > good as it gets). Again, it's using an index on the single WHERE clause > (userid = int). The field that's used to ORDER BY (timestamp) is also > indexed. You mean you are doing SELECT ... WHERE userid = 42 ORDER BY timestamp DESC LIMIT 5; and hoping that separate indexes on userid and timestamp will get the job done? They won't. There are only two possible plans for this, neither very good: select all of user 42's posts and sort them, or scan timewise backwards through *all* posts looking for the last 5 from user 42. If you do this enough to justify a specialized index, I would suggest a two-column index on (userid, timestamp). You will also need to tweak the query, because the planner is not quite smart enough to deduce that such an index is applicable to the given sort order: SELECT ... WHERE userid = 42 ORDER BY userid DESC, timestamp DESC LIMIT 5; This should generate an index-scan-backwards plan that will execute nigh instantaneously, because it will only fetch the rows you really want. You might or might not be able to drop the separate indexes on userid and timestamp, depending on what other queries you might have that need them. But you should be paying attention to what plans you are really getting (see EXPLAIN) rather than just assuming that some indexes chosen at random will do what you need. regards, tom lane