Indexing on a boolean field?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-sql(at)postgresql(dot)org
Subject: Indexing on a boolean field?
Date: 1998-06-15 15:33:16
Message-ID: 1691.897924796@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Can anyone suggest to me a better way to handle this?

I want to be able to select out the elements of a table that have
a TRUE value in a boolean field "flag". (Basically the flag means
the record hasn't been processed yet.) I can do something like

SELECT * FROM table WHERE flag = 't';

However, the table is going to get large, and most of the elements
will have flag = 'f' as transaction history accumulates. It looks
to me like a select done as above will have to be processed by scanning
the whole table; that's not going to do.

I thought of creating an index on the flag field, but soon found that
you can't do it in Postgres (there's no operator for index on boolean).
In any case, I doubt that btree or hash indexes would work well with
only two distinct data values.

Another possibility is to keep the not-yet-processed records in a
separate table, but that seems pretty ugly as well; especially since
I sometimes want to see both processed and unprocessed records.

Has anyone got some advice on how to approach this problem?

regards, tom lane

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Herouth Maoz 1998-06-15 16:17:18 Re: [SQL] Indexing on a boolean field?
Previous Message Dwight Johnson 1998-06-15 08:23:21 Re: [SQL] Postgresql-Perl -->PQexec() -- there is no connection to the backend