Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Z2hNM-0006hs-Aq for pgsql-performance@arkaria.postgresql.org; Wed, 10 Jun 2015 14:53:08 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Z2hNL-0007e6-S2 for pgsql-performance@arkaria.postgresql.org; Wed, 10 Jun 2015 14:53:07 +0000 Received: from makus.postgresql.org ([174.143.35.229]) by malur.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Z2hNL-0007e0-1M for pgsql-performance@postgresql.org; Wed, 10 Jun 2015 14:53:07 +0000 Received: from mail-wg0-f44.google.com ([74.125.82.44]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1Z2hLE-0007OH-7J for pgsql-performance@postgresql.org; Wed, 10 Jun 2015 14:50:58 +0000 Received: by wgbgq6 with SMTP id gq6so37797164wgb.3 for ; Wed, 10 Jun 2015 07:50:54 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:message-id:date:from:user-agent:mime-version:to :subject:references:in-reply-to:content-type :content-transfer-encoding; bh=4/wfhmVdyVXXTTPHgbfSeXR9tMsiYWpyJ+Pd/0VsbIY=; b=Z2/LOW7VVLRbP1QHGecG24nXisUcQICar5OppmroD7o18FqlJqVaEaOmp4eZJ7OIRe 29EhHwfxWqYcsSq4UxSpUKEFzXq2OxF+BHDPo1iot6z8sBuyWE2eK18wG7L+zfS7WEFv 90svkiJQXmOWwPm0esa3em3mOXQdzq37C4EX0AvOSPQSYeKmfDh7GrAqhkDPe86yUv5M AYcZL1nh1bAaNBbglGqirjN55sO/Wf75D+ey/OOoKk1B7uN2Gs6x4q9dza9woQMwLoK/ 7mb2UXyEOsfOu7H01nApB7m3Zl809uzbr6MCilV64D+WVB5gsTi5dXmSBGp/zYkEdwnP 4kzw== X-Gm-Message-State: ALoCoQnWncJG+0UVtXWrYdBTU9t+u2v20wPwOhDxYhvJEF6WtEIT2m3ziOYg2yzeBDzJMD2nXesJ7Ubsdn/GYvfEmGWgR+Hnh2E6v56uoXrpsTdbYsclAoHoE3X9tzvJMoeKTOE23G5w6iwg6o/c6ftavA9ne5KncW38u+4hUXK6GEbdgObroQv6acmpSBkJSSTj5sKVT1qmGfBwJIJ85eYMknEy+8c0FQ== X-Received: by 10.194.59.79 with SMTP id x15mr6714046wjq.81.1433947854836; Wed, 10 Jun 2015 07:50:54 -0700 (PDT) Received: from [10.137.2.12] (ip-89-176-99-192.net.upcbroadband.cz. [89.176.99.192]) by mx.google.com with ESMTPSA id di9sm8335818wib.16.2015.06.10.07.50.53 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 10 Jun 2015 07:50:53 -0700 (PDT) Message-ID: <55784ECA.6000406@2ndquadrant.com> Date: Wed, 10 Jun 2015 16:50:50 +0200 From: Tomas Vondra User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: Re: Slow query - lots of temporary files. References: In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit 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 On 06/10/15 15:42, Johann Spies wrote: > On 10 June 2015 at 15:02, Claudio Freire > wrote: > > > The joins are different on both versions, and the most likely culprit > is the join against D. It's probably wrong, and the first query is > building a cartesian product. > > Without more information about the schema it's difficult to be sure > though. > > > Thanks for your reply. I will experiment futher with different joins. I don't know what you mean by "experimenting with joins" - that should be determined by the schema. The problematic piece of the explain plan is this: -> Merge Join (cost=4384310.92..21202716.78 rows=6664163593 width=390)" Output: a.ut, c.gt, b.go, b.gn, d.au" Merge Cond: ((c.ut)::text = (d.rart_id)::text)" That is, the planner expects ~6.7 billion rows, each ~390B wide. That's ~2.5TB of data that needs to be stored to disk (so that the sort can process it). The way the schema is designed might be one of the issues - ISTM the 'ut' column is somehow universal, mixing values referencing different columns in multiple tables. Not only that's utterly misleading for the planner (and may easily cause issues with huge intermediate results), but it also makes formulating the queries very difficult. And of course, the casting between text and int is not very good either. Fix the schema to follow relational best practices - separate the values into multiple columns, and most of this will go away. regards -- Tomas Vondra http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance