Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Wl8Lb-0002kY-Or for pgsql-performance@arkaria.postgresql.org; Fri, 16 May 2014 02:58:11 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Wl8Lb-0000uX-5a for pgsql-performance@arkaria.postgresql.org; Fri, 16 May 2014 02:58:11 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Wl8La-0000uR-3q for pgsql-performance@postgresql.org; Fri, 16 May 2014 02:58:10 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Wl8LX-0006yA-Js for pgsql-performance@postgresql.org; Fri, 16 May 2014 02:58:09 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id s4G2w2D2021695; Thu, 15 May 2014 22:58:02 -0400 From: Tom Lane To: "Huang, Suya" cc: "pgsql-performance@postgresql.org" Subject: Re: same query different execution plan (hash join vs. semi-hash join) In-reply-to: References: Comments: In-reply-to "Huang, Suya" message dated "Fri, 16 May 2014 02:38:22 -0000" Date: Thu, 15 May 2014 22:58:02 -0400 Message-ID: <21694.1400209082@sss.pgh.pa.us> X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org "Huang, Suya" writes: > I've got a query as below, it runs several times with different execution plan and totally different execution time. The one using hash-join is slow and the one using semi-hash join is very fast. However, I have no control over the optimizer behavior of PostgreSQL database. Or, do I have? A salient feature of the slow plan is that the planner is misinformed about the size of test1: > -> Seq Scan on test1 (cost=0.00..5153.94 rows=63294 width=516) (actual time=0.068..91.378 rows=441736 loops=1) whereas in the fast plan its rows estimate for that scan is dead on. It looks like the two cases also have different ideas of how many distinct values are in the test1.userid column, though this is more a guess than an indisputable fact. In short, I suspect you're recreating the test1 table and not bothering to ANALYZE it after you fill it. This leaves you at the mercy of when the autovacuum daemon gets around to analyzing the table before you'll get good plans for it. regards, tom lane -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance