Received: from localhost (maia-4.hub.org [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id E8DE89F9726 for ; Sun, 19 Aug 2007 15:12:52 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 34132-01-2 for ; Sun, 19 Aug 2007 15:12:48 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from fetter.org (start.fetter.org [66.92.188.65]) by postgresql.org (Postfix) with ESMTP id 6281D9F9B1B for ; Sun, 19 Aug 2007 15:12:18 -0300 (ADT) Received: by fetter.org (Postfix, from userid 500) id 6FFF1FBC840; Sun, 19 Aug 2007 11:12:16 -0700 (PDT) Date: Sun, 19 Aug 2007 11:12:16 -0700 From: David Fetter To: mark overmeer Cc: pgsql-performance@postgresql.org Subject: Re: schema design question Message-ID: <20070819181216.GK28490@fetter.org> References: MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.5.14 (2007-02-12) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200708/196 X-Sequence-Number: 26326 On Sun, Aug 19, 2007 at 03:19:52PM +0200, mark overmeer wrote: > Hi, > > Maybe not completely the wright place to ask but... I have this > schema design question (db is postgres of course). I have a couple > of classes with attributes. Danger, Will Robinson! Danger! The DBMS way of looking at things is fundamentally different from OO coding, and if you try to make them fit together naïvely as you do below, you only get grief. > The only goal is to search the object > that I want to find (which is stored on the harddrive). > > I have hundreds of classes that are similar but not the same. They all have > attributes/properties (type is probably String), e.g. (in pseudo code): > > class A_version_1 { > attribute1, attribute2, attribute3, ..., attributeN > } > > class A_version_2 { > attribute1, attribute3, ..., attributeN, attributeN+1, attributeN+2 > } > > class B_version_1 { > attribute3, attribute4, attribute7, attributeN+3, ..., attributeN+M > } > > > Class A will have attributes from class B, class B will have > attributes from class C and so on. My initial thought was to use the > (sometimes dreaded) EAV model: class_id, object_id, attribute_id and > attribute_value. In this way I can make queries like: > > SELECT CLASS_ID, > OBJECT_ID > FROM EAV_TABLE EAV There's your mistake. EAV is not performant, and won't become so. Decide what your database will and won't do, and design your schema around that. I know it takes a little extra helping of courage, but it's worth it in the long run. Cheers, David. -- David Fetter http://fetter.org/ phone: +1 415 235 3778 AIM: dfetter666 Skype: davidfetter Remember to vote! Consider donating to PostgreSQL: http://www.postgresql.org/about/donate