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 1fQDAL-0006VC-VW for pgsql-performance@arkaria.postgresql.org; Tue, 05 Jun 2018 14:42:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fQDAK-0005zY-Ll for pgsql-performance@arkaria.postgresql.org; Tue, 05 Jun 2018 14:42:28 +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.89) (envelope-from ) id 1fQDAK-0005zP-Ef for pgsql-performance@lists.postgresql.org; Tue, 05 Jun 2018 14:42:28 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fQDAH-0001M6-Dc for pgsql-performance@lists.postgresql.org; Tue, 05 Jun 2018 14:42:28 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id w55EgMH3028449; Tue, 5 Jun 2018 10:42:22 -0400 From: Tom Lane To: Fred Habash cc: pgsql-performance@lists.postgresql.org Subject: Re: Simple Query Elapsed Time ~ 3 hrs Using Bitmap Index/Heap Scan In-reply-to: <560c24f9-dced-94ae-8e14-fe9451246342@gmail.com> References: <560c24f9-dced-94ae-8e14-fe9451246342@gmail.com> Comments: In-reply-to Fred Habash message dated "Tue, 05 Jun 2018 10:17:08 -0400" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <28447.1528209742.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Tue, 05 Jun 2018 10:42:22 -0400 Message-ID: <28448.1528209742@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Fred Habash writes: > Indexes: > "cl_pk" PRIMARY KEY, btree (cl_id) > "cl_cnst_uk01" UNIQUE CONSTRAINT, btree (cit_id, vclf_number, cl_v= alue) > "cl_indx_fk01" btree (cit_id) > "cl_indx_fk02" btree (vclf_number) This is pretty inefficient index design. Your query is slow because the only selective condition it has is on cl_value, but you have no index that can be searched with cl_value as the leading condition. Moreover, you have two indexes that can be searched with cit_id as the leading condition, which is just wasteful. I'd try reorganizing the cl_cnst_uk01 index as (cl_value, vclf_number, cit_id) so that it can serve for searches on cl_value, while still enforcing the same uniqueness condition. This particular column ordering would also let your query use the vclf_number constraint as a secondary search condition, which would help even more. There's relevant advice about index design in the manual, https://www.postgresql.org/docs/current/static/indexes.html (see 11.3 and 11.5 particularly) regards, tom lane