Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lOLwk-0007bk-Uc for pgsql-performance@arkaria.postgresql.org; Mon, 22 Mar 2021 14:54:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lOLwj-0008QZ-Ra for pgsql-performance@arkaria.postgresql.org; Mon, 22 Mar 2021 14:54:21 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lOLwj-0008QS-BX for pgsql-performance@lists.postgresql.org; Mon, 22 Mar 2021 14:54:21 +0000 Received: from mail-ej1-x632.google.com ([2a00:1450:4864:20::632]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lOLwe-0000g2-87 for pgsql-performance@postgresql.org; Mon, 22 Mar 2021 14:54:20 +0000 Received: by mail-ej1-x632.google.com with SMTP id r12so21773884ejr.5 for ; Mon, 22 Mar 2021 07:54:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20150623.gappssmtp.com; s=20150623; h=message-id:subject:from:to:date:in-reply-to:references:user-agent :mime-version:content-transfer-encoding; bh=TcabnnYaQRFbFRV+JZYX5HYFMnDLVeonuG2Cf70xIN8=; b=CkFDRRfyJczA+RfVJqNPHdRAa7daVXN2feU3MfHQVZ3mt1i15rsc2zHSKtEmt7aYs2 ZmIVCv2ACix+tSU+by9hZSb6TEbSPiF54/QGPnPDrWhOKt5i82JToOEpYEJgYxPfa6ga uH5pfE0lFCcMGpPTHQ2wZJfiAkASn3LJy/4nkc4z/oA4+QNLSRF3iNz0byjFX3ecgh5x 4pnMeeuiNhqpyzNLRpCP3s0+myG283qMRzZfFNVvci3nRjPDZd5CvwPQnXcizPVDvC/9 nsYIwlIvkKSrVmbvRS8FKiwElF6NM6JS200OAaw7K6sij+OqHdez0ILGY23H2bBZxYt7 9AnA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:subject:from:to:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=TcabnnYaQRFbFRV+JZYX5HYFMnDLVeonuG2Cf70xIN8=; b=tQrCZarAdHoToyJnPEcpHDoyi0bIaD//O0YXlVE+aSdD6QxPVkjGAJSjCyaOTY9f7k SG0cOsnTpO8kGzFazYMHwJwXKPf5HNnRr7DNgcqI42U31oDoc8X5S7lih0fH4IDmX1i6 fBCQdr+qlyp9Ff7C3QNf2jqPVtCi/cXRlVnRc9Xfq4RAdBAGDLUJzNzJcENUj9Ui/kGt q8ewjRlqeKZ3JDOPe1pdPOrNbUh9QrK/mSxPY+HshroEwzS1+DaxfknimgLo8tHymVkI eCuNKO67LEihK+a0FysBLc3hUtd/ZZbwZY02IrYg9iP+lS+N1K9nFnfDemeU3P2tWQ7b OErQ== X-Gm-Message-State: AOAM531WaO+J9JXf5RzbI7Y/TZw19i+LXcMnqPjmp6a/JhaOALjxo/EY zXi1nDlzABWRuZ7AGPOF2Rcp+A== X-Google-Smtp-Source: ABdhPJzvKrlhcuUVfXn0CnI0bO4mkdN8kIrieLSeLXW5ZNI3WuBPys35ew37yRBcOAoUR7SGnSGPlg== X-Received: by 2002:a17:906:fcb2:: with SMTP id qw18mr141428ejb.434.1616424854521; Mon, 22 Mar 2021 07:54:14 -0700 (PDT) Received: from localhost.localdomain (217-149-168-217.nat.highway.telekom.at. [217.149.168.217]) by smtp.gmail.com with ESMTPSA id z17sm9821624eju.27.2021.03.22.07.54.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Mar 2021 07:54:14 -0700 (PDT) Message-ID: Subject: Re: SQL performance issue (postgresql chooses a bad plan when a better one is available) From: Laurenz Albe To: Chris Stephens , pgsql-performance@postgresql.org Date: Mon, 22 Mar 2021 15:54:13 +0100 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.36.5 (3.36.5-2.fc32) MIME-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Mon, 2021-03-22 at 08:10 -0500, Chris Stephens wrote: > The following SQL takes ~25 seconds to run. I'm relatively new to postgres > but the execution plan (https://explain.depesz.com/s/N4oR) looks like it's > materializing the entire EXISTS subquery for each row returned by the rest > of the query before probing for plate_384_id existence. postgres is > choosing sequential scans on sample_plate_384 and test_result when suitable, > efficient indexes exist. a re-written query produces a much better plan > (https://explain.depesz.com/s/zXJ6). Executing the EXISTS portion of the > query with an explicit PLATE_384_ID yields the execution plan we want as > well (https://explain.depesz.com/s/3QAK). unnesting the EXISTS and adding > a DISTINCT on the result also yields a better plan. Great! Then use one of the rewritten queries. Yours, Laurenz Albe -- Cybertec | https://www.cybertec-postgresql.com