Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 961B59F9243 for ; Fri, 10 Aug 2007 11:53:33 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 46536-09 for ; Fri, 10 Aug 2007 11:53:20 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from ns1.officenet.no (ns1.officenet.no [193.212.174.3]) by postgresql.org (Postfix) with ESMTP id 738F49F9242 for ; Fri, 10 Aug 2007 11:53:29 -0300 (ADT) Received: from [85.112.130.182] (helo=andreak-laptop.local) by ns1.officenet.no with esmtpsa (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.63) (envelope-from ) id 1IJVrc-0006g4-Tc for pgsql-sql@postgresql.org; Fri, 10 Aug 2007 16:53:24 +0200 From: Andreas Joseph Krogh Organization: OfficeNet AS To: pgsql-sql@postgresql.org Subject: Index usage in order by with multiple columns in order-by-clause Date: Fri, 10 Aug 2007 16:53:12 +0200 User-Agent: KMail/1.9.6 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Message-Id: <200708101653.13011.andreak@officenet.no> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200708/80 X-Sequence-Number: 29018 I have the following test-case: CREATE TABLE test( name varchar PRIMARY KEY, value varchar NOT NULL, created timestamp not null ); create index test_lowernamevalue_idx ON test ((lower(name) || lower(value))= ); create index test_lowernamevaluecreated_idx ON test ((lower(name) ||=20 lower(value)), created); andreak=3D# EXPLAIN ANALYZE select * from test order by lower(name) ||=20 lower(value) ASC, created ASC; QUERY PLAN =2D------------------------------------------------------------------------= =2D--------------------------------------------------------------- Index Scan using test_lowernamevaluecreated_idx on test (cost=3D0.00..61.= 58=20 rows=3D770 width=3D72) (actual time=3D0.013..0.013 rows=3D0 loops=3D1) Total runtime: 0.127 ms (2 rows) andreak=3D# EXPLAIN ANALYZE select * from test order by lower(name) ||=20 lower(value) ASC, created DESC; QUERY PLAN =2D------------------------------------------------------------------------= =2D------------------------------ Sort (cost=3D60.39..62.32 rows=3D770 width=3D72) (actual time=3D0.034..0.= 034 rows=3D0=20 loops=3D1) Sort Key: (lower((name)::text) || lower((value)::text)), created -> Seq Scan on test (cost=3D0.00..23.47 rows=3D770 width=3D72) (actual= =20 time=3D0.004..0.004 rows=3D0 loops=3D1) Total runtime: 0.123 ms (4 rows) As the EXPLAIN-output shows, the index is not used when sort-ordering diffe= rs=20 in the two order-by-columns. Is there a way I can have multiple columns in the ORDER BY clause, each wit= h=20 different ASC/DESC-order and still use an index to speed up sorting? In my application I often have a need to sort by more than 3 columns, so I'= m=20 really wondering if there is a way to make sorting of multiple columsn (eac= h=20 which may have different sort-order) use an index? Preferrably without havi= ng=20 to create 2^N indexes. =2D-=20 Andreas Joseph Krogh Senior Software Developer / Manager =2D-----------------------+---------------------------------------------+ OfficeNet AS | The most difficult thing in the world is to | Karenslyst All=E9 11 | know how to do a thing and to watch | PO. Box 529 Sk=F8yen | somebody else doing it wrong, without | 0214 Oslo | comment. | NORWAY | | Tlf: +47 24 15 38 90 | | =46ax: +47 24 15 38 91 | | Mobile: +47 909 56 963 | | =2D-----------------------+---------------------------------------------+