Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1irH8t-0004Hp-Lg for pgsql-performance@arkaria.postgresql.org; Tue, 14 Jan 2020 08:01:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1irH8s-0006xg-C9 for pgsql-performance@arkaria.postgresql.org; Tue, 14 Jan 2020 08:01:38 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1irH8r-0006xS-WD for pgsql-performance@lists.postgresql.org; Tue, 14 Jan 2020 08:01:38 +0000 Received: from mout.kundenserver.de ([212.227.126.187]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1irH8p-0007tv-1Y for pgsql-performance@lists.postgresql.org; Tue, 14 Jan 2020 08:01:36 +0000 Received: from [10.8.0.75] ([185.40.248.10]) by mrelayeu.kundenserver.de (mreue010 [212.227.15.129]) with ESMTPSA (Nemesis) id 1N3KgE-1jrSTx0xve-010Ifb for ; Tue, 14 Jan 2020 09:01:31 +0100 Subject: Re: Bad query plan when you add many OR conditions To: pgsql-performance@lists.postgresql.org References: From: Thomas Kellerer Message-ID: <9748c027-a524-2718-28ec-3c6bc74feafb@gmx.net> Date: Tue, 14 Jan 2020 09:01:30 +0100 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.23) Gecko/20090812 Thunderbird/2.0.0.23 Mnenhy/0.7.6.666 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: de-DE Content-Transfer-Encoding: 8bit X-Provags-ID: V03:K1:F1HKBZZ1yeudts0/UI2vGtCDwY1UwRnrR0hs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HAWc63ZzgcJTRSCalZ76geeQGF77XM8LLck8HC99ZAt9ujXwrI/6PTl/WX8YGWP/Y a09D2QUmGONxtiruMUfd2dpC7NBxMxuWgk3k+feX8= List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Marco Colli schrieb am 10.01.2020 um 02:11: > I have a query on a large table that is very fast (0s): > https://gist.github.com/collimarco/039412b4fe0dcf39955888f96eff29db#file-fast_query-txt > > Basically the query matches the rows that have a tag1 OR tag2 OR tag3 OR tag4 OR tag5...  > > However if you increase the number of OR at some point PostgreSQL makes the bad decision to change its query plan! And the new plan makes the query terribly slow: > https://gist.github.com/collimarco/039412b4fe0dcf39955888f96eff29db#file-slow_query-txt > > Instead of this (which is fast): >   Bitmap Index Scan on index_subscriptions_on_project_id_and_tags > It starts using this (which is slow): >   Parallel Index Scan using index_subscriptions_on_project_id_and_created_at > The choice seems quite stupid since it doesn't have the tags on the new index... and indeed the query takes about 1 minute instead of a few milliseconds. Here's a list of the available indexes: > https://gist.github.com/collimarco/039412b4fe0dcf39955888f96eff29db#file-_indexes-txt > > How can I encourage PostgreSQL to use the Bitmap Index Scan even when there are many OR conditions? I have tried with VACUUM ANALYZE subscriptions but it doesn't help. > > Note: the query is generated dynamically by customers of a SaaS, so I don't have full control on it Can you replace the many ORs with a single "overlaps" comparison? This (tags @> ARRAY['crt:2018_04']::varchar[]) OR (tags @> ARRAY['crt:2018_05']::varchar[]) OR (tags @> ARRAY['crt:2018_06']::varchar[]) is equivalent to tags && array['crt:2018_04','crt:2018_05','crt:2018_06', ...] The && operator can make use of a GIN index so maybe that uses the index_subscriptions_on_project_id_and_tags regardless of the number of elements.