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 1tvYH6-005m6a-I5 for pgsql-hackers@arkaria.postgresql.org; Fri, 21 Mar 2025 09:02:44 +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 1tvYH4-002Gnj-T5 for pgsql-hackers@arkaria.postgresql.org; Fri, 21 Mar 2025 09:02:42 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tvYH4-002GnZ-Gb for pgsql-hackers@lists.postgresql.org; Fri, 21 Mar 2025 09:02:42 +0000 Received: from mail-wr1-x429.google.com ([2a00:1450:4864:20::429]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tvYH2-000HjN-2c for pgsql-hackers@lists.postgresql.org; Fri, 21 Mar 2025 09:02:41 +0000 Received: by mail-wr1-x429.google.com with SMTP id ffacd0b85a97d-390cf7458f5so1275997f8f.2 for ; Fri, 21 Mar 2025 02:02:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1742547758; x=1743152558; darn=lists.postgresql.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=hcbAmuQmyTvWOLrrrJnw94S6zBCQdRu4ItgL1EwFj6k=; b=blbk+NVNW4HkMibbVtTbFEyvQI4Vkw8LRDUaeXPOANTH9pyfghpTZzzaG6UPS9IeuK HC1XNyOs9p2Aq/8aj24t/EnHekuOFzQCEYuRZehnpOlxuv4/cMynmeew2gfJyWuoVTHI tuzogzM9eDz1Rf6ebvKKwE5s6coo1IqM5K8+pkczzBOtevYdFgtfqmnK+8Nek0kNp0CW 6uepJXTfe72I3/x3eVrq8mKUNWK1bciCDlMDrdiUJ+m12tglR7cXmkyLZ497iCihmsEA W0hsKkR81ELB4kc60nmrOOE/fSW9vyDZdQftqwIiqumDBNXm26ieAMvBa4//u86zuh/l i9Qw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742547758; x=1743152558; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=hcbAmuQmyTvWOLrrrJnw94S6zBCQdRu4ItgL1EwFj6k=; b=UGATKu/05OBvFabr9ndgTfhRGpU57+wY+4+KZeQxcYoKGdxeXdBSzisL9SNZJU+kzb 3ZVm8V+lglP62DuAhUFA/7jsuV4GqvYr+1fnq/KLxg1XVbuEKYy7Bddh6I9v3dLAG93l PqFaOW49fWiKiqM2yBxBayedkEm5NZCEy7wSgkkLnmG5lJpWX7/jdU6j1hrb4y/USGv8 +3ERDDrl0SacyeoJ7J1WxQyX5/t8Z1Ldg2plxS8AJ8BQWrK6R71pxEPi50L8jcR/oguh JGxx/VH7suPG/oESd1LNHbxnV9BQdVEJUxVrIR8YLa3v4pBH835ErkYmM1ia8vAzodg1 wCRg== X-Forwarded-Encrypted: i=1; AJvYcCUyAtZGragxu3jeve1+YKVieLQiR2vlisfCNQrn8sNRI30NIve+/MAQpux8upDrSwiT5PxlUUlfDhBqAnxt@lists.postgresql.org X-Gm-Message-State: AOJu0YzMftWOF3oNz3uEY/qU/w4QXQGxP1uLmf+t7SUQL8rdb70HXwwJ OAiVkj9HkziWUXi+ritbnoVFzPcO8EDj2WaQeAo39z3e3GHE5nAV X-Gm-Gg: ASbGncv7WTpsgphIcUvi80nsa0xBaUBBEKWNjKFfRp0EMu6maMfVTwNVyVEjuQiGYCo 2m0MCEFHLh0n3JpC/J7H5yvJvYd18srO1ODeRqKF4eVZD8A0z0NhAYc8Q28niP/RuUS6DfFfEUy TsBCQrmMkUX9EBL2DMT+JB16mKsD2bj1O8HNmofrEZ9wOYNMI764Gj75Cw4oPOrVEb4ZTR6cfv2 PTGwR8L8h7ev75rqTlnjiGIBxrK/O+obnk302ClGSWR9emo2VGWq96l0ProA/8bi32ye4XmeNC9 AtIS75NLUn3NAaMRXLb0X+XWYEa9EKmZhhI0xGcLD1KHKOPaDbHx X-Google-Smtp-Source: AGHT+IHfZ7m6KEspQUHmry8GmXnBWPb9dzcnIX9EzZYhrpu8Og+B5IrtzMa4F6pm8/KskMs12ROQhw== X-Received: by 2002:a5d:64af:0:b0:391:2c0c:1247 with SMTP id ffacd0b85a97d-3997f8f8c14mr2658395f8f.1.1742547757596; Fri, 21 Mar 2025 02:02:37 -0700 (PDT) Received: from [10.59.3.23] ([90.121.90.131]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43d4fdb03e9sm20351045e9.33.2025.03.21.02.02.36 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 21 Mar 2025 02:02:37 -0700 (PDT) Message-ID: Date: Fri, 21 Mar 2025 10:02:36 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Add estimated hit ratio to Memoize in EXPLAIN to explain cost adjustment To: David Rowley Cc: Ilia Evdokimov , Lukas Fittl , Daniel Gustafsson , PostgreSQL Hackers References: <51A15CD7-E31B-483D-B911-D0EB9F5FF952@yesql.se> <44229379-3901-4cb0-8812-b354ef70d5e2@gmail.com> <8e12aa55-39eb-4e03-a8f7-077fe02309c3@gmail.com> Content-Language: en-US From: Andrei Lepikhov In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 21/3/2025 03:50, David Rowley wrote: > On Fri, 21 Mar 2025 at 07:54, Andrei Lepikhov wrote: >> I have some doubts here. >> The number of distinct values says something only when it has taken >> together with the number of calls. > > Couldn't the reader just look at the Nested Loop's outer side row > estimate for that? In my cases, key sources are usually spread across dozens of joins, and it is visually hard to find out (especially when we have an EXPLAIN ANALYSE VERBOSE) the JOIN operator to extract the number of calls. The hit ratio, meanwhile, may be analysed locally in the Memoize node. For example, 80% (0.8) is evidently a good one, 40% is questionable, and 5% is too low and we should avoid Memoize here. May it be beaten by just printing the "calls" number at the Memoize output? > >> Frequently, one of the caching keys is from outer table A (10 tuples), >> and another is from outer table B (100 tuples). Calculating the number >> of successful cache fetches predicted by the planner may not be evident >> in the case of a composite cache key. >> >> What I may propose here is: >> 1. Use fraction of calls, for example - 50% duplicated key values. >> 2. Show the calculated hit and eviction ratio. > > I think the primary factors in how useful Memoize is are: 1) How many > items do we expect to be able to store in the cache concurrently, and; > 2) How many unique lookups keys do we expect to be looked up, and; 3) > The total number of expected lookups. #1 is quite difficult to > figure out (maybe by looking at row width and row estimates) and > there's just no information about #2. #3 is already shown, in the > Nested Loop's outer side. It depends on the task. If you are looking for the answer to how precise the group's estimation has been (to check statistics), I agree. In cases I have seen before, the main question is how effective was (or maybe) a Memoize node == how often the incoming key fits the cache. In that case, the hit ratio fraction is more understandable for a broad audience. That's why according to my experience in case of a good cache reusability factor, users are usually okay with increasing the cache size to the necessary numbers and avoiding evictions at all costs. So, the predicted evict_ratio also tells us about incrementing work_mem to enhance the chances of Memoisation. Having written the last sentence I came back to the point why work_mem is so universal and is used at each node as a criteria of memory allocation size? But it is a different story, I think. -- regards, Andrei Lepikhov