Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 5D43AB5DBD9 for ; Tue, 4 Oct 2011 12:05:02 -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 44716-07 for ; Tue, 4 Oct 2011 15:04:56 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from mailout.thl.fi (mailout.thl.fi [193.167.195.59]) by mail.postgresql.org (Postfix) with ESMTP id B8FB4B5DBC7 for ; Tue, 4 Oct 2011 12:04:55 -0300 (ADT) Received: from mailout (localhost.localdomain [127.0.0.1]) by mailout.thl.fi (8.13.1/8.13.1) with SMTP id p94F4rYM005365; Tue, 4 Oct 2011 18:04:53 +0300 Received: from 172.24.32.92 (172.24.32.92) by mailout (F-Secure/fsigk_smtp/580/mailout); Tue, 4 Oct 2011 18:04:53 +0200 (EET) X-Virus-Status: clean(F-Secure/fsigk_smtp/580/mailout) Received: from exhub1.stakes.fi (exhub1.stakes.fi [10.10.3.203]) by mail-hub.thl.fi (8.13.8/8.13.8) with ESMTP id p94F4rPM030398; Tue, 4 Oct 2011 18:04:53 +0300 Received: from [172.24.70.235] (172.24.70.235) by exhub1.stakes.fi (10.10.3.203) with Microsoft SMTP Server id 8.3.83.0; Tue, 4 Oct 2011 18:04:53 +0300 Message-ID: <4E8B20A6.4090104@thl.fi> Date: Tue, 4 Oct 2011 18:05:10 +0300 From: =?ISO-8859-1?Q?Anssi_K=E4=E4ri=E4inen?= User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.23) Gecko/20110921 Lightning/1.0b2 Thunderbird/3.1.15 MIME-Version: 1.0 To: Robert Klemme CC: "pgsql-performance@postgresql.org" Subject: Re: Window functions and index usage References: <4E8AD46F.30003@thl.fi> <4E8B12F5.20308@thl.fi> In-Reply-To: Content-Type: text/plain; charset="ISO-8859-1"; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.404 tagged_above=-10 required=5 tests=BAYES_00=-1.9, RP_MATCHES_RCVD=-0.504 X-Spam-Level: X-Archive-Number: 201110/47 X-Sequence-Number: 45269 On 10/04/2011 05:52 PM, Robert Klemme wrote: > But then why do require using the second index column in the first > place? If the data set is small then the query is likely fast if the > selection via id can use any index. I mean the fetched dataset is not large, I didn't mean the dataset in total isn't large. Imagine the commit fest application, but with 10000 comments per patch. You want to fetch the 100 patches in the current commit fest, and 3 latest comments per patch. >> And you don't have to fetch >> them for all threads / patches. You might fetch them only for patches in >> currently viewed commit fest. See >> https://commitfest.postgresql.org/action/commitfest_view?id=12 for one such >> use. What I have in mind is fetching first all the patches in the commit >> fest in one go. Then issue query which would look something like: >> select * from >> (select comment_data, row_number() over (partition by patch_id order by >> comment_date desc) >> from patch_comments >> where patch_id in (list of patch_ids fetched in first query) >> ) tmp where row_number<= 3; > Interesting: I notice that I the query cannot successfully be simplified on 8.4: > > rklemme=> select *, > row_number() over (partition by id order by seq desc) as rn > from test > where id in (1,2,3) > and rn<= 3 > ; That can't be done, where conditions targeting window functions must be done using subquery. There is no difference in 9.1 as far as I know. > Again, what is easy for you as a human will likely be quite complex > for the optimizer (knowing that the order by and the row_number output > align). I am not trying to say it is easy. - Anssi