Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id B87EFB5DBD9 for ; Tue, 4 Oct 2011 12:00:46 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 69303-04 for ; Tue, 4 Oct 2011 15:00:40 +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 1E001B5DBC0 for ; Tue, 4 Oct 2011 12:00:39 -0300 (ADT) Received: from mailout (localhost.localdomain [127.0.0.1]) by mailout.thl.fi (8.13.1/8.13.1) with SMTP id p94F0cAK005332; Tue, 4 Oct 2011 18:00:38 +0300 Received: from 172.24.32.92 (172.24.32.92) by mailout (F-Secure/fsigk_smtp/580/mailout); Tue, 4 Oct 2011 18:00:38 +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 p94F0ccf030293; Tue, 4 Oct 2011 18:00:38 +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:00:38 +0300 Message-ID: <4E8B1FA7.50607@thl.fi> Date: Tue, 4 Oct 2011 18:00:55 +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: Tom Lane CC: "pgsql-performance@postgresql.org" Subject: Re: Window functions and index usage References: <4E8AD46F.30003@thl.fi> <7900.1317739011@sss.pgh.pa.us> In-Reply-To: <7900.1317739011@sss.pgh.pa.us> Content-Type: text/plain; charset="ISO-8859-1"; format=flowed Content-Transfer-Encoding: quoted-printable X-MIME-Autoconverted: from 8bit to quoted-printable by mail-hub.thl.fi id p94F0ccf030293 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/46 X-Sequence-Number: 45268 On 10/04/2011 05:36 PM, Tom Lane wrote: > The cost estimates I get are 806 for bitmap scan and sort, 2097 for > seqscan and sort, 4890 for indexscan without sort. It *can* use the > index for that query ... it just doesn't think it's a good idea. It's > probably right, too. At least, the actual runtimes go in the same orde= r > on my machine. Seqscan-and-sort very often beats an indexscan for > sorting a table, unless the table is clustered on the index or nearly s= o. I tested it and yes, it can use the index scan. But not in the way I=20 though it would be used. > Note that it cannot use the index for both ordering and satisfying the > IN condition. If it used the =3DANY clause as an index condition, what > that would imply is three separate index searches and so the results > wouldn't necessarily be correctly ordered. This is why the plain > indexscan costs out so expensive: it's a full-table scan. This I don't understand. I would imagine it would be possible to execute=20 this query as get 5 first values for id 1, get 5 first values for id 2,=20 get 5 first values for id 3. At least if I do this by hand using UNION I=20 get two orders of magnitude faster execution time. I am not saying it=20 would be easy to do that, but to me it seems it would be possible to use=20 the index more efficiently for the example query. Or is the following=20 UNION query not equivalent to the window function query, assuming I am=20 not interested in the row_number column itself? (select id, seq from test where id =3D 1 order by seq limit 5) union (select id, seq from test where id =3D 2 order by seq limit 5) union (select id, seq from test where id =3D 3 order by seq limit 5); The results are in different order, but there is no order by in the=20 original query except in the OVER clause, so it should not matter. - Anssi K=E4=E4ri=E4inen