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.92) (envelope-from ) id 1j6zXs-0003Xt-Ag for pgsql-performance@arkaria.postgresql.org; Wed, 26 Feb 2020 16:28: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 1j6zXr-0004lW-41 for pgsql-performance@arkaria.postgresql.org; Wed, 26 Feb 2020 16:28:23 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1j6zXq-0004lO-J4 for pgsql-performance@lists.postgresql.org; Wed, 26 Feb 2020 16:28:22 +0000 Received: from mail-yw1-xc43.google.com ([2607:f8b0:4864:20::c43]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1j6zXj-0007Xz-J9 for pgsql-performance@postgresql.org; Wed, 26 Feb 2020 16:28:21 +0000 Received: by mail-yw1-xc43.google.com with SMTP id y72so444544ywg.3 for ; Wed, 26 Feb 2020 08:28:15 -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=Tc4WuJUq/nfz3bazISIFNThMFqYN8OnFe+WdlM4gFJg=; b=Ix7+oyO/WiRgfvWR1pSWu3Y+wHVA/zifiTXKIlPz8lno2mVEUqjXnfW0uaz1EcOOCc E6K+77jctPwLHB0jFGATEAF6XLFdUHobH/4L+4ZBDyYeGQtZEuc48qKpQiJzsQvYiYqx I/EsIpFbMmA4Va6U6L2TIoCxhgLPP5A0zEXBrSyZIyF54CEDx5pOMPrSWs7VmGbGvIHw R2wOcnJ0+Q+A6NZV1/GBylSwwZOsSZPctGDqRsdhvsA6Z8E7ueB8rbJhhNPsicC5649r H1DL8NeelpPJgJWBxdBjle3TMVS4QLlBM+7QTQBxmQHZnKz82sVxL8HpIBuo8Q9OqI2C jwZw== 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=Tc4WuJUq/nfz3bazISIFNThMFqYN8OnFe+WdlM4gFJg=; b=E2CohjxtTQxwX2wPId+hiAOARZKhxzoNchDNA8w4C83QdX9VXz0n8hw6q3C55mi3In W66DAIWNQcw8/KlE4mNkVi6FeqyYikYyziRrIM/VRyVjS8ucrfLUCoK9gUOG5CvtGg2a Z9kv4v+bHW4ov36N7lN6fN46KcUrZyRlTdfh7Ak/1I4BdTA84ct8bKZLiZiLjLFnvHT6 zZb5dz+qJyo9YPzJWaFv6060C/UIkq4Q+lgQn4FThRRsLJhsAUIIrU2vHo61c/0gK9XM dOz7YAopkpX8CvqS6C2GzixGx0rfVryiII/Nq3o831SfzJAXyytSBpY6CJWJX7SKaHu5 OzAg== X-Gm-Message-State: APjAAAX7HTVe0x0IerJkdk/yH2dyW+CosE/syPvYqC/EXLp1Dj4JHvjE 76MjjzQfWdk+hKtVmnEltxveFYO8rvA= X-Google-Smtp-Source: APXvYqw61qDzvEt4EbQmoGfyTTrk5z1wE6DqfgwT6Cj2BYf0Pi2f78iyOAV7b/UGoRK/4mSD8Qb92g== X-Received: by 2002:a81:1358:: with SMTP id 85mr4463ywt.418.1582734494272; Wed, 26 Feb 2020 08:28:14 -0800 (PST) Received: from pryzbyj (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id j72sm1177128ywj.60.2020.02.26.08.28.13 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 26 Feb 2020 08:28:13 -0800 (PST) Received: by pryzbyj (Postfix, from userid 1000) id C108E800827; Wed, 26 Feb 2020 10:28:09 -0600 (CST) Date: Wed, 26 Feb 2020 10:28:09 -0600 From: Justin Pryzby To: Guillaume Cottenceau Cc: pgsql-performance@postgresql.org Subject: Re: much slower query in production Message-ID: <20200226162809.GZ31889@telsasoft.com> References: <878skpnx0u.fsf@mnc.ch> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <878skpnx0u.fsf@mnc.ch> User-Agent: Mutt/1.5.24 (2015-08-30) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Wed, Feb 26, 2020 at 05:17:21PM +0100, Guillaume Cottenceau wrote: > On production: > > # EXPLAIN ANALYZE SELECT transaction_uid, (SELECT COUNT(*) FROM tickets WHERE multicard_uid = multicards.uid) from multicards; > QUERY PLAN > --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- > Seq Scan on multicards (cost=0.00..1455177.30 rows=204548 width=12) (actual time=0.178..1694987.355 rows=204548 loops=1) > SubPlan 1 > -> Aggregate (cost=7.07..7.08 rows=1 width=8) (actual time=8.283..8.283 rows=1 loops=204548) > -> Index Only Scan using tickets_multicard_uid on tickets (cost=0.43..7.05 rows=9 width=0) (actual time=1.350..8.280 rows=6 loops=204548) > Index Cond: (multicard_uid = multicards.uid) > Heap Fetches: 1174940 > Planning Time: 1.220 ms > Execution Time: 1695029.673 ms > The execution time ratio is a huge 3700. I guess the Heap Fetches > difference is the most meaningful here; Yes, it's doing an "index only" scan, but not very effectively. Vacuum the tickets table to set relallvisible and see if that helps. If so, try to keep it better vacuumed with something like ALTER TABLE tickets SET (AUTOVACUUM_VACUUM_SCALE_FACTOR=0.005); -- Justin