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 1nJyJ8-0003t7-6l for pgsql-general@arkaria.postgresql.org; Tue, 15 Feb 2022 13:55:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nJyJ6-0008SA-Qf for pgsql-general@arkaria.postgresql.org; Tue, 15 Feb 2022 13:55:52 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nJyJ6-0008S1-2j for pgsql-general@lists.postgresql.org; Tue, 15 Feb 2022 13:55:52 +0000 Received: from mail-wr1-x431.google.com ([2a00:1450:4864:20::431]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nJyJ1-0005Gi-Qw for pgsql-general@lists.postgresql.org; Tue, 15 Feb 2022 13:55:51 +0000 Received: by mail-wr1-x431.google.com with SMTP id d27so28479263wrb.5 for ; Tue, 15 Feb 2022 05:55:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; h=message-id:date:mime-version:user-agent:subject:content-language:to :cc:references:from:in-reply-to:content-transfer-encoding; bh=VMLDPplPOur2Zubywj61gAG+L3+PY+9GhkpKTU8b+6k=; b=dkrwODqcbiirxfq1eJDQW0bIRsfVtoO1PqdIyhfM9DUYCH2fstSHq2mUe65pPdwnMV LNJDGfs4+8smHjliYDTIhPnM3d0RvrOk8kEzpZaSBLhYiT9YQrQKHsUglt3tcHHr1nc+ qhOoOAUnxTnuaW+PayR0S0KJKo3bu74eNgST9O0t9qO77W9K5yqEIKApdgeZfo+Izv+m yyPJ5PZVfhjO1hB99y01FXQFLJQukRS2sotSuIuF5cYIyKH2fFvm4pOV77i2PVQGCnqF 57O5IOVXBTnS/Z5HZ7jOBzdDIhXmtE8LFQAsuvyOvN4Q+9NdcWaN25T4/p9MuhOZPOpl r8rg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:in-reply-to :content-transfer-encoding; bh=VMLDPplPOur2Zubywj61gAG+L3+PY+9GhkpKTU8b+6k=; b=1my78lwKz1oLXArkPGOKihzL5VkQ7neKGHc20EGksQ8KMtMUDn7ZdPzRLUzUf5iuPl KV+KMEyF1PPB9FeAJ6p3tEE8fmPjxE8gbv1DD+FXbNGOteUjjnJaC+qdrB4ZCN0OTydn 38yah4hi5Vr6BACSOfED6FE1VSG94kaffTdaCcYn9CNy1GiHboqDkV4CYLzmDtkmRO8q yq943csJMlQgvJTRp4B4TfH9+Jplm3At3sYp+JhIrcRkQYnAzMAAdaZBs8Qn8onPjqw3 Cw6eJ3JpFpEs0D0WBzZuZYz4cPUOjrnRZiGzfq7kEv4njdHLjdwEjuzrOB1r74vXRt0w RNlg== X-Gm-Message-State: AOAM532bJ3sqU5gfgmrVC/9A8oC/DVT/sm6w30OENE1+ZLmAwK8DcIci 5892/YC1zJlnPSOjItBeK97gONlL31WSPJ+RUQLic7eaUFaAb+K5iqCbUP82CLScmJFVMBI1odS Fk1g+K1aHggioughHb7z4SGHNYWCRpnm65oDD6XZWCU9HoaKgHtShHxRw40XLn1sJqgLHtI6WmQ FB3qOWkTeBTcdszbx37k1vSaophVRir2Nq/3Hu533f+XskIwehNDAw X-Google-Smtp-Source: ABdhPJxR58DYScU2UuK+vlj1+aZELOYxfhIUZW4NZyjY7OiDrXKUHz3eQY9gCRO5ODRkdxKW+jDEKA== X-Received: by 2002:adf:e34c:: with SMTP id n12mr3391745wrj.263.1644933346284; Tue, 15 Feb 2022 05:55:46 -0800 (PST) Received: from [10.137.0.18] (static-84-42-175-93.net.upcbroadband.cz. [84.42.175.93]) by smtp.gmail.com with ESMTPSA id m31sm16264412wms.34.2022.02.15.05.55.45 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 15 Feb 2022 05:55:45 -0800 (PST) Message-ID: Date: Tue, 15 Feb 2022 14:55:42 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.5.0 Subject: Re: increasing effective_cache_size slows down join queries by a factor of 4000x Content-Language: en-US To: A Shaposhnikov Cc: pgsql-general References: <747d822c-eae1-df4e-f4c3-099a5a00a721@enterprisedb.com> From: Tomas Vondra In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-CLOUD-SEC-AV-Info: enterprisedb,google_mail,monitor X-CLOUD-SEC-AV-Sent: true X-Gm-Spam: 0 X-Gm-Phishy: 0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 2/15/22 01:06, A Shaposhnikov wrote: > > Interestingly I have a second PG 14.2 database, with identical table > definitions, but about 10% smaller row counts, and the exact same query > works fast there without the 2nd condition: > Are you sure about the 10%? Because in the plans from the first machine I see this: > > -> Index Scan using team_pkey on team t (cost=0.57..11382381.88 > rows=78693167 width=175) (actual time=0.016..0.695 rows=854 loops=1) > while the second machine does this: > > -> Index Scan using team_pkey on team t (cost=0.57..2366113.83 > rows=2807531 width=160) (actual time=0.031..0.801 rows=888 loops=1) > That's 2.8M vs. 78M, quite far from "10% difference". Not sure about team_aliases table, that's imposible to say from the plans. This may matter a lot, because we use effective cache size to calculate cache hit ratio for the query, with relation sizes as an input. So smaller relations (or larger effective_cache_size) means cheaper random I/O, hence preference for nested loop join. The other thing is data distribution - that may matter too. IMO it's pointless to investigate this further - we know what's causing the issue. The optimizer is oblivious that merge join will have to skip large part of the second input, due to the implicit condition. Notice that adding the condition changes the cost from: Limit (cost=81.33..331.82 rows=1000 width=183) ... to Limit (cost=81.33..720.48 rows=1000 width=183) ... So it seems *more* expensive than the first plan. Taken to the extreme the planner could theoretically have chosen to use the first plan (and delay the condition until after the join). regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company