Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1aw7LM-0002cJ-06 for pgsql-bugs@arkaria.postgresql.org; Fri, 29 Apr 2016 12:16:24 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1aw7LL-0005I6-EE for pgsql-bugs@arkaria.postgresql.org; Fri, 29 Apr 2016 12:16:23 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1aw7Ki-0004Nw-M9 for pgsql-bugs@postgresql.org; Fri, 29 Apr 2016 12:15:44 +0000 Received: from mail-wm0-x232.google.com ([2a00:1450:400c:c09::232]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1aw7Kf-00055H-9H for pgsql-bugs@postgresql.org; Fri, 29 Apr 2016 12:15:44 +0000 Received: by mail-wm0-x232.google.com with SMTP id g17so33127082wme.1 for ; Fri, 29 Apr 2016 05:15:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=subject:to:references:cc:from:message-id:date:user-agent :mime-version:in-reply-to:content-transfer-encoding; bh=0QUEx5tuYsSY3JUnnFXQ3JODvNofwYIkyQo22MqImRE=; b=wjsicwz/BcNdkRxA0a+mR8crKx6qkgADaBcCXPvvnYVG0bCsbmS3+NYpQg0+VkA2kX uDRCzPmfZgq9QrEqXQRajXZVdMt+RvFS+Iq14EN/B2lP1BeSU+s2uUT48ZFOvELMzl7E va9j2cvr/L12hlbf/mIi5SDyV1JmZkfYQhCeOtVX7rh6xYG+DtnO8QJavjBwlTIL7FT7 Ig9tMvKeW+poYwEama5d0zC4XXw4f5443YTKEA69MdFLxX3VQCgb4mzCEPcIPi/60CdK KLRkiAKb738vzOK7i8zaV2Qi3fkTHpSF0u/KSWmOqC782TlDjI13WkZlPc0rMLy69Y0u oeMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:subject:to:references:cc:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding; bh=0QUEx5tuYsSY3JUnnFXQ3JODvNofwYIkyQo22MqImRE=; b=J+4W4romIjPCg66YYlI7Eg5bQK6uGXloELT7x/sBFfAvbUI8nMuETsEftmIlQ4Eyjo UvW6iD29eYdkgqir8CO1XaeACyn0oahTqIW25g7KgDpizuuFemfJlTzIOXozZuOYcB2i u1jHj7W93AkAQZsB2R82g5q5AQygh/CRVEv7JBR9IXwFmno2rnXaxoKUBrIvJz7PlorA OQYvKQSZFMwQhqPZYjLjS315wSh5wYvkaqDGqFW8hOQRLJ8B0UH2+ngB6XBhoV4nBtmZ 63cc0ON92pBvtmu9fzy8B9Ajf4pqj/ouFKmNuUFcyYU/yxQWvos9oRSU3bOho1Jwk61c A+7Q== X-Gm-Message-State: AOPr4FVnhMOYXDW+yNuyA9DzkdaXyt49wS7W0e/hRaank4F6VSebagEhvx9J8C++UNOYHA== X-Received: by 10.28.85.137 with SMTP id j131mr3570629wmb.15.1461932140256; Fri, 29 Apr 2016 05:15:40 -0700 (PDT) Received: from [192.168.1.144] (dsl-25-149.utaonline.at. [81.189.25.149]) by smtp.googlemail.com with ESMTPSA id r5sm2977372wjy.37.2016.04.29.05.15.39 (version=TLSv1/SSLv3 cipher=OTHER); Fri, 29 Apr 2016 05:15:39 -0700 (PDT) Subject: Re: BUG #14107: Major query planner bug regarding subqueries and indices To: Tom Lane References: <20160421115638.22895.78902@wrigleys.postgresql.org> <69e376c7-6c2c-c96d-2ad6-3734affb59b7@gmail.com> <44df2e58-87f5-282c-9871-c735240c55e4@gmail.com> <1461406942121-5900147.post@n5.nabble.com> <3ac6f379-08f8-d1db-639f-2dc3075d2eb4@gmail.com> <1461610493986-5900430.post@n5.nabble.com> <629ecf23-b7b2-2d73-6792-150b111e83ab@gmail.com> <18635.1461892238@sss.pgh.pa.us> Cc: "pgsql-bugs@postgresql.org" From: Mathias Kunter Message-ID: Date: Fri, 29 Apr 2016 14:15:38 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.0 MIME-Version: 1.0 In-Reply-To: <18635.1461892238@sss.pgh.pa.us> Content-Type: text/plain; charset=iso-8859-15; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.7 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org > If you know that the > sub-select isn't going to return very many rows, you could do > > SELECT ... FROM a WHERE a.x = ? OR a.y = ANY(ARRAY(SELECT ...)); Isn't the planner already doing something like this, since the following query is using the index as expected: SELECT ... FROM a WHERE a.x IN (SELECT ...); > but this would blow up rather badly with a large sub-select result, > so I'm not sure I want to try to make the planner transform it that > way automatically. Wouldn't it be possible then to use this optimization based on the estimated result size of the subquery? I think this would almost always be faster than a sequential scan anyway. I observed that using the ANY(ARRAY(SELECT...)) syntax on my small test tables (100 K rows) already improves the query time by a factor of more than 100, and it will be even more when tables are large. Please consider implementing this optimization! > I don't actually see any way to do very much with your second example at > all: > >> SELECT ... FROM a JOIN b ON (...) WHERE a.x = ? OR b.y = ?; Assuming both joined tables contain a PK (or another unique column), then it should be possible by replanning the query as: SELECT ... FROM a JOIN b ON ((join_cond AND a.x = ?) OR (join_cond AND b.y = ?)); (Let "join_cond" denote the original join condition here.) Now, the JOIN implementation must be smart enough to handle OR conditions: First, obtain the rows satisfying join_cond AND a.x = ? by using the index as usual. For each matching row, create the tuple (a.id, b.id) and insert it into a search tree (or hash or whatever). Then, obtain the rows satisfying join_cond AND b.y = ? For each matching row, query the search tree whether it already contains the tuple (a.id, b.id), and only add the current row to the final result if it doesn't. -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs