X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id EC038329E76 for ; Sun, 19 Sep 2004 17:59:04 +0100 (BST) 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 55503-07 for ; Sun, 19 Sep 2004 16:59:01 +0000 (GMT) Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by svr1.postgresql.org (Postfix) with ESMTP id A1E4D329E70 for ; Sun, 19 Sep 2004 17:59:01 +0100 (BST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.1/8.13.1) with ESMTP id i8JGwv97019734; Sun, 19 Sep 2004 12:58:57 -0400 (EDT) To: mailreg@numerixtechnology.de Cc: Jean-Luc Lachance , pgsql-sql@postgresql.org Subject: Re: ORDER BY and NULLs In-reply-to: <414DB594.5000300@numerixtechnology.de> References: <011d01c49e5b$c88fdb20$2766f30a@development.greatgulfhomes.com> <414DB1AB.5080207@sympatico.ca> <414DB594.5000300@numerixtechnology.de> Comments: In-reply-to T E Schmitz message dated "Sun, 19 Sep 2004 17:36:36 +0100" Date: Sun, 19 Sep 2004 12:58:57 -0400 Message-ID: <19733.1095613137@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: 200409/134 X-Sequence-Number: 18980 T E Schmitz writes: > You must've been reading my mind. I was just wondering what to do about > indexing on that particular table. I read somewhere that an Index is not > going to improve the performance of an ORDER BY if the sort column > contains NULLs because NULLs aren't indexed? Whatever you were reading had it pretty badly garbled :-( Btree indexes *do* store nulls, so the presence of nulls doesn't affect whether they are usable for meeting an ORDER BY spec. However the index sort order does have to exactly match the ORDER BY list, and even then it's not necessarily the case that the index is useful. The brutal fact is that seqscan-and-sort is generally faster than a full-table indexscan for large tables anyway, unless the table is clustered or otherwise roughly in order by the index. If you are going to use an ORDER BY that involves COALESCE or NOT NULL expressions, then the only way that it could be met with an index is to create an expressional index on exactly that list of expressions. For instance regression=# create table foo (f int, t int); CREATE TABLE regression=# explain select * from foo order by f, coalesce(t, -1); QUERY PLAN ------------------------------------------------------------- Sort (cost=69.83..72.33 rows=1000 width=8) Sort Key: f, COALESCE(t, -1) -> Seq Scan on foo (cost=0.00..20.00 rows=1000 width=8) (3 rows) regression=# create index fooi on foo (f, (coalesce(t, -1))); CREATE INDEX regression=# explain select * from foo order by f, coalesce(t, -1); QUERY PLAN -------------------------------------------------------------------- Index Scan using fooi on foo (cost=0.00..52.00 rows=1000 width=8) (1 row) regression=# I'm a bit dubious that such an index would be worth its update costs, given that it's likely to be no more than a marginal win for the query. But try it and see. > Jean-Luc Lachance wrote: >> If you have large amount of rows (with or without nulls) it is faster if >> use a partial index. This advice seems entirely irrelevant to the problem of sorting the whole table... regards, tom lane