Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fzLzL-0002Eu-2r for pgsql-general@arkaria.postgresql.org; Mon, 10 Sep 2018 13:12:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fzLzI-0007j9-RL for pgsql-general@arkaria.postgresql.org; Mon, 10 Sep 2018 13:12:20 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fzLzI-0007j2-GI for pgsql-general@lists.postgresql.org; Mon, 10 Sep 2018 13:12:20 +0000 Received: from mx1.mailbox.org ([80.241.60.212]) by makus.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fzLzF-0005Jr-10 for pgsql-general@postgresql.org; Mon, 10 Sep 2018 13:12:19 +0000 Received: from smtp1.mailbox.org (smtp1.mailbox.org [80.241.60.240]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.mailbox.org (Postfix) with ESMTPS id C26264A92B; Mon, 10 Sep 2018 15:12:13 +0200 (CEST) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp1.mailbox.org ([80.241.60.240]) by spamfilter03.heinlein-hosting.de (spamfilter03.heinlein-hosting.de [80.241.56.117]) (amavisd-new, port 10030) with ESMTP id rSl9UwR1F6nv; Mon, 10 Sep 2018 15:12:11 +0200 (CEST) Message-ID: Subject: Re: Why my query not doing index only scan From: Laurenz Albe To: Arup Rakshit , pgsql-general@postgresql.org Date: Mon, 10 Sep 2018 15:12:10 +0200 In-Reply-To: References: Organization: Cybertec Content-Type: text/plain; charset="UTF-8" Mime-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Arup Rakshit wrote: > I am learning at this point how index works in DBMS. So I am looking for a very broad explanation to clear my basics. > > I have an index defined "inspector_tool_idx4_1" UNIQUE, btree (company_id, item_code, deleted_at). > Now I am using the *company_id* column in the where clause, and the selecting just the *item_code* field for all matching rows. > I expected here the planner will do a index only scans. But it is doing bitmap index scan. Any idea what it is not doing what I expected it to do. > > > inspection_development=# explain analyze select item_code from inspector_tool_components where company_id = '7881ff2e-0557-4734-9da8-2d33072ff1ef'; > QUERY PLAN > ------------------------------------------------------------------------------------------------------------------------------------- > Bitmap Heap Scan on inspector_tool_components (cost=45.92..1360.48 rows=1226 width=8) (actual time=0.382..1.202 rows=1232 loops=1) > Recheck Cond: (company_id = '7881ff2e-0557-4734-9da8-2d33072ff1ef'::uuid) > Heap Blocks: exact=81 > -> Bitmap Index Scan on inspector_tool_idx4_1 (cost=0.00..45.61 rows=1226 width=0) (actual time=0.347..0.347 rows=1232 loops=1) > Index Cond: (company_id = '7881ff2e-0557-4734-9da8-2d33072ff1ef'::uuid) > Planning time: 0.207 ms > Execution time: 1.358 ms > (7 rows) Try to "VACUUM (ANALYZE) inspector_tool_components", that will set the visibility mape and get the statistics right, maybe than you get an index only scan. How many rows does the table contain? Yours, Laurenz Albe -- Cybertec | https://www.cybertec-postgresql.com