Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id 7B9F6101438B for ; Thu, 1 Mar 2012 13:50:23 -0400 (AST) Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1S3A8y-0005eY-9k for pgsql-performance@postgresql.org; Thu, 01 Mar 2012 17:50:22 +0000 Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id q21Ho2iv014668; Thu, 1 Mar 2012 12:50:02 -0500 (EST) To: "Kevin Grittner" cc: "=?ISO-8859-15?Q?Marcin=20Miros*aw?=" , pgsql-performance@postgresql.org Subject: Re: [planner] Ignore "order by" in subselect if parrent do count(*) In-reply-to: <4F4F54910200002500045D59@gw.wicourts.gov> References: <4F4F6158.7060200@mejor.pl> <4F4F54910200002500045D59@gw.wicourts.gov> Comments: In-reply-to "Kevin Grittner" message dated "Thu, 01 Mar 2012 10:50:57 -0600" Date: Thu, 01 Mar 2012 12:50:02 -0500 Message-ID: <14667.1330624202@sss.pgh.pa.us> From: Tom Lane X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201203/12 X-Sequence-Number: 46462 "Kevin Grittner" writes: > Marcin Miros*aw wrote: >> SELECT count(*) >> from (select * from users_profile order by id) u_p; >> "order by id" can be ignored by planner. > This has been discussed before. Certainly not all ORDER BY clauses > within query steps can be ignored, so there would need to be code to > determine whether it was actually useful, which wouldn't be free, > either in terms of planning time or code maintenance. It wasn't > judged to be worth the cost. If you want to avoid the cost of the > sort, don't specify ORDER BY where it doesn't matter. Considering that ORDER BY in a subquery isn't even legal per spec, there does not seem to be any tenable argument for supposing that a user wrote it there "by accident". It's much more likely that he had some semantic reason for it (say, an order-sensitive function in a higher query level) and that we'd break his results by ignoring the ORDER BY. I doubt that very many of the possible reasons for needing ordered output are reliably detectable by the planner, either. regards, tom lane