Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 879D8633246 for ; Mon, 13 Sep 2010 13:24:01 -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 92077-08 for ; Mon, 13 Sep 2010 16:23:52 +0000 (UTC) Received: from news.hub.org (news.hub.org [200.46.204.72]) by mail.postgresql.org (Postfix) with ESMTP id E951863252D for ; Mon, 13 Sep 2010 13:23:52 -0300 (ADT) Received: from news.hub.org (news.hub.org [200.46.204.72]) by news.hub.org (8.14.4/8.14.4) with ESMTP id o8DGNqeg094198 for ; Mon, 13 Sep 2010 13:23:52 -0300 (ADT) (envelope-from news@news.hub.org) Received: (from news@localhost) by news.hub.org (8.14.4/8.14.4/Submit) id o8DFtmrV085721 for pgsql-performance@postgresql.org; Mon, 13 Sep 2010 12:55:48 -0300 (ADT) (envelope-from news) From: Gaetano Mendola X-Newsgroups: pgsql.performance Subject: Re: Useless sort by Date: Mon, 13 Sep 2010 17:55:46 +0200 Organization: Hub.Org Networking Services Lines: 32 Message-ID: <4C8E4982.70004@gmail.com> References: <12103.1284389088@sss.pgh.pa.us> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@news.hub.org To: Tom Lane User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.10pre) Gecko/20100911 Shredder/3.1.5pre In-Reply-To: <12103.1284389088@sss.pgh.pa.us> To: pgsql-performance@postgresql.org X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-10 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201009/57 X-Sequence-Number: 40260 On 09/13/2010 04:44 PM, Tom Lane wrote: > Gaetano Mendola writes: >> because of that sort in the view definition the first query above >> takes not less than 3 seconds. I have solved this performance issue >> removing the sort from the view definition and putting it in the >> select reducing the time from > 3secons to < 150ms. > >> Can not the optimizer take rid of that useless sort on those >> kind of queries ? > > It is not the optimizer's job to second-guess the user on whether a sort > is really needed there. If we did make it throw away non-top-level > sorts, we'd have hundreds of users screaming loudly. Of course I'm not suggesting to take away the "sort by" and give the user an unsorted result, I'm asking why the the optimizer in cases like: select unique(a) from v_table_with_order_by; doesn't takes away the "order by" inside the view and puts it back "rewriting the query like this: select unique(a) from v_table_without_order_by order by a; then the user will not know about it. The result is the same but 30 times faster (in my case). Regards Gaetano Mendola