From: | Jan de Visser <jan(at)de-visser(dot)net> |
---|---|
To: | pgsql-general(at)postgresql(dot)org |
Cc: | Robert Nikander <rob(dot)nikander(at)gmail(dot)com> |
Subject: | Re: use null or 0 in foreign key column, to mean "no value"? |
Date: | 2015-06-27 04:31:27 |
Message-ID: | 2600101.Y9m2JbqpjT@bison |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
On June 26, 2015 11:59:05 PM Robert Nikander wrote:
> Hi,
>
> (Maybe my subject line should be: `is not distinct from` and indexes.)
>
> In Postgres 9.4, I’ve got a table of ‘items’ that references a table
> ‘colors’. Not all items have colors, so I created a nullable column in
> items like:
>
> color_id bigint references colors
>
> There is also an index on color_id:
>
> create index on items (color_id);
>
> I thought this was the right way to do it, but now I’m not so sure... In
> application code, prepared statements want to say: `select * from items
> where color_id = ?` and that `?` might be a int or null, so that doesn’t
> work. I used `is not distinct from` instead of =, which has the right
> meaning, but now I notice it doesn’t use the index for queries that replace
> `=` with `is not distinct from`, and queries run much slower. Using
> `explain` confirms: it’s doing sequential scans where `=` was using index.
I test for NULL in my application code and emit '... WHERE foo = ?' if the
value is not NULL and '... WHERE foo IS NOT NULL' otherwise. ISTR that that
actually uses indexes.
>
> So… is this bad DB design to use null to mean that an item has no color?
> Should I instead put a special row in `colors`, maybe with id = 0, to
> represent the “no color” value? Or is there some way to make an index work
> with nulls and `is not distinct from`?
>
> thank you,
> Rob
From | Date | Subject | |
---|---|---|---|
Next Message | David G. Johnston | 2015-06-27 04:41:11 | Re: use null or 0 in foreign key column, to mean "no value"? |
Previous Message | Adrian Klaver | 2015-06-27 04:08:55 | Re: How to convert a text variable into a timestamp in postgreSQL? |