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 1ipkcn-0002mq-D5 for pgsql-performance@arkaria.postgresql.org; Fri, 10 Jan 2020 03:06:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ipkcl-0005Dg-VJ for pgsql-performance@arkaria.postgresql.org; Fri, 10 Jan 2020 03:06:11 +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 1ipkcl-0005DX-CU for pgsql-performance@lists.postgresql.org; Fri, 10 Jan 2020 03:06:11 +0000 Received: from mail-yw1-xc43.google.com ([2607:f8b0:4864:20::c43]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1ipkcf-0005Xe-JF for pgsql-performance@lists.postgresql.org; Fri, 10 Jan 2020 03:06:09 +0000 Received: by mail-yw1-xc43.google.com with SMTP id 10so164005ywv.5 for ; Thu, 09 Jan 2020 19:06:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=ztUvBlj+9E/MWpEeujQM1jh6RisRpw0urs7oG/gHhsY=; b=pxsEoatAwCsZJDlg5nQN7VncPxA+Ru++9ioqhLymcuxYYtDtFl39tSXMVQrZAZjVQx 33L7/p+gQw03A/kmjSn+RZi5waqkQSnqoY3Fo1q6XI05Fx2oWBMykK01vrAjSkqmWynx E7vbmiFHQye7+gtNDb85c+Z8kGWH1k45y39YNlXsJiqfmZpnQYnNJUTyq5nQlzrS0uSh /pjOP1+vSPyA644OmZPa5ICleROzsjIIdqrsKAQHNJy7e3RbVxD++sPj50u+TFdqJmrZ k5IwaoL7NZJK1Q2MijCdQRrS9feq1NEJkW5LpV+ypjKB1vqwXzRr6BwCo4CMY2AF83RH KQqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=ztUvBlj+9E/MWpEeujQM1jh6RisRpw0urs7oG/gHhsY=; b=tDJCz5bcmNV+llv2IIbG/63xCS/AaxPwL6yshIp4qDvSC77NmNB04/Ru6py5LRp4vl 2eQ3DeeQluuSGo1Ez23o1qoFrHihUvpqXBQjq2YgbC+Oj12gsz5l7ITBONE15cWKL1Nv Zf6oz/yEQ5jcFp1wI+Q4DppGlGWAnmSgNoBJva4GXNv1Ltlq8aOSGe5yQbuMB0+x7WCO jUfRQsXw8yvalC/vjBmFuo1Luh9cdMhiYWBI02VzD+f4uXIeoVAeZZubAM/l34UmxNVl YQ9WZckCxJgkz4KSoPAONf7UTKR0Af40j+eXn07dFe+jWqLWOP+ag83cHaG60vFEa9bj gCew== X-Gm-Message-State: APjAAAUJ8e2R0EkrFQM9A2EFPqrRZCT0XZwiuOEli7JQrL3lcQzLKyLC BYQKc4Ab95pYnbiFbFEwRWSB4Q== X-Google-Smtp-Source: APXvYqxztqD/aLH+z+6R/6uMxBsBcZA0XbGXvgQ+1JqLqNW9qRC9ZPBgRmdVQ8GAVp9k7cblr2aKFw== X-Received: by 2002:a81:66c1:: with SMTP id a184mr919055ywc.372.1578625564590; Thu, 09 Jan 2020 19:06:04 -0800 (PST) Received: from pryzbyj (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id q189sm423150ywb.15.2020.01.09.19.06.03 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 09 Jan 2020 19:06:03 -0800 (PST) Received: by pryzbyj (Postfix, from userid 1000) id 297358009F4; Thu, 9 Jan 2020 21:06:02 -0600 (CST) Date: Thu, 9 Jan 2020 21:06:02 -0600 From: Justin Pryzby To: Marco Colli Cc: pgsql-performance@lists.postgresql.org Subject: Re: Bad query plan when you add many OR conditions Message-ID: <20200110030601.GH12066@telsasoft.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.24 (2015-08-30) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Fri, Jan 10, 2020 at 02:11:14AM +0100, Marco Colli wrote: > I have a query on a large table that is very fast (0s): > https://gist.github.com/collimarco/039412b4fe0dcf39955888f96eff29db#file-fast_query-txt ORDER BY + LIMIT is a query which sometimes has issues, you can probably find more by searching. The planner thinks it'll hit the LIMIT pretty soon and only run a fraction of the index scan - but then it turns out to be wrong. You might have poor statistics on project_id and/or tags. This *might* help: ALTER TABLE subscriptions ALTER project_id SET STATISTICS 2000; ANALYZE subscriptions; But I'm guessing there's correlation between the two, which the planner doesn't know. If you're running at least v10, I'm guessing it would help to CREATE STATISTICS on those columns (and analyze). See one similar problem here (not involving LIMIT). https://www.postgresql.org/message-id/flat/CABFxtPedz4zL%2BaPWut4%2B%3Dum4av1aAXr6OVRfRB_6K7mJKMbEcw%40mail.gmail.com