Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gxD9b-00049z-NH for pgsql-performance@arkaria.postgresql.org; Fri, 22 Feb 2019 15:54:24 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gxD9a-0005rX-Df for pgsql-performance@arkaria.postgresql.org; Fri, 22 Feb 2019 15:54:22 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gxD9a-0005rP-0C for pgsql-performance@lists.postgresql.org; Fri, 22 Feb 2019 15:54:22 +0000 Received: from mail-io1-xd42.google.com ([2607:f8b0:4864:20::d42]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gxD9W-0005sX-TR for pgsql-performance@postgresql.org; Fri, 22 Feb 2019 15:54:21 +0000 Received: by mail-io1-xd42.google.com with SMTP id k21so2050445ior.13 for ; Fri, 22 Feb 2019 07:54:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=kPLxs5kWA9pAXwHFBoaW1v1D8+COYezkA7y8Nm0ZZUU=; b=tMrqHU4hVTji3U0XC56o5GflEFrmw3U4MH38RaaHbhMSS+KBVP8KpkFXQFytRpW1ZE 8TWhawVhuehJnNNhIB8n2TSBoJZaBzCZnqIfPfMEFptubfrINuMUTuk7c2neJ1UTDdNH nUOJamszRKYb7kDtY8axAfvw30AbZPDvftPwS5/WA7AjkeKMlFx6U+q++b36eI/FzRvU Fzo80gYtsjljp3KeuNQM/gvrECurvv9R4eHGv1N/qGgZnPwqp1ZNXSr7tGHF7TKwuXLK M/X0g4+fY/d0u7A4X8GEIZLJeFKkPCTeSckIPDClwidwHEoO37piiHwopUSOnaUiibxv KsCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=kPLxs5kWA9pAXwHFBoaW1v1D8+COYezkA7y8Nm0ZZUU=; b=X7kFMoPI9qc9JSJb6tXA1eXWyU6V/J/pmImuZac2hIwhtmsD8c8lob8WlBPvWGh3bC f0MO/Iwa9Dss6hYxvkmc5knLMGt7LXie8ny0IPdh8L7czR0QwwT3lptRliKgFlfB0ZKi tCnWG6y8Jr0b2rp+LJJRKLS8Mm9bJMSmbfcCnmnkcYDkuqf0vY8y8uD3krpRey/p4Nzb 0KRnIuMiuLYkse0hAg3Muz39uFMmUVrG098/5GojcvvuZL9uDcQOpmqHwVoscdkeT1aU rhroJyaXwI8mMc1dpfSB4uwOzNgk/jnrNB2w0kMfmCOccQUdiMda3Kcwh9qhaW8GGh3b ctOQ== X-Gm-Message-State: AHQUAuZG4mSZeG2FUSPb5bkFMjWQlPa3NRWmMaxr8PtyY7jJ67oFHYfG ieM4hAMfP2ajOfR7Naazxa6Gyw== X-Google-Smtp-Source: AHgI3IZep7073/OfIMmyQikejJFHCskaAzO5GM6sn3oWvN9Mb6iVvL4aBLZJpSvV5jbOU0XuDx3FkA== X-Received: by 2002:a6b:dd1a:: with SMTP id f26mr2627341ioc.10.1550850856817; Fri, 22 Feb 2019 07:54:16 -0800 (PST) Received: from pryzbyj (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id r65sm834284itb.19.2019.02.22.07.54.15 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 22 Feb 2019 07:54:15 -0800 (PST) Received: by pryzbyj (Postfix, from userid 1000) id 29D44800B9A; Fri, 22 Feb 2019 09:54:15 -0600 (CST) Date: Fri, 22 Feb 2019 09:54:15 -0600 From: Justin Pryzby To: kimaidou Cc: pgsql-performance@postgresql.org Subject: Re: Aggregate and many LEFT JOIN Message-ID: <20190222155415.GE28750@telsasoft.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.23 (2014-03-12) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Fri, Feb 22, 2019 at 04:14:05PM +0100, kimaidou wrote: > Explain shows that the GROUP AGGREGATE and needed sort kill the performance. > Do you have any hint how to optimize this ? > https://explain.depesz.com/s/6nf This is writing 2GB tempfile, perhaps the query would benefit from larger work_mem: |Sort (cost=3,014,498.66..3,016,923.15 rows=969,796 width=1,818) (actual time=21,745.193..22,446.561 rows=1,212,419 loops=1) | Sort Method: external sort Disk: 1782200kB | Buffers: shared hit=5882951, temp read=230958 written=230958 This is apparently joining without indices: |Nested Loop Left Join (cost=1.76..360,977.37 rows=321,583 width=1,404) (actual time=0.080..1,953.007 rows=321,849 loops=1) | Join Filter: (tgc1.groupe_nom = t.group1_inpn) | Rows Removed by Join Filter: 965547 | Buffers: shared hit=1486327 This perhaps should have an index on tgc2.groupe_type ? |Index Scan using t_group_categorie_pkey on taxon.t_group_categorie tgc2 (cost=0.14..0.42 rows=1 width=28) (actual time=0.002..0.002 rows=1 loops=321,849) | Index Cond: (tgc2.groupe_nom = t.group2_inpn) | Filter: (tgc2.groupe_type = 'group2_inpn'::text) | Buffers: shared hit=643687 This would perhaps benefit from an index on tv.cd_ref ? |Index Scan using taxref_consolide_non_filtre_cd_nom_idx on taxon.taxref_consolide_non_filtre tv (cost=0.42..0.63 rows=1 width=94) (actual time=0.002..0.002 rows=1 loops=690,785) | Index Cond: (tv.cd_nom = t.cd_ref) | Filter: (tv.cd_nom = tv.cd_ref) | Buffers: shared hit=2764875 I don't think it's causing a significant fraction of the issue, but for some reason this is overestimating rowcount by 2000. Do you need to VACUUM ANALYZE the table ? |Seq Scan on occtax.personne p_1 (cost=0.00..78.04 ROWS=2,204 width=56) (actual time=0.011..0.011 ROWS=1 loops=1) Justin