Received: from magus.postgresql.org ([87.238.57.229]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TIz2k-0005P3-Dw for pgsql-sql@postgresql.org; Tue, 02 Oct 2012 09:45:34 +0000 Received: from mail-qc0-f174.google.com ([209.85.216.174]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TIz2g-0007rZ-Kg for pgsql-sql@postgresql.org; Tue, 02 Oct 2012 09:45:33 +0000 Received: by qchd3 with SMTP id d3so4424702qch.19 for ; Tue, 02 Oct 2012 02:45:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=references:in-reply-to:mime-version:content-type:message-id :content-transfer-encoding:cc:x-mailer:from:subject:date:to; bh=g9f2RdftDhkBZD3AQ3vRWScG26Vi0BA59uaE8qV9jWQ=; b=LixuWVfp4SCsvnKtHugJlOUnls65byrKhAo4dt/8RyDpzzCQdD6DRs5vzvDJ77iLU0 x2C+IKhPI0gsxkzQJAq8rSGZE/7ouCedCNCOLw0V3Bhfp6RIo8xqkCPeZJnLihdYErxF ad5Vrge7+/JHVYU5qT+gftBK2g4/+Gx5omHJKr/pfI4Q6N8fhGHMcFVhKz+q8h7ox5v8 pF4OGI6c3SRr+boioqZdPRVdxQZJLjltb9DhaJS9Us5ELeUhjFo0dgQUhAJe0yvbkoRh XnQ4bZjBOKO+FAPYyA4cfYOKe8hrBZK2rgH+maiEXbugMZ0daLa12NwkTmEzp2oV+i3c M1TQ== Received: by 10.229.136.82 with SMTP id q18mr11527872qct.108.1349171127810; Tue, 02 Oct 2012 02:45:27 -0700 (PDT) Received: from [10.170.75.24] (mobile-198-228-205-173.mycingular.net. [198.228.205.173]) by mx.google.com with ESMTPS id y17sm1015730qaa.2.2012.10.02.02.45.23 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 02 Oct 2012 02:45:27 -0700 (PDT) References: <001201cda03a$49a686d0$dcf39470$@yahoo.com> In-Reply-To: Mime-Version: 1.0 (1.0) Content-Type: multipart/alternative; boundary=Apple-Mail-06EE7F4C-3ED1-4601-A6F3-A1A128B8A320 Message-Id: <2E4F715D-9957-4576-94DD-63D42C39ADDC@gmail.com> Content-Transfer-Encoding: 7bit Cc: Thomas Kellerer , "pgsql-sql@postgresql.org" X-Mailer: iPad Mail (9B206) From: Robert Buck Subject: Re: [noob] How to optimize this double pivot query? Date: Tue, 2 Oct 2012 05:45:32 -0400 To: Samuel Gendler X-Pg-Spam-Score: -2.7 (--) X-Archive-Number: 201210/11 X-Sequence-Number: 36882 --Apple-Mail-06EE7F4C-3ED1-4601-A6F3-A1A128B8A320 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii Hi Samuel=20 Thank you. This may be a bit of a stretch for you, but would it be possible f= or me to peek at a sanitized version of your cross tab query, for a good exa= mple on how to do this for this noob? This will be pretty common in my case. The biggest tables will get much larg= er as they are raw metrics feeds, which at some point need to be fed through= reporting engines to analyze and spot regressions. Lastly, am I simply using the wrong tech for data feeds and analytics? The f= irst cut of this used flat files and R and though it scoured thousands of fi= les was much faster than the SQL I wrote here. The big goal was to get this o= ff disk and into a database, but as its highly variable, very sparse, metric= data, this is why I chose k-v. SQL databases are internally more politicall= y acceptable, though I am personally agnostic on the matter. In the end it w= ould be nice to directly report off a database, but so long as I can transfo= rm to csv I can always perform reporting and analytics in R, and optionally m= ap and reduce natively in Ruby. Sane? Ideas? This is early on, and willing t= o adjust course and find a better way if suggestions indicate such. I've hea= rd a couple options so far. Best regards, Bob On Oct 2, 2012, at 5:21 AM, Samuel Gendler wrote= : >=20 >=20 > On Mon, Oct 1, 2012 at 11:46 PM, Thomas Kellerer wrot= e: >=20 > That combined with the tablefunc module (which let's you do pivot queries)= might > make your queries substantially more readable (and maybe faster as well). >=20 >=20 > I woud think that using the crosstab functions in tablefunc would solve th= e problem without needing a complete change of structure. I've built crossta= bs over a whole lot more than 54K rows in far, far less time (and resulting i= n more than 35 columns, too) than the 11 seconds that was quoted here, witho= ut feeling the need to deal with hstore or similar. In fact, wouldn't hstor= e actually make it more difficult to build a crosstab query than the schema t= hat he has in place now? >=20 > --sam >=20 --Apple-Mail-06EE7F4C-3ED1-4601-A6F3-A1A128B8A320 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8
Hi Samuel 
=
Thank you. This may be a bit of a stretch for you, but would i= t be possible for me to peek at a sanitized version of your cross tab query,= for a good example on how to do this for this noob?

This will be pretty common in my case. The biggest tables will get much la= rger as they are raw metrics feeds, which at some point need to be fed throu= gh reporting engines to analyze and spot regressions.

Lastly, am I simply using the wrong tech for data feeds and analytics? Th= e first cut of this used flat files and R and though it scoured thousands of= files was much faster than the SQL I wrote here. The big goal was to get th= is off disk and into a database, but as its highly variable, very sparse, me= tric data, this is why I chose k-v. SQL databases are internally more politi= cally acceptable, though I am personally agnostic on the matter. In the end i= t would be nice to directly report off a database, but so long as I can tran= sform to csv I can always perform reporting and analytics in R, and optional= ly map and reduce natively in Ruby. Sane? Ideas? This is early on, and willi= ng to adjust course and find a better way if suggestions indicate such. I've= heard a couple options so far.

Best regards,

Bob<= /div>

On Oct 2, 2012, at 5:21 AM, Samuel Gendler <sgendler@ideasculptor.com> wrote:<= br>


On Mon, Oct 1, 2012 at 11:46 PM, Thomas Kellerer <spam_eater@= gmx.net> wrote:

That combined with the tablefunc module (which let's you do pivot queries) m= ight
make your queries substantially more readable (and maybe faster as well).

I woud think that using the crosstab fu= nctions in tablefunc would solve the problem without needing a complete chan= ge of structure. I've built crosstabs over a whole lot more than 54K rows in= far, far less time (and resulting in more than 35 columns, too) than the 11= seconds that was quoted here, without feeling the need to deal with hstore o= r similar.  In fact, wouldn't hstore actually make it more difficult to= build a crosstab query than the schema that he has in place now?

--sam

= --Apple-Mail-06EE7F4C-3ED1-4601-A6F3-A1A128B8A320--