Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 9C2F8633FD0 for ; Mon, 13 Sep 2010 11:26:40 -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 79712-09 for ; Mon, 13 Sep 2010 14:26:33 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id C1C72632B42 for ; Mon, 13 Sep 2010 11:26:32 -0300 (ADT) 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 o8DEQSjq011778; Mon, 13 Sep 2010 10:26:28 -0400 (EDT) To: azajac@ang.com.pl cc: pgsql-performance@postgresql.org Subject: Re: Problem with mergejoin performance In-reply-to: <000001cb5307$78955480$69bffd80$@com.pl> References: <000001cb5307$78955480$69bffd80$@com.pl> Comments: In-reply-to message dated "Mon, 13 Sep 2010 07:49:45 +0200" Date: Mon, 13 Sep 2010 10:26:28 -0400 Message-ID: <11777.1284387988@sss.pgh.pa.us> From: Tom Lane 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/54 X-Sequence-Number: 40257 writes: > Merge Join (cost=18.90..20.85 rows=1 width=8) (actual time=614.912..614.912 > rows=0 loops=1) > Merge Cond: (rr.id = ze.id) > -> Index Scan using bug_t2_i1 on bug_t2 rr (cost=0.00..17893.49 > rows=278417 width=4) (actual time=0.023..351.945 rows=278417 loops=1) > -> Sort (cost=18.88..18.89 rows=4 width=4) (actual time=0.164..0.164 > rows=1 loops=1) > Sort Key: ze.id > Sort Method: quicksort Memory: 17kB > -> Index Scan using bug_t1_i1 on bug_t1 ze (cost=0.00..18.84 > rows=4 width=4) (actual time=0.059..0.141 rows=4 loops=1) > Index Cond: (ids = 94543) > Total runtime: 615.003 ms > I think that problem is with estimation of total mergejoin time, why is it > so small (18.90..20.85) while estimates of subqueries (especially first) is > high (0..17893). Merging time should be high, because it needs to scan > almost all bug t2 table. Am I right? Actually, a mergejoin can stop short of processing all of either input, if it exhausts the keys from the other input first; and the planner knows that. In this case it evidently thinks that the maximum key from bug_t1 is much less than the maximum key from bug_t2, so that most of the indexscan on bug_t2 won't have to be executed. With only 4 rows in bug_t1 it doesn't seem very likely that it would get this wrong. What exactly are those join key values, and what are the min/max values in bug_t2? regards, tom lane