Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id BFF91476A86 for ; Thu, 12 Sep 2002 12:05:27 -0400 (EDT) Received: from megazone.bigpanda.com (megazone.bigpanda.com [63.150.15.178]) by postgresql.org (Postfix) with ESMTP id C6D8C476A11 for ; Thu, 12 Sep 2002 12:05:24 -0400 (EDT) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id 2E90DD60C; Thu, 12 Sep 2002 09:05:26 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id 2442D5C02; Thu, 12 Sep 2002 09:05:26 -0700 (PDT) Date: Thu, 12 Sep 2002 09:05:26 -0700 (PDT) From: Stephan Szabo To: Gaetano Mendola Cc: Subject: Re: Performance inside and outside view ( WAS Re: Select the In-Reply-To: Message-ID: <20020912090208.Q46162-100000@megazone23.bigpanda.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200209/107 X-Sequence-Number: 9384 On Thu, 12 Sep 2002, Gaetano Mendola wrote: > > "Tom Lane" wrote in message > news:7350.1031842223@sss.pgh.pa.us... > > "Gaetano Mendola" writes: > > > Yes that's right it's help me, > > > but here the optimizer have some problems: > > > > What's the datatype of id_user, and why are you quoting the compared > > constant in some cases but not others? I don't think the difference > > has anything to do with the view here ... > > id_user is an INTEGER type. > > Well what about this two selects: > > a) SELECT DISTINCT ON (id_user) * > FROM user_logs > WHERE id_user = 5430 > ORDER BY id_user, id_user_log DESC; > > b) CREATE VIEW last_user_logs AS > SELECT DISTINCT ON (id_user) * > FROM user_logs > ORDER BY id_user, id_user_log DESC; > > SELECT * > FROM last_user_logs > WHERE id_user = 5430 > > > I think that in the second case the filter is not pushed inside the view. Pushing into distinct on has limited cases where it's legal, you're using one of them, but the system doesn't differentiate and doesn't push in either. select distinct on (col1) * from table where col2=2; and select * from (select distinct on (col1) * from table) v where col2=2; -- this is like a view case are not the same query. I believe Tom makes a comment to the effect that it should be safe if the clause only references distinct on'd columns.