X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 463B43A5F88 for ; Thu, 27 Jan 2005 20:03:01 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 05191-06 for ; Thu, 27 Jan 2005 20:02:51 +0000 (GMT) Received: from wproxy.gmail.com (wproxy.gmail.com [64.233.184.197]) by svr1.postgresql.org (Postfix) with ESMTP id 2951D3A5FF1 for ; Thu, 27 Jan 2005 20:02:51 +0000 (GMT) Received: by wproxy.gmail.com with SMTP id 40so290403wri for ; Thu, 27 Jan 2005 12:02:51 -0800 (PST) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:reply-to:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:references; b=KEXKIKzyR1DTD/BcDRHVyfcq+ebs9pdrx1j6WQfvo21r7eG/01s8H+bNXXx/ZQiKLCET9JVETUTga2HnDw3Rtnu8boPELnSGRgWkMLcHGhkd1oDQ44eKd61FIqdDHhQIRv/yPZy+INIKorfv9eaEFK9kgwFe+TD+eA+c8uVDLHw= Received: by 10.54.59.30 with SMTP id h30mr150304wra; Thu, 27 Jan 2005 12:02:48 -0800 (PST) Received: by 10.54.20.47 with HTTP; Thu, 27 Jan 2005 12:02:48 -0800 (PST) Message-ID: <1dc7f0e305012712024c1c8d30@mail.gmail.com> Date: Thu, 27 Jan 2005 15:02:48 -0500 From: Alexandre Leclerc Reply-To: Alexandre Leclerc To: Merlin Moncure Subject: Re: Flattening a kind of 'dynamic' table Cc: PERFORM In-Reply-To: <6EE64EF3AB31D5448D0007DD34EEB3412A75F0@Herge.rcsinc.local> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit References: <6EE64EF3AB31D5448D0007DD34EEB3412A75F0@Herge.rcsinc.local> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.024 tagged_above=0 required=5 tests=AWL, RCVD_BY_IP X-Spam-Level: X-Archive-Number: 200501/555 X-Sequence-Number: 10245 On Thu, 27 Jan 2005 13:02:48 -0500, Merlin Moncure wrote: > Alexandre wrote: > > On Thu, 27 Jan 2005 10:44:45 -0500, Merlin Moncure > > wrote: > > > Alexandre wrote: > > > Let's start with the normalized result set. > > > > > > product_id | department_id | sum > > > 924 a 6000 > > > 924 c 1575 > > > 907 a 1500 > > > [...] > > > Right. I expanding departments into columns is basically a dead end. > First of all, SQL is not really designed to do this, and second of all > (comments continued below) Ok, I got it. The basic message is to avoid making columns out of rows like I'm doing right now, that "de-normalizing" in an array is the way to go. So I should query and get the results in an array then after my application will parse the array into the good columns. (I'm developping a software.) If I still got it wrong, this is because the 'geek' section of my brain is in vacation: leave a message and when it'll come back, it'll explain all this to me! :) So I found the array_accum function in the doc, so I did create it. CREATE AGGREGATE array_accum ( sfunc = array_append, basetype = anyelement, stype = anyarray, initcond = '{}' ); Then I created this new select: SELECT product_id, array_accum(department_id) as a_department_id, array_accum(req_time) as a_req_time FROM (SELECT * FROM design.product_department_time) AS tmp GROUP BY product_id; It gives: product_id | a_department_id | a_req_time ------------+-----------------+------------------ 924 | {A,C} | {6000,1575} 907 | {A,C,D} | {1500,1500,4575} 906 | {A,C,D} | {3000,3000,1935} So, the performance should be much better using this agregate approach? No I thing I'll merge the results in my software, unless you think that at this point doing a LEFT JOIN with my jobs table is the way to go, beacuse the performance will be good. (Personally I don't know the answer of this one.) > If parsing an array string is a pain I happen to have a C++ class handy > that can compose/decompose a postgresql array string if: > a: no more than 1 dimension and > b: array bounds are known > > Let me know if you need it and I'll send it over. Thank you for your offer. I think parsing an array is the easiest thing to do for me in all this. :) If I encounter any problem, I'll drop you a mail. Regards. -- Alexandre Leclerc