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 1sp8uP-00Gx0F-1E for pgsql-general@arkaria.postgresql.org; Fri, 13 Sep 2024 16:12:34 +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 1sp8uO-006IAE-P1 for pgsql-general@arkaria.postgresql.org; Fri, 13 Sep 2024 16:12:32 +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 1sp8uO-006I9i-DS for pgsql-general@lists.postgresql.org; Fri, 13 Sep 2024 16:12:32 +0000 Received: from mail-lf1-f47.google.com ([209.85.167.47]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sp8uH-000zRJ-U7 for pgsql-general@lists.postgresql.org; Fri, 13 Sep 2024 16:12:30 +0000 Received: by mail-lf1-f47.google.com with SMTP id 2adb3069b0e04-5365928acd0so2794978e87.2 for ; Fri, 13 Sep 2024 09:12:25 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726243944; x=1726848744; h=content-transfer-encoding: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=JevfkfCmb3rH9sRO2dNvJ/FoNwwHV9ieOCgIbPwr6ms=; b=XG89lwhBlPJAXUsAGIoHvCJityLJaUQWS8LrMJZ6X2bjCXBR6bySbq9xt6LpWHXlpb Z74TD6eo0Bf3uXK1wjxV3LCYVlFHk4M94UzEdMWlfxmODtWCtVYvKprKJhoP4hCERmnR evkD4ESnpWxx75gr6PH/sXcnaQ1Ssl1w0zUKsBO1kFzB1OzeMtZ7Xz761+CURzCdzqmY q6HP2BCAECIsqDEq84ZjXf6vMQkYrBzK3HcdJBHV6EkRsj/U1EO3VMZpgTkVsbTksfoh /bZlc9fndx7fU99otpNl1zhGOtIqqx/RyRNT2spC43WA/F1iDc1o/A/LqRYewhFqSJtn O3Xg== X-Forwarded-Encrypted: i=1; AJvYcCVsYvOA3AEnOjmNAXAwf8F2zs1LiwtrB0n0OMvTX18rPvzcYRUYFftzRZbWux5VMXQS15nGcWKdDPP/nHyH@lists.postgresql.org X-Gm-Message-State: AOJu0YwNYt2hRO+fFsaOAn/Vxu+DzDR+RI1Yv6M5b0Db+aAT2xbzlVLF 9wbSWBoHOJYW/0rKttDYYMra0OqBcGLtbPqY88nvlEYukxX+4Ka6fN8B5yXGPHQq+aHS8iluQNX 1lWMLp7W5WMNi88HolqeJPtOsCycc/W77OjBGew== X-Google-Smtp-Source: AGHT+IEHzadY/LZOb/hLo4c8eQMCPvpIGbJHYTgeMMbN8u+wPdCDR0wUKCKYJ/Ehl4JoaJ2BZgJcdY/ZrQe57l8vjm8= X-Received: by 2002:a05:6512:304d:b0:536:581c:9d9f with SMTP id 2adb3069b0e04-53678fb7326mr4638497e87.24.1726243943827; Fri, 13 Sep 2024 09:12:23 -0700 (PDT) MIME-Version: 1.0 References: <88e52d11-6185-473d-9eb2-673b62cb2426@gmx.net> In-Reply-To: From: Willow Chargin Date: Fri, 13 Sep 2024 09:12:12 -0700 Message-ID: Subject: Re: Functionally dependent columns in SELECT DISTINCT To: "David G. Johnston" Cc: "shammat@gmx.net" , "pgsql-general@lists.postgresql.org" Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Thanks both for your suggestions so far. On Fri, Sep 13, 2024 at 8:43=E2=80=AFAM David G. Johnston wrote: > > On Friday, September 13, 2024, Willow Chargin w= rote: >> >> In reality I really do want the ID columns of the >> *most recent* items. > > > Use a window function to rank them and pull out rank=3D1 Hmm, like this? noting that it's rank<=3D5, not rank=3D1: -- 1. rank all item-part combinations, densely since an item may have multiple parts -- 2. limit by rank, still retaining multiple copies of each item -- 3. de-duplicate IDs SELECT DISTINCT id FROM ( SELECT id, dense_rank FROM ( SELECT items.id, dense_rank() OVER (ORDER BY create_time DESC) FROM items JOIN parts ON items.id =3D parts.item_id WHERE part_id % 3 =3D 0 ) q WHERE dense_rank <=3D 5 ) q I've done this before, but my experience is that it's usually far slower because the rank is computed eagerly even for rows that don't match the rank bound. And indeed here it takes 20% longer than even the slower GROUP BY from before: https://explain.depesz.com/s/mQIi > or use a lateral subquery to surgically (fetch first 1) retrieve the firs= t row when sorted by recency descending. I'm not sure that I see how to apply this when I need top-k, not top-1.