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 1iJJjh-0006jU-2u for pgsql-performance@arkaria.postgresql.org; Sat, 12 Oct 2019 15:55:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iJJih-0002KJ-RD for pgsql-performance@arkaria.postgresql.org; Sat, 12 Oct 2019 15:54:15 +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_SHA1:256) (Exim 4.89) (envelope-from ) id 1iJJih-0002K6-H6 for pgsql-performance@lists.postgresql.org; Sat, 12 Oct 2019 15:54:15 +0000 Received: from mout.perfora.net ([74.208.4.194]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iJJif-0002z3-Ex for pgsql-performance@postgresql.org; Sat, 12 Oct 2019 15:54:15 +0000 Received: from [192.168.1.101] ([98.169.208.73]) by mrelay.perfora.net (mreueus004 [74.208.5.2]) with ESMTPSA (Nemesis) id 1McYbJ-1hn0hx1rW1-00cxBk; Sat, 12 Oct 2019 17:54:08 +0200 Subject: Re: Optimising a two column OR check To: Andrew Gierth Cc: Jeff Janes , Justin Pryzby , Ivan Voras , postgres performance list References: <20191012144336.GA4475@telsasoft.com> <87y2xqvv1x.fsf@news-spur.riddles.org.uk> <25d9306a-d0e9-ed82-3128-bff469f09aed@sqlexec.com> <87tv8evuq0.fsf@news-spur.riddles.org.uk> From: MichaelDBA Message-ID: Date: Sat, 12 Oct 2019 11:54:07 -0400 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 PostboxApp/7.0.5.2 MIME-Version: 1.0 In-Reply-To: <87tv8evuq0.fsf@news-spur.riddles.org.uk> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US X-Provags-ID: V03:K1:Z1fNRjUXd0F3bJ+4nqdeEjJTcWWgV2O+XkazBAY95LqSs47pYCo cPG6BxKIf+MQjoTjAH9tdYoX3VZJNLzRAAroCYS0jsodQxLrTu4UfOp8x89kligP2xOEkzP QcPlPVh/Ffwfq7qJ8ZKQzHZLEYAi1imvpXu83FV59qsgBykgsJh/CjszR12iTWMi8L9uekn J37lUaw9SxcrCQQPi9vIA== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:lQ2BngcNsMA=:22wXhDoXt+mBnn4BJzEJR/ aM2SxA0m5VpIavGlG+ascwq9Tmdgz26NAQPbri6k4bKC3QVAy2Odzt1BwbwrNWJQqSxx7b9KC 7aLNQzXd/I60VEYny64wxCtjF12IoEUjn3UVjlQZd3hx/y6TefCWkbyL08/TPVJyXTXuX0co5 cj5G3qk2klbNthSJYqxVKLXROryp17bzqTpmqpOox4Wf+x22Khl2oLFSb+ZAkia2YMvBBQZ6z q+1RbchFHak/bEveUyND48hboX57sDBiEbrLB/kxaziWOjtgCE1yndUDnGS1PQ4sPThZQ6x8F u7XzS+JlOmV3HSIjyS/dtMTXnMDzCceqadjbA35ynTzw3xBRATCg/eqIy8tU33aWN5z8KxHjg ovUzyCIdzr4Jpg8HZrLQerHhUILrwY7sWwM531FwEhYod1t98JGs8S51Rdtq1ACJrvc+0S+8B kh1b6310Q87SRIMdq74O/IzvTRSmWbY0iS6trdXaUCtCb0jsj0rVPTBASe6RRlpS1JZiGTnqE ZimDVx/2azQsSQwWAm1GDYIs6eKQEIvSVTHW7hfRZUCbp3gEVXo1041h8S3f54PkcXSEVrzHk IHOuyU8VClmDSIZAy4yO3V32sxqxsBwnwu9JomkpXzwpBjNiS5sMcfJ+hKI4vCz2p6KBh/fBa ag7dsdBjgOOfQAa90GhhsgfRr2VV2Bni/C/wsXd4l7Toey1f83/e7TENAh0PYaIS+NUNsUo7t Vj8FsPs4MuSurME859i388N/gZdMvVbA+ziQDIwUhpeAjQrM7Y6lsKTN5fyTwQyY6MZC0bZum 2hRP9BRIuh5/DcdXNf00I5FlG2kt/tNKrmYSmtN8OeUXs1OsVPpfoNqrdZLg90nXOHlnBRTc1 S3x4b07sIoqnSiRX7JUpY8gTsEXduKIFh2XlJ79ZI= List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Yep, you're right, Andrew, adding a couple rows made it do the index only scan.  I reckon I got misled by turning off sequential scans, thinking that actual rows were not important anymore.  Overly simplistic reasonings can get one into trouble, lol. Regards, Michael Vitale Andrew Gierth wrote on 10/12/2019 11:46 AM: >>>>>> "MichaelDBA" == MichaelDBA writes: > MichaelDBA> Nope, vacuumed it and still got the bitmap index scans. > > Let's see your explains. Here's mine: > > # set enable_seqscan=false; -- because I only have a few rows > SET > # insert into friend values (1,2),(2,5); > INSERT 0 2 > # vacuum analyze friend; > VACUUM > # explain analyze SELECT user1_id FROM friend WHERE user2_id=2 UNION ALL select user2_id FROM friend WHERE user1_id=2; > QUERY PLAN > ------------------------------------------------------------------------------------------------------------------------------------------------ > Append (cost=0.13..8.32 rows=2 width=4) (actual time=0.009..0.014 rows=2 loops=1) > -> Index Only Scan using friend_user2_id_user1_id_idx on friend (cost=0.13..4.15 rows=1 width=4) (actual time=0.009..0.009 rows=1 loops=1) > Index Cond: (user2_id = 2) > Heap Fetches: 0 > -> Index Only Scan using friend_pkey on friend friend_1 (cost=0.13..4.15 rows=1 width=4) (actual time=0.003..0.004 rows=1 loops=1) > Index Cond: (user1_id = 2) > Heap Fetches: 0 > Planning Time: 0.271 ms > Execution Time: 0.045 ms > (9 rows) > > Note that you have to put some actual rows in the table; if it is > completely empty, you'll not get a representative result. >