Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id 705BE19B3F2A for ; Mon, 28 Nov 2011 13:32:34 -0400 (AST) Received: from gwmta.wicourts.gov ([165.219.244.99] helo=gw.wicourts.gov) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1RV54D-0002pI-BE for pgsql-performance@postgresql.org; Mon, 28 Nov 2011 17:32:34 +0000 Received: from Courts-MTA by gw.wicourts.gov with Novell_GroupWise; Mon, 28 Nov 2011 11:32:20 -0600 Message-Id: <4ED3713F020000250004355E@gw.wicourts.gov> X-Mailer: Novell GroupWise Internet Agent 8.0.1 Date: Mon, 28 Nov 2011 11:32:15 -0600 From: "Kevin Grittner" To: "Joost Kraaijeveld" Cc: Subject: Re: PostgreSQL 9.1 : why is this query slow? References: <1322498526.3343.22.camel@panoramix.Askesis.nl> <1322499469.3343.25.camel@panoramix.Askesis.nl> <4ED36AF0020000250004354F@gw.wicourts.gov> <1322500982.3343.30.camel@panoramix.Askesis.nl> In-Reply-To: <1322500982.3343.30.camel@panoramix.Askesis.nl> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Content-Disposition: inline X-Pg-Spam-Score: -5.4 (-----) X-Archive-Number: 201111/276 X-Sequence-Number: 45787 Joost Kraaijeveld wrote: > I would like the answer to be "the number of times the word > appears in all three the queries", the intersection of the three > queries. That's still not entirely clear to me. If there are two 'f' rows, three 's' rows, and four 'n' rows, do you want to see an answer of 2 (which seems like the intersection you request here), 9 (which is the sum), 24 (which is the product), or something else? If you really want the intersection, perhaps: with x as ( select word, count(*) as countall, count(case when filetype = 'f' then 1 else null end) as countf, count(case when filetype = 's' then 1 else null end) as as counts, count(case when filetype = 'n' then 1 else null end) as as countn from unique_words ) select word, least(countf, counts, countn) from x where countf > 0 and counts > 0 and countn > 0 order by word; -Kevin