pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: PT <wmoran@potentialtech.com>
To: hmidi slim <hmidi.slim2@gmail.com>
Cc: pgsql-general@postgresql.org
Subject: Re: Using enum instead of join tables
Date: Tue, 10 Apr 2018 18:48:58 -0400
Message-ID: <20180410184858.a8d8874485a520bdc2ecf0ae@potentialtech.com> (raw)
In-Reply-To: <CAMsqVxsmpWTqzCrL2PP2G4-6dCSUF3PjroRYLmAeF9tGFWjCHA@mail.gmail.com>
References: <CAMsqVxsmpWTqzCrL2PP2G4-6dCSUF3PjroRYLmAeF9tGFWjCHA@mail.gmail.com>

On Tue, 10 Apr 2018 11:24:49 +0100
hmidi slim <hmidi.slim2@gmail.com> wrote:

> Hi,
> Is it a good thing to use enum type such a column in a table instead of
> making a foreign key which references to another table?
> I found these links talking about enum and when I will use them:
> http://komlenic.com/244/8-reasons-why-mysqls-enum-data-type-is-evil/
> https://www.percona.com/blog/2008/01/24/enum-fields-vs-varchar-vs-int-joined-table-what-is-faster/
> 
> Is there any advices or new updates to use enum type in order to decrease
> the number of join between the table and optimize the performance and query
> runtime?

Performance isn't always the only issue.

Enums work well if you're 100% sure that the values will never change.
While it's not impossible to change them, it's essentially a schema change,
which can be tricky to do on an active databse, due to locking.

A foreign table has the advantage of being easy to change, but with the
extra join required to get the text representation.

A foreign table with a text field doesn't require the join, but takes up
more space and requires a cascading change if you need to change an enum
value. Adding new values is pretty easy, though.

Another option is a text field with a check constraint to ensure the data
in it stays valid. This is somewhere in between as changing the check constraint
is easier than with an enum, but harder than with a foriegn table. It doesn't
require a join to get the text representation of the value, but takes up more
space (depending on the lenght of the text for each value).

So you have to balance the requirements of your use case to decide what method
is best.

-- 
Bill Moran




view thread (2+ messages)

Message-ID: <20180410184858.a8d8874485a520bdc2ecf0ae@potentialtech.com>
Permalink:  ../20180410184858.a8d8874485a520bdc2ecf0ae@potentialtech.com/
Also on:    postgresql.org/message-id/20180410184858.a8d8874485a520bdc2ecf0ae@potentialtech.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: wmoran@potentialtech.com, hmidi.slim2@gmail.com
  Subject: Re: Using enum instead of join tables
  In-Reply-To: <20180410184858.a8d8874485a520bdc2ecf0ae@potentialtech.com>
  References: <CAMsqVxsmpWTqzCrL2PP2G4-6dCSUF3PjroRYLmAeF9tGFWjCHA@mail.gmail.com> <20180410184858.a8d8874485a520bdc2ecf0ae@potentialtech.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox