Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1atbfc-0000AU-6m for pgsql-bugs@arkaria.postgresql.org; Fri, 22 Apr 2016 14:02:56 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1atbfb-0002hQ-HK for pgsql-bugs@arkaria.postgresql.org; Fri, 22 Apr 2016 14:02:55 +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 1atbfa-0002h3-T5 for pgsql-bugs@postgresql.org; Fri, 22 Apr 2016 14:02:54 +0000 Received: from mail-wm0-x241.google.com ([2a00:1450:400c:c09::241]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1atbfY-0004sI-0j for pgsql-bugs@postgresql.org; Fri, 22 Apr 2016 14:02:54 +0000 Received: by mail-wm0-x241.google.com with SMTP id n3so5109917wmn.1 for ; Fri, 22 Apr 2016 07:02:51 -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=rMFqxbA5gWwfivxVigsR5LG0/LM/NfQfZULSDoNOESc=; b=KgAHY/3YSMLnTuBzgSvNniujCr1/TEiTxyH0raZJ13bSBK0tiZQJ1xbgCk9x+hobz7 n4bEqmv+ljzyDmWtWanDk/FkvQZaD59bbxAZGwDKmdFOTr2apLAq4+ISIEKLznB9vwXT zQtDstMpvIgEl3KNNyxDrm04W6TJN9owqll96O3w/t7C0FYkccUrcZP4a7XUDbzab6cx H/PInIBy2IaDbhxfjGOMuba5M5DwKytvDz5Ex3fOHfMg/Ta7XXiFNcndCDcUjFRBElr2 p/ogMfysGKoNYyDKLMrPEaf7pr8ff5RoFlI7Mw1z1cMsHU7G1ycrnf9ZQzuSjVxIh9yX zl7Q== 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=rMFqxbA5gWwfivxVigsR5LG0/LM/NfQfZULSDoNOESc=; b=llCnjR6geFOEjf8aNl+Hs10MUG3ypVBXhDQD7n6WrYjTfW8ZIPekP+wNCzK6wEE6th XGL/s3iqdAQIvtvh6kSc6LvhFkqiqfw4CzrJqhJ0xx033b9P+eweL0QYMyyxZSzGOl7J OObFQv0GWxRK2+1NnWWgnMcfJ7WgVKp23cFSr8Ndd+wMJKSLRZGb25Wu2Vch1Uzv1G3T 4L5yeBWowcL/2wmHSWJ3kSwuYDiyldulUEcU2mTf0xyYTGnFhlok/DhqEJuDz9OuhTJ7 f8ogNLWvUYv1k9m9BTZbTh6yDFig3gS4ZKsPWSF7PWCat7EKe1AHu1XoNibuxGRslivg EEnA== X-Gm-Message-State: AOPr4FUM526F753a2vxC8iOxGZfLPTQmuuim+kDbPODGBnXOho5yt06aoZowwlrwZLGJsw== X-Received: by 10.28.189.10 with SMTP id n10mr4301541wmf.101.1461333770462; Fri, 22 Apr 2016 07:02:50 -0700 (PDT) Received: from [192.168.1.144] (dsl-25-149.utaonline.at. [81.189.25.149]) by smtp.googlemail.com with ESMTPSA id a16sm3521164wme.22.2016.04.22.07.02.49 (version=TLSv1/SSLv3 cipher=OTHER); Fri, 22 Apr 2016 07:02:49 -0700 (PDT) Subject: Re: BUG #14107: Major query planner bug regarding subqueries and indices To: David Rowley References: <20160421115638.22895.78902@wrigleys.postgresql.org> Cc: "pgsql-bugs@postgresql.org" From: Mathias Kunter Message-ID: <69e376c7-6c2c-c96d-2ad6-3734affb59b7@gmail.com> Date: Fri, 22 Apr 2016 16:02:49 +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: Content-Type: text/plain; charset=utf-8; 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 > Thanks for the report, but I think your subject line is a little > exaggerated. Sorry for that :-) > I'd not actually thought about expanding this to IN and NOT IN, but > likely it would be possible, providing NULLs could be handled > correctly too. Was this the optimisation you think is missing? No - let's consider a real-world example where the problem becomes more evident. Assume we have the following two tables: CREATE TABLE book (id SERIAL NOT NULL, name VARCHAR, authorId SERIAL, CONSTRAINT book_pkey PRIMARY KEY (id)); CREATE TABLE author (id SERIAL NOT NULL, surname VARCHAR, CONSTRAINT author_pkey PRIMARY KEY (id)); All relevant columns are indexed. The book table contains 1.3 M rows. Now let's say we want to find books by either name or by author name. A simple problem, actually. The following query is used (which could of course also be rewritten to use a JOIN instead, but that's not the point here). We find out that it has an unexpected and unacceptably long execution time: EXPLAIN SELECT * FROM book WHERE name = 'Harry Potter' OR authorId IN (SELECT id FROM author WHERE surname = 'Rowling'); QUERY PLAN ------------------------------------------------------------------------------------------- Seq Scan on book (cost=13.68..26792.88 rows=576709 width=40) Filter: (((name)::text = 'Harry Potter'::text) OR (hashed SubPlan 1)) SubPlan 1 -> Bitmap Heap Scan on author (cost=4.20..13.67 rows=6 width=4) Recheck Cond: ((surname)::text = 'Rowling'::text) -> Bitmap Index Scan on author_surname_index (cost=0.00..4.20 rows=6 width=0) Index Cond: ((surname)::text = 'Rowling'::text) In contrast to that, consider the query plan for the following logically equivalent query, which executes in just a few milliseconds (!): EXPLAIN SELECT * FROM book WHERE name = 'Harry Potter' UNION SELECT * FROM book WHERE authorId IN (SELECT id FROM author WHERE surname = 'Rowling'); QUERY PLAN ------------------------------------------------------------------------------------------------------------ HashAggregate (cost=454.87..455.99 rows=112 width=29) Group Key: book.id, book.name, book.authorid -> Append (cost=0.43..454.03 rows=112 width=29) -> Index Scan using book_name_index on book (cost=0.43..16.48 rows=3 width=29) Index Cond: ((name)::text = 'Harry Potter'::text) -> Nested Loop (cost=4.63..436.43 rows=109 width=29) -> Bitmap Heap Scan on author (cost=4.20..13.67 rows=6 width=4) Recheck Cond: ((surname)::text = 'Rowling'::text) -> Bitmap Index Scan on author_surname_index (cost=0.00..4.20 rows=6 width=0) Index Cond: ((surname)::text = 'Rowling'::text) -> Index Scan using book_authorid_index on book book_1 (cost=0.43..70.28 rows=18 width=29) Index Cond: (authorid = author.id) -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs