Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 66231B5DCC2 for ; Sat, 22 Oct 2011 11:28:53 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 34533-02 for ; Sat, 22 Oct 2011 14:28:46 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from nm33-vm5.bullet.mail.ne1.yahoo.com (nm33-vm5.bullet.mail.ne1.yahoo.com [98.138.229.69]) by mail.postgresql.org (Postfix) with SMTP id 6C28EB5DCC0 for ; Sat, 22 Oct 2011 11:28:46 -0300 (ADT) Received: from [98.138.90.51] by nm33.bullet.mail.ne1.yahoo.com with NNFMP; 22 Oct 2011 14:28:45 -0000 Received: from [98.138.87.8] by tm4.bullet.mail.ne1.yahoo.com with NNFMP; 22 Oct 2011 14:28:45 -0000 Received: from [127.0.0.1] by omp1008.mail.ne1.yahoo.com with NNFMP; 22 Oct 2011 14:28:45 -0000 X-Yahoo-Newman-Id: 265781.95128.bm@omp1008.mail.ne1.yahoo.com Received: (qmail 10979 invoked from network); 22 Oct 2011 14:28:45 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=DKIM-Signature:X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:References:Mime-Version:In-Reply-To:X-Apple-Yahoo-Original-Message-Folder:Content-Type:Content-Transfer-Encoding:Message-Id:Cc:X-Mailer:From:X-Apple-Yahoo-Replied-Msgid:Subject:Date:To; b=ptQzzdg4k6N6KQqViAvbAi27KJlIiA94CUR5WUvY/BYak2WtJcuShTLcGY7o3TpuLDVq2xtxG4ngzzsAfRpWCtpwgQ/Rf4FHWwQWCVdeMG0o/gaqF8+fs0wYpYvUuZN6BjpnGLN45jpmdohxLKUrBXjPzE51eolf5U1NUBkI0VU= ; DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1319293725; bh=whHCqBbDkI8nJm63aeNJ/aSxh/hl22SvsFV+SfjgZaM=; h=X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:References:Mime-Version:In-Reply-To:X-Apple-Yahoo-Original-Message-Folder:Content-Type:Content-Transfer-Encoding:Message-Id:Cc:X-Mailer:From:X-Apple-Yahoo-Replied-Msgid:Subject:Date:To; b=2kGDYWmOXKJHRbbCm/4BTxkBzbYlgmz3NXwlO0mUZqSmRV55IQ1ifnEpQIzYYG+W3TMg/yJ3MmL6fB/r1/KPbtOgrGiJWoF+SaYsmFUXr7dYYPijiQelNK4GfPgY4bmXUBzJGydDQcEapvTyMdhwE5XTdYA/7osEITexaqf5UpU= X-Yahoo-Newman-Property: ymail-3 X-YMail-OSG: TVpZMysVM1kFY2COyXQ97U2CRGBW1Gae.7ZcHE86j_F_Okg sZbtD1XSsVhgvuUlP7xFXclgOcUtT44d1iU9n4.SrjF46fS1_C.6uPVfa1V8 PkNN8y904tTpHYKvi0rXQZUhcvfOsDirVI02EBfTexpe5gn8H2IuF6rYqi2u j2khfXLkTT6FkQd_fFdl5YA_gj.RkL1jiK7lgi66r0mYVtFiO0yVYFzCzP6g ttmCJrX_U9UwJx1G8V.8vLn19s9bNnCGzUv.h.PpqUF6.1PGtEVUfE5BQEzD NJIyu7WuY9ZNJc07Sod0gH.KnghaENpTNzfAGejrwnHFi..YX0.qaZ8Befdk 6bA9B7YM9O55lJP_1e2yDM8naiCZ.q2bkM6ukpePtKkAZGal.DhrKPqogrCs i6FlSP4.k.n5x9I5z0XCvbWjvkPf69CtCncfriC3BrnlhFie2ksyv X-Yahoo-SMTP: mpGJl6eswBD2IBufoVEg0Pa8gg-- Received: from [192.12.17.102] (polobo@24.93.23.188 with xymcookie) by smtp110-mob.biz.mail.ne1.yahoo.com with SMTP; 22 Oct 2011 07:28:44 -0700 PDT References: <4EA29DDB.9000300@linos.es> <00655F1E-08DC-42C5-B702-C8D332081557@yahoo.com> Mime-Version: 1.0 (iPad Mail 8J2) In-Reply-To: X-Apple-Yahoo-Original-Message-Folder: Inbox Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Message-Id: Cc: Linos , "pgsql-sql@postgresql.org" X-Mailer: iPad Mail (8J2) From: David Johnston X-Apple-Yahoo-Replied-Msgid: 1_529728_APGkiGIAAFi9TqLOVQO9cmtjNsI Subject: Re: advice on how to store variable attributes Date: Sat, 22 Oct 2011 10:28:48 -0400 To: Pavel Stehule X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.403 tagged_above=-5 required=5 tests=BAYES_00=-1.9, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RP_MATCHES_RCVD=-0.504 X-Spam-Level: X-Archive-Number: 201110/74 X-Sequence-Number: 35958 On Oct 22, 2011, at 10:07, Pavel Stehule wrote: > 2011/10/22 David Johnston : >> On Oct 22, 2011, at 6:41, Linos wrote: >>=20 >>> Hi all, >>> i need a little of advice on what could be the best way to store this= information. >>>=20 >>> We need to calculate the difference in costs for our operations, we are a= lready >>> storing our vendor invoices in the database so calculate the monetary ch= ange it >>> is a no-brainer but we need to store special attributes for any of the i= nvoices >>> that we need to compare too, for example: >>> -electric provider: total Kw. >>> -water provider: total m3. >>> -car maintenance: kilometers of the car. >>> -mobile phones provider: international call minutes, national minutes= , number >>> of sms, etc.. >>>=20 >>> And much more types/variables, the number of variables can change, not e= very day >>> but still can change, i would like that they can be defined/changed from= our >>> application, so alter table to add columns don't seem the best way (stil= l an >>> option though). We will have "generic" reports that will show us changes= in >>> costs and specific reports for the types with "extended attributes" that= we want >>> to compare. >>>=20 >>> To compare values from this "extended attributes" i think we have two wa= ys: >>> 1- have them in columns and use standard SQL. >>> 2- create the columns with a function that reads this attrs and creat= e the columns. >>>=20 >>> So far i thin we have this options: >>> 1- a bunch of columns that would be null except when the type of the i= nvoice >>> uses them. >>> 2- a table related with the vendor invoices table for every type of i= nvoice >>> with his specifics columns. >>> 3- a key/value in a separate table related with the vendor invoices t= able where >>> i store the extended attrs of every invoice that needs them. >>> 4- use a hstore column in the vendor invoces table to store this attr= s. >>>=20 >>> The first two have the problem of probably changes to the number of attr= ibutes >>> of every type and give a more closed solution, apart from that 1- seems t= o be a >>> bit awkward and 2- would need the application that creates the query to k= now >>> with what table should join for every type (other point we will need to c= hange >>> if we want to create new invoices types). >>>=20 >>> The last two have his own problems too, with 3 i will need to create a f= unction >>> that return rows as columns to compare them, with 4- given that i will s= tore the >>> attrs of every type in the database anyway i can use the operator -> (wi= th a >>> CASE using operator ? returning 0 if the searched attr it is not in the h= store) >>> but still don't seem a clean solution for me. >>>=20 >>> For me it seems i am missing something, probably any of you have a much m= ore >>> elegant (or correct) way to handle this situation, what would be your ad= vice? >>> Thanks. >>>=20 >>>=20 >>=20 >> Create a table with a single numeric column and multiple category columns= . >>=20 >> ( amount_value, amount_unit, amount_category, vendor_id ) >=20 >=20 > This is EAV model - is good for smaller datasets, for larger datasets > is problematic. There is second possibility - using a "hstore" contrib > module - that emulates HASH table - It has better for larger datasets. >=20 > Regards >=20 > Pavel Stehule Store was mentioned by the OP. Any suggestions on where the line between small and large is drawn? Partitions could help in the larger cases. My personal first choice is to use separate tables. If going EAV route plan= on eventually moving to the separate table route and at least try to make m= igration relatively easy. Since both models capture the same data the decision at least partially rest= s upon the immediate use-cases for the data. Lacking use-cases and size par= ameters recommendations are difficult to make. I have not used hstore so my ability to recommend it is limited. My main "c= oncern" is the fact that the data is less visible and always stored as text.= I see hstore as being useful for situations where the attributes are varia= ble but this use case seems to have fairly well-defined attributes. David J. >=20 >>=20 >> If necessary each "amount_value" data type should have it's own table sin= ce the processing logic will vary (I.e., you cannot subtract text or Boolean= values). >>=20 >> You are , in effect, creating multiple tables but combining them into one= and using the category column to distinguish between them. >>=20 >> David J. >> -- >> Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) >> To make changes to your subscription: >> http://www.postgresql.org/mailpref/pgsql-sql >>=20 >=20 > --=20 > Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) > To make changes to your subscription: > http://www.postgresql.org/mailpref/pgsql-sql