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 183F8535EB for ; Mon, 18 Apr 2005 14:11:18 -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 95959-04 for ; Mon, 18 Apr 2005 17:11:05 +0000 (GMT) Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by svr1.postgresql.org (Postfix) with ESMTP id A7785535A1 for ; Mon, 18 Apr 2005 14:11:03 -0300 (ADT) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.3/8.13.3) with ESMTP id j3IHAD0I029802 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 18 Apr 2005 11:10:16 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.3/8.13.3) with ESMTP id j3IHADTe034526; Mon, 18 Apr 2005 11:10:13 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.3/8.13.3/Submit) id j3IHADrK034525; Mon, 18 Apr 2005 11:10:13 -0600 (MDT) (envelope-from mfuhr) Date: Mon, 18 Apr 2005 11:10:13 -0600 From: Michael Fuhr To: Dave Held Cc: pgsql-performance@postgresql.org Subject: Re: Sort and index Message-ID: <20050418171013.GA34421@winnie.fuhr.org> References: <49E94D0CFCD4DB43AFBA928DDD20C8F9026184B6@asg002.asg.local> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <49E94D0CFCD4DB43AFBA928DDD20C8F9026184B6@asg002.asg.local> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.004 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200504/410 X-Sequence-Number: 11807 On Mon, Apr 18, 2005 at 10:44:43AM -0500, Dave Held wrote: > > > > I thought that an index can be used for sorting. > > I'm a little confused about the following result: > > > > create index OperationsName on Operations(cOperationName); > > explain SELECT * FROM Operations ORDER BY cOperationName; > > QUERY PLAN > > -------------------------------------------------------------- > > --------- > > Sort (cost=185.37..189.20 rows=1532 width=498) > > Sort Key: coperationname > > -> Seq Scan on operations (cost=0.00..104.32 rows=1532 width=498) > > (3 rows) > > > > Is this supposed to be so? > > Since you are fetching the entire table, you are touching all the rows. > If the query were to fetch the rows in index order, it would be seeking > all over the table's tracks. By fetching in sequence order, it has a > much better chance of fetching rows in a way that minimizes head seeks. > Since disk I/O is generally 10-100x slower than RAM, the in-memory sort > can be surprisingly slow and still beat indexed disk access. Of course, > this is only true if the table can fit and be sorted entirely in memory > (which, with 1500 rows, probably can). Out of curiosity, what are the results of the following queries? (Queries run twice to make sure time differences aren't due to caching.) SET enable_seqscan TO on; SET enable_indexscan TO off; EXPLAIN ANALYZE SELECT * FROM Operations ORDER BY cOperationName; EXPLAIN ANALYZE SELECT * FROM Operations ORDER BY cOperationName; SET enable_seqscan TO off; SET enable_indexscan TO on; EXPLAIN ANALYZE SELECT * FROM Operations ORDER BY cOperationName; EXPLAIN ANALYZE SELECT * FROM Operations ORDER BY cOperationName; SELECT version(); With 1500 rows of random data, I consistently see better performance with an index scan (about twice as fast as a sequence scan), and the planner uses an index scan if it has a choice (i.e., when enable_seqscan and enable_indexscan are both on). But my test case and postgresql.conf settings might be different enough from yours to account for different behavior. -- Michael Fuhr http://www.fuhr.org/~mfuhr/