Re: Forcing query to use an index

From: Greg Stark <gsstark(at)mit(dot)edu>
To: Jean-Luc Lachance <jllachan(at)nsd(dot)ca>
Cc: Greg Stark <gsstark(at)MIT(dot)EDU>, josh(at)agliodbs(dot)com, Michael Nachbaur <mike(at)nachbaur(dot)com>, pgsql-sql(at)postgresql(dot)org
Subject: Re: Forcing query to use an index
Date: 2003-03-04 20:10:23
Message-ID: 87r89mki4w.fsf@stark.dyndns.tv
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Jean-Luc Lachance <jllachan(at)nsd(dot)ca> writes:

> I beg to differ.
>
> A NULL field means not set.

The best description for what NULL means is "unknown".

> Having to use work around because the database does not index null is
> one thing, but making it a general rule is not.

My natural inclination is to use exactly the representation he used.
I've done so numerous times in the past. But using NULL sort of means "we
don't know when this account might have been deactivated" which is why it
leads to all these awkward OR clauses in his queries.

I admit using 9999-01-01 as a date gives me the willies. But it does match
with the way the field is used and it produces nice clean index range lookups.

> Having NULL indexed would also speed up things when "is null" is part af
> the query.

No, it wouldn't. Not in his query. His query had
"disabled IS NULL OR disabled < ?"
Even if "IS NULL" was indexable this still wouldn't be an indexable clause.

Another option would be to use a functional index.

create function disabled_as_of(timestamp with time zone) as
'select coalesce($1,''9999-01-01'')' language sql immutable

Then index disabled_as_of(disabled) and access it with
"disabled_as_of(disabled) > current_time"

(or perhaps it would be clearer as "NOT disabled_as_of(disabled) < current_time")

Of course all this is just extra busywork to keep the ugly data representation
out of the data model and hidden in the functional index. And it's not really
very well hidden either.

--
greg

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Stephan Szabo 2003-03-04 20:13:05 Re: Sorting by NULL values
Previous Message Dan Langille 2003-03-04 20:07:29 Re: Sorting by NULL values