Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f624a-00060U-Lz for pgsql-general@arkaria.postgresql.org; Tue, 10 Apr 2018 22:49:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1f624Z-0008Nq-4o for pgsql-general@arkaria.postgresql.org; Tue, 10 Apr 2018 22:49:07 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f624Y-0008Ng-TE for pgsql-general@lists.postgresql.org; Tue, 10 Apr 2018 22:49:06 +0000 Received: from virtual1.potentialtech.com ([69.25.178.43] helo=mail.potentialtech.com) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f624U-0003vY-Ts for pgsql-general@postgresql.org; Tue, 10 Apr 2018 22:49:06 +0000 Received: from beezelbub (c-67-171-70-156.hsd1.pa.comcast.net [67.171.70.156]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.potentialtech.com (Postfix) with ESMTPSA id A5AA924BEB7; Tue, 10 Apr 2018 18:48:59 -0400 (EDT) Date: Tue, 10 Apr 2018 18:48:58 -0400 From: PT To: hmidi slim Cc: pgsql-general@postgresql.org Subject: Re: Using enum instead of join tables Message-Id: <20180410184858.a8d8874485a520bdc2ecf0ae@potentialtech.com> In-Reply-To: References: X-Mailer: Sylpheed 3.5.0 (GTK+ 2.24.30; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Tue, 10 Apr 2018 11:24:49 +0100 hmidi slim 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