X-Original-To: pgsql-performance@postgresql.org Received: from spampd.localdomain (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id A64734760E3 for ; Sun, 13 Apr 2003 21:45:04 -0400 (EDT) Received: from wolff.to (wolff.to [66.93.249.74]) by postgresql.org (Postfix) with SMTP id ED07047580B for ; Sun, 13 Apr 2003 21:45:03 -0400 (EDT) Received: (qmail 19493 invoked by uid 500); 14 Apr 2003 01:45:47 -0000 Date: Sun, 13 Apr 2003 20:45:47 -0500 From: Bruno Wolff III To: pgsql-performance@postgresql.org Subject: Multicolumn indexes and equal conditions Message-ID: <20030414014547.GA19450@wolff.to> Mail-Followup-To: pgsql-performance@postgresql.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.4i X-Spam-Status: No, hits=-12.1 required=5.0 tests=BAYES_10,USER_AGENT_MUTT autolearn=ham version=2.50 X-Spam-Level: X-Spam-Checker-Version: SpamAssassin 2.50 (1.173-2003-02-20-exp) X-Archive-Number: 200304/128 X-Sequence-Number: 1634 I noticed that when a multicolumn index exists it isn't necessarily fully used when the first column is constrained by an equals condition. However by adding a redundant sort condition you can get both columns used. In the following examples crate has an index on gameid and areaid. The examples below are for 7.4 development, but 7.3.2 behaves similarly. explain analyze select areaid from crate where gameid = 'TTN' order by areaid; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------- Sort (cost=132.93..133.02 rows=36 width=11) (actual time=5.44..5.57 rows=287 loops=1) Sort Key: areaid -> Index Scan using crate_game on crate (cost=0.00..132.00 rows=36 width=11) (actual time=0.06..1.94 rows=287 loops=1) Index Cond: (gameid = 'TTN'::text) Total runtime: 5.81 msec (5 rows) explain analyze select areaid from crate where gameid = 'TTN' order by gameid, areaid; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------- Index Scan using crate_game on crate (cost=0.00..132.00 rows=36 width=18) (actual time=0.08..2.06 rows=287 loops=1) Index Cond: (gameid = 'TTN'::text) Total runtime: 2.51 msec (3 rows)