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 1q6re7-00074d-Py for pgsql-bugs@arkaria.postgresql.org; Wed, 07 Jun 2023 11:48:11 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1q6re5-0005Cv-H3 for pgsql-bugs@arkaria.postgresql.org; Wed, 07 Jun 2023 11:48:09 +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 1q6re5-0005Cm-7l for pgsql-bugs@lists.postgresql.org; Wed, 07 Jun 2023 11:48:09 +0000 Received: from mail-ej1-x62a.google.com ([2a00:1450:4864:20::62a]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1q6re0-000hbJ-0I for pgsql-bugs@lists.postgresql.org; Wed, 07 Jun 2023 11:48:08 +0000 Received: by mail-ej1-x62a.google.com with SMTP id a640c23a62f3a-976a0a1a92bso888938666b.1 for ; Wed, 07 Jun 2023 04:48:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686138481; x=1688730481; h=content-transfer-encoding:in-reply-to:from:content-language :references:to:subject:user-agent:mime-version:date:message-id:from :to:cc:subject:date:message-id:reply-to; bh=u7j3+ro0VbIzfwXvuFMqU0lgehjaOtSTgkJlrz+YltI=; b=hLr1dZnl2tM2wn/BYUf5aW5NKySwyW8lXYN6KYhO9UCPA7cnEvYmsrJ+hhXNR3zeFq c4gm+S5JHpGlNXghJwZW4QhtfX9uat5LNCu4WOPP8uJNmhTiC6ggEiCyC6Il5EGzgvwt Rr9C2DbOb8wBRlsHhPjRYlm5t2OaObLz5uqbvboJEdL5QXtsd+b+kaX/gZQCDRwbRjxP 0zXyEJi3Yfbbpa/Oi7vi1Ka+l98T9e7peNxGNSCag+U7IeqkOU/6WUa3OxninyQGaj1Q AeOvMKs8BPzqioxTonr55Gp0Y0cLstWwkvArP56JcYnO8N8iAItRObD3DfW/ZJ6SJIpW gWaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686138481; x=1688730481; h=content-transfer-encoding:in-reply-to:from:content-language :references:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=u7j3+ro0VbIzfwXvuFMqU0lgehjaOtSTgkJlrz+YltI=; b=XwnczIm8G9Ils9qydhbyZbmVNMnT+IfgEWpUdrdEDKaJm7CgS8qXyzLtqzQsanuWKG jsQTtGUAI8HXHyJ6NrMvX+g2pCjJWHX+OotvsrnHgf9fSe9bgl6F9aUlv42IlzRsAXFe H/bOND2/rsANyUj4yaHuCPWPerqcllKaqddzisOfSQe97QKV1M3WyB4Ik2vtG+odsyuF K7JHiqhrdbpvVm9Egi2sCy0ftDdKCrYptQ5IENeq06IEzR85OfjrIjko5NK1A7bkTDj2 irt8ksGC5vu1cZtWvHY1q1tUZ88FUWrslMKo5dyfCw1kJIMTZ+G0UhD8MDwKGfbcvPqG pSsg== X-Gm-Message-State: AC+VfDyLyYo4mpKG0VxHDUBO2INsFD+TQjRZmwFoxRWytegDzD+tUKgL IMJkRNhxnvxSToO3YHRj0g0= X-Google-Smtp-Source: ACHHUZ48qVTqS0cytq0I+TbDtFsurkWOinNdA4ZWRvPzLLQ/EiAmJK4P56ODTf076FT0+AOEpl1MeA== X-Received: by 2002:a17:907:6e25:b0:969:93f2:259a with SMTP id sd37-20020a1709076e2500b0096993f2259amr4764389ejc.73.1686138481457; Wed, 07 Jun 2023 04:48:01 -0700 (PDT) Received: from ?IPV6:2a02:8388:e002:8cf0:904:dd2c:61fe:518c? (2a02-8388-e002-8cf0-0904-dd2c-61fe-518c.cable.dynamic.v6.surfer.at. [2a02:8388:e002:8cf0:904:dd2c:61fe:518c]) by smtp.googlemail.com with ESMTPSA id h15-20020a170906260f00b00977e6992962sm3268663ejc.106.2023.06.07.04.48.00 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 07 Jun 2023 04:48:00 -0700 (PDT) Message-ID: <94608857-707f-55ce-99a0-ac500aeb06f8@gmail.com> Date: Wed, 7 Jun 2023 13:47:59 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0 Subject: Re: BUG #17964: Missed query planner optimization To: David Rowley , pgsql-bugs@lists.postgresql.org References: <17964-cdeb9e3f89c18393@postgresql.org> Content-Language: de-AT From: Mathias Kunter 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 > It's not a bug that the planner does not consider evaluating the join > before the UNION Yes, it's not a bug, but it's something which can be improved. If I simply change the original query from this: > SELECT ... WHERE id IN (SELECT ...); into this: > SELECT ... WHERE id = ANY(ARRAY(SELECT ...)); then Postgres uses an index scan, and the query is orders of magnitude faster. Note that the planner actually correctly computes the estimated costs for both variants, since I get: > cost=769.11..1227.17 when using IN > cost=86.45..86.65 when using ANY See https://dbfiddle.uk/iOkiiTJJ Also note that this issue doesn't only affect UNION queries. For example, the following query will also execute orders of magnitude faster if I simply replace IN with ANY: > SELECT * FROM t WHERE x = 'a' OR y IN (SELECT ...); Again, estimated costs say that using ANY should be faster: > cost=8.30..2443.31 when using IN > cost=56.45..350.69 when using ANY See https://dbfiddle.uk/b9piwQr4 Hence, why doesn't the planner simply test whether it's beneficial to replace IN with ANY? It seems that all which has to be done is to compare the query plans for both possible execution variants. I guess this should be rather simple to implement, isn't it? Thanks Mathias Am 06.06.23 um 23:32 schrieb David Rowley: > On Wed, 7 Jun 2023 at 04:44, PG Bug reporting form > wrote: >> In the example below, the query planner uses a sequential scan (query 1) >> even though it could use an index scan (query 2). >> >> EXPLAIN ANALYZE SELECT id, name FROM (SELECT id, name FROM table1 UNION >> SELECT id, name FROM table2) AS q >> WHERE id IN (SELECT id FROM table3); > >> EXPLAIN ANALYZE SELECT id, name FROM (SELECT id, name FROM table1 UNION >> SELECT id, name FROM table2) AS q >> WHERE id IN (1538,8836,5486,3464,2673); > > It's not a bug that the plannSer does not consider evaluating the join > before the UNION, it's just an optimisation opportunity we don't > currently explore. > > If you want that, then write: > > EXPLAIN ANALYZE SELECT id, name FROM table1 WHERE id IN (SELECT id > FROM table3) UNION SELECT id, name FROM table2 WHERE id IN (SELECT id > FROM table3); > > David