Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tFhlz-00FEcu-NO for pgsql-performance@arkaria.postgresql.org; Mon, 25 Nov 2024 22:41:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tFhly-001JMr-DK for pgsql-performance@arkaria.postgresql.org; Mon, 25 Nov 2024 22:41:38 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tFhly-001JMi-2L for pgsql-performance@lists.postgresql.org; Mon, 25 Nov 2024 22:41:38 +0000 Received: from mail-lf1-x12d.google.com ([2a00:1450:4864:20::12d]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1tFhlv-003qEE-Is for pgsql-performance@lists.postgresql.org; Mon, 25 Nov 2024 22:41:37 +0000 Received: by mail-lf1-x12d.google.com with SMTP id 2adb3069b0e04-53dd8b7796dso3524493e87.1 for ; Mon, 25 Nov 2024 14:41:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1732574494; x=1733179294; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=vJi9yrVqlhDgayePvrA/saltTKtmzjP4JoWsRqBAjhE=; b=niF7hjENhqYDUnAoHOptXG/aN+5ylcg5fKRc+UsxExLhhcvZXQ7RiJVIsMLaJaP0j4 KIf7Q86T2RgkiBOvIups0m6M4kg2moz4ouuFWM1UHZo6c/1I8BpIDcYA6tjQ9NtIHp1Q WtbJPoRE5qZZFB5kHICRDB9uLCPNraJkEAruOG+dgtBhnGkr/WCHH/sqT932FK9ze1+2 B96vo+SNu4s9h7lsJu+A2nIgLikHH9Oy0yIul0VSc5yuajxDT2WAnyWZ1VCvITJFckpY WRQKtx4GzH0M9ZQ58apTsuVQ+TKwNFx7kqV/05uXSs/eVj9lDXnSKC6q7tz0+fYn33Eu K8cQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732574494; x=1733179294; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=vJi9yrVqlhDgayePvrA/saltTKtmzjP4JoWsRqBAjhE=; b=c6eeiXIVHAqp8kLPRh+H1OK1HEA5GnuEXMyvFhudaxsEBM5hhGN0Z5AsAR8KULngSs saHQerB5PBdyoAim6pRF1YKsrXwmG3j/OATiPENudeIRE9vMDdEv9S6B/lbcvgj2I3SO hEaJb8F2egBS553+PMDJ/zptksJ7VX6Nh9jCMonmrYek9ZLb9ZwoR0EyDWLx3syRVD2P evbcmb5diyHB3lubrG1jgrM2Z2kfQwSPwcQ98dT5uiI/ujrBKfkjFpUQhE3i6mCA36ZO 0/zHuuDk5/ktK3wyyO1Ym2dC058YcU6AgX9ksGb/5WITakL5Qqccwxix8oq7+oignJqa /lDQ== X-Forwarded-Encrypted: i=1; AJvYcCWicDr8dNu0OxWBx6nRAVgOg+tLbBB81isyarv8TDJph68S1drdXf6akRbtcNzux2SgNkUCN4TJZwrLapLopUQAbA==@lists.postgresql.org X-Gm-Message-State: AOJu0YxtlmAMDRWtruyB2Azs1AOHzzTOT7ZSAKvA/ZMFa7SZT3ymoA0U J96pAklM8UmFTe69l6sb6x1rIe7m7XGeV2Y7kxIZGirRKo6Ad5PVkMbFvPAMOlrBh765hZ5Q600 or2s4X91mFT3qk1KG/QimyM0JPEsY13hy X-Gm-Gg: ASbGnctnDZtIMX+O3m+1RXV9de5mXITQulYJV2XVfhtWeSevMsc/QxT3ABbzg08VkBz MqNuf7XTcVU+c+J4DxjI13DBhAFafpbdwJYwpaiFF4gc+615vSTcLjrQYVfULy0io X-Google-Smtp-Source: AGHT+IFhmPADX3CzwXEcFw99+adhWFvTD0NQHtzhGwFB2sWraUgP/WVLlfKTQwz/7x7Uj2c026nQOp6ZNPe6wrszeTk= X-Received: by 2002:a05:6512:ad3:b0:53d:6b50:f613 with SMTP id 2adb3069b0e04-53dd35a45b9mr6879626e87.3.1732574494109; Mon, 25 Nov 2024 14:41:34 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: David Rowley Date: Tue, 26 Nov 2024 11:41:22 +1300 Message-ID: Subject: Re: Performance of TPC-DS Query 95 To: Ba Jinsheng Cc: Andrei Lepikhov , "pgsql-performance@lists.postgresql.org" Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Tue, 26 Nov 2024 at 09:55, Ba Jinsheng wrote: > TPC-DS query 95: > Its execution time is nearly 1 min: > CTE ws_wh > -> Hash Join (cost=37772.14..198810.77 rows=7242361 width=12) (actual time=211.161..1443.926 rows=6644004 loops=1) > If applying this patch: > - if (is_innerrel_unique_for(root, joinrelids, outerrelids, innerrel, > + if (!is_innerrel_unique_for(root, joinrelids, outerrelids, innerrel, > The execution time is reduced to 6 seconds: > CTE ws_wh > -> Hash Join (cost=37772.14..74062.53 rows=7095248 width=12) (actual time=203.407..560.264 rows=719205 loops=1) > The difference between both query plans is the second one uses Materialize instead of Memoize. From the code, it seems that changing the usage of the cache brings performance improvement unexpectedly. What's going on here is that you've introduced a new bug which has the Hash Join effectively perform a semi-join rather than an inner join so that it only joins the first matching inner-side row instead of all matching inner-side rows for each outer-side row. The bug is fairly evident when to see that the Hash Join produces 6644004 rows without your change and only 719205 rows after you added the bug. I'm not sure what you expect us to do here, but just in case you're not aware, we're not going to prioritise performance over correct results. I expect the problem you've introduced will be more evident if you use EXPLAIN ANALYZE VERBOSE. The verbose option will show which joins are marked "Inner Unique". If you want to learn more about the optimisation you've broken, see [1]. David [1] https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=9c7f5229ad68d7e0e4dd149e3f80257893e404d4