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 8AE2D14B2800 for ; Thu, 12 Aug 2004 03:49:41 -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 85989-05 for ; Thu, 12 Aug 2004 06:49:32 +0000 (GMT) Received: from boutiquenumerique.com (gailleton-2-82-67-9-10.fbx.proxad.net [82.67.9.10]) by svr1.postgresql.org (Postfix) with ESMTP id 6F20614B27FF for ; Thu, 12 Aug 2004 03:49:29 -0300 (ADT) Received: (qmail 13635 invoked from network); 12 Aug 2004 08:49:35 +0200 Received: from unknown (HELO musicbox) (192.168.0.2) by gailleton-2-82-67-9-10.fbx.proxad.net with SMTP; 12 Aug 2004 08:49:35 +0200 To: "Jason Coene" , "'Tom Lane'" Cc: "'Merlin Moncure'" , pgsql-performance@postgresql.org Subject: Re: Hardware upgrade for a high-traffic database References: <200408120029.i7C0T30E002389@mail.gotfrag.com> Message-ID: Date: Thu, 12 Aug 2004 08:49:37 +0200 From: =?iso-8859-15?Q?Pierre-Fr=E9d=E9ric_Caillaud?= Organization: =?iso-8859-15?Q?La_Boutique_Num=E9rique?= Content-Type: text/plain; format=flowed; delsp=yes; charset=iso-8859-15 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit In-Reply-To: <200408120029.i7C0T30E002389@mail.gotfrag.com> User-Agent: Opera M2/7.53 (Linux, build 737) 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/183 X-Sequence-Number: 7819 On Wed, 11 Aug 2004 20:29:04 -0400, Jason Coene wrote: > gf=# EXPLAIN ANALYZE SELECT id FROM comments WHERE userid = 51 ORDER BY > timestamp DESC LIMIT 5; > QUERY > PLAN > ---------------------------------------------------------------------------- > ------------------------------------------------------------------- > Limit (cost=1608.43..1608.45 rows=5 width=8) (actual time=0.292..0.317 > rows=5 loops=1) > -> Sort (cost=1608.43..1609.45 rows=407 width=8) (actual > time=0.287..0.295 rows=5 loops=1) > Sort Key: "timestamp" > -> Index Scan using comments_ix_userid on comments > (cost=0.00..1590.79 rows=407 width=8) (actual time=0.031..0.190 rows=35 > loops=1) > Index Cond: (userid = 51) > Total runtime: 0.375 ms > (6 rows) Well, you have to read it from the bottom. - Index Scan using comments_ix_userid : It selects all records for your user. rows=407 : there are 407 rows. -> Sort (cost=1608.43..1609.45 rows=407 width=8) It sorts them to find the 5 more recent. So basically you grab 407 rows to return only 5, so you do 80x more disk I/O than necessary. It is likely that posts from all users are interleaved in the table, so this probably translates directly into 407 page fetches. Note : EXPLAIN ANALYZE will only give good results the first time you run it. The second time, all data is in the cache, so it looks really faster than it is. > gf=# EXPLAIN ANALYZE SELECT id FROM comments WHERE userid = 51 ORDER BY > userid DESC, timestamp DESC LIMIT 5; > QUERY PLAN > ---- > Limit (cost=0.00..19.90 rows=5 width=12) (actual time=0.040..0.076 > rows=5 > loops=1) > -> Index Scan Backward using comments_ix_userid_timestamp on comments > (cost=0.00..1620.25 rows=407 width=12) (actual time=0.035..0.054 rows=5 > loops=1) > Index Cond: (userid = 51) > Total runtime: 0.134 ms > (4 rows) > > Note: This was done after adding an index on comments (userid, timestamp) Well, this one correctly uses the index, fetches 5 rows, and returns them. So, excluding index page hits, your unoptimized query has >400 page fetches, and your optimized one has 5 page fetches. Still wonder why it's faster ? Seq scan is fast when locality of reference is good. In your case, it's very bad.