Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id DFEF72E30C7 for ; Fri, 14 Mar 2008 16:46:30 -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 68966-04 for ; Fri, 14 Mar 2008 16:46:27 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from mail01.enterprisedb.com (mail01.enterprisedb.com [63.246.7.168]) by postgresql.org (Postfix) with ESMTP id 71DE42E30B6 for ; Fri, 14 Mar 2008 16:46:27 -0300 (ADT) thread-index: AciGDCxGVL1W4TFKTDWKAcBZhRM9yg== Received: from [192.168.1.33] ([86.27.180.141]) by mail01.enterprisedb.com over TLS secured channel with Microsoft SMTPSVC(6.0.3790.3959); Fri, 14 Mar 2008 15:46:59 -0400 Content-Class: urn:content-classes:message Importance: normal X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.4133 Message-ID: <47DAD5F8.6060804@enterprisedb.com> Date: Fri, 14 Mar 2008 19:46:00 +0000 From: "Heikki Linnakangas" Organization: EnterpriseDB User-Agent: Mozilla-Thunderbird 2.0.0.9 (X11/20080110) MIME-Version: 1.0 To: "Kynn Jones" Cc: Subject: Re: The "many nulls" problem References: In-Reply-To: Content-Type: text/plain; format=flowed; charset="ISO-8859-1" Content-Transfer-Encoding: 7bit X-OriginalArrivalTime: 14 Mar 2008 19:47:00.0359 (UTC) FILETIME=[2B42D970:01C8860C] X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200803/315 X-Sequence-Number: 29269 Kynn Jones wrote: > In all these cases, the design choice, at least according to RDb's 101, is > between including a column in the table that will be NULL most of the time, > or defining a second auxiliary column that references the first one and > holds the non-redundant information for the minority of cases for which this > is necessary (and maybe define a VIEW that includes all the columns). > > But for me it is a frequent occurrence that my quaint and simple RDb's 101 > reasoning doesn't really apply for PostgreSQL. Basically, Pg is too smart > for it! For example, does a large proportion of NULLs really imply a lot of > wasted space? It depends. If there's *any* NULLs on a row, a bitmap of the NULLs is stored in the tuple header. Without NULL bitmap, the tuple header is 23 bytes, and due to memory alignment, it's always rounded up to 24 bytes. That one padding byte is "free" for use as NULL bitmap, so it happens that if your table has eight columns or less, NULLs will take no space at all. If you have more columns than that, if there's *any* NULLs on a row you'll waste a whole 4 or 8 bytes (or more if you have a very wide table and go beyond the next 4/8 byte boundary), depending on whether you're on a 32-bit or 64-bit platform, regardless of how many NULLs there is. That's on 8.3. 8.2 and earlier versions are similar, but the tuple header used to be 27 bytes instead of 23, so you have either one or five "free" bytes, depending on architecture. In any case, that's pretty good compared to many other RDBMSs. > Maybe this is true for fixed-length data types, but what > about for type TEXT or VARCHAR? Datatype doesn't make any difference. Neither does fixed vs variable length. > What's your schema design approach for such situations? How would you go > about deciding whether the number of exceptional cases is small enough to > warrant a second table? Of course, one could do a systematic profiling of > various possible scenarios, but as a first approximation what's your > rule-of-thumb? From performance point of view, I would go with a single table with NULL fields on PostgreSQL. -- Heikki Linnakangas EnterpriseDB http://www.enterprisedb.com