Received: from localhost (unknown [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 69A8C9F9B5A for ; Fri, 31 Aug 2007 11:54:21 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 76258-04 for ; Fri, 31 Aug 2007 11:54:06 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.5 Received: from el-out-1112.google.com (el-out-1112.google.com [209.85.162.183]) by postgresql.org (Postfix) with ESMTP id E48C59FA5BA for ; Fri, 31 Aug 2007 11:54:14 -0300 (ADT) Received: by el-out-1112.google.com with SMTP id r27so247466ele for ; Fri, 31 Aug 2007 07:54:11 -0700 (PDT) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:user-agent:mime-version:to:cc:subject:references:in-reply-to:content-type:content-transfer-encoding; b=XYprLP7k2FpZFjhZukZGkTxeOBneDbIV5tgbVntgBUKvvYXm/coUtMOfR21weJNR0LMwhuh9i4BB3GFeiIc7chzPkfqJijM7/hBztPhJnQJh2ZjX5jU7eWRSpN3VMVGZyQoS2nNaI9gei9Wezy2Shw4+L+eXkebiVE/EIBxb5jU= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:user-agent:mime-version:to:cc:subject:references:in-reply-to:content-type:content-transfer-encoding; b=KEt/cAVovvYKjO/8MxwHfLXi6shY2OO+5jRfXLJ/A9sgFeUrwuKi2H2wE+Q9kxxzMIq4e8uP9y0BaTYYOAYYx00lAfo8WT5z2GxwHmy1JFzry50YK8M1dbldMyU0SDPnSuBBu42nfhT9BaSYTc8iyuwH3u6MkE4F+mPLCiGJl4U= Received: by 10.100.10.20 with SMTP id 20mr1618745anj.1188572048961; Fri, 31 Aug 2007 07:54:08 -0700 (PDT) Received: from ?10.0.0.75? ( [213.170.101.138]) by mx.google.com with ESMTPS id b23sm3525195ugd.2007.08.31.07.53.59 (version=SSLv3 cipher=RC4-MD5); Fri, 31 Aug 2007 07:54:00 -0700 (PDT) Message-ID: <46D82B86.6060600@gmail.com> Date: Fri, 31 Aug 2007 18:53:58 +0400 From: Aleksandr Vinokurov User-Agent: Thunderbird 2.0.0.6 (Windows/20070728) MIME-Version: 1.0 To: Tom Lane CC: pgsql-sql@postgresql.org Subject: Re: Incomprehensible dogged sort in Merge Join References: <46D82318.3070700@gmail.com> <24061.1188571419@sss.pgh.pa.us> In-Reply-To: <24061.1188571419@sss.pgh.pa.us> Content-Type: text/plain; charset=windows-1251; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200708/262 X-Sequence-Number: 29200 Tom Lane wrote: > Aleksandr Vinokurov writes: >> Trying to get an extra time savings in my query, I stopped at an unusual >> doggedness of the planner. > >> Merge Left Join >> Merge Cond: ("outer".name = "inner".name) >> -> Sort >> Sort Key: log.name >> -> Seq Scan on log_example_3 log >> -> Sort >> Sort Key: uh.name >> -> Subquery Scan uh >> -> Sort >> Sort Key: name >> -> Seq Scan on user_history > >> The strange thing is that planner can combine two sorts by uh.name key >> in one, but it seems it can't see this. > >> May be this can be recorded as a needed feature for future releases? > > When criticizing planner deficiencies, it's considered polite to use > something that's less than two major releases back ;-) Sorry, it was blown out from my head at the end of composing: my version is 8.0.1, not so old, IMHO. > > CVS HEAD gets this right, although I need to go look at why it's > sticking a Materialize in there: > > regression=# explain select * from (select * from tenk1 order by twothousand) uh right join tenk1 log on log.thousand = uh.twothousand; > QUERY PLAN > ----------------------------------------------------------------------------------- > Merge Right Join (cost=4575.77..6225.77 rows=100000 width=488) > Merge Cond: (tenk1.twothousand = log.thousand) > -> Sort (cost=2287.89..2312.89 rows=10000 width=244) > Sort Key: tenk1.twothousand > -> Seq Scan on tenk1 (cost=0.00..458.00 rows=10000 width=244) > -> Materialize (cost=2287.89..2412.89 rows=10000 width=244) > -> Sort (cost=2287.89..2312.89 rows=10000 width=244) > Sort Key: log.thousand > -> Seq Scan on tenk1 log (cost=0.00..458.00 rows=10000 width=244) > (9 rows) > > [ pokes at it a bit more... ] 8.1 and 8.2 get it right for a plain > join, but not for an outer join. Strange, I need to check that too. > > regards, tom lane > Best wishes, Aleksandr.