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 1hHGDI-0005nt-F4 for pgsql-general@arkaria.postgresql.org; Thu, 18 Apr 2019 23:13:04 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hHGDG-0005b5-4r for pgsql-general@arkaria.postgresql.org; Thu, 18 Apr 2019 23:13:02 +0000 Received: from magus.postgresql.org ([87.238.57.229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hHGDF-0005ar-Ep for pgsql-general@lists.postgresql.org; Thu, 18 Apr 2019 23:13:01 +0000 Received: from mbx.knossos.net.nz ([202.160.48.10]) by magus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hHG6l-0004mu-Ip for pgsql-general@lists.postgresql.org; Thu, 18 Apr 2019 23:06:34 +0000 Received: from [192.168.77.204] (default-rdns.vocus.co.nz [121.99.202.67] (may be forged)) (authenticated bits=0) by mbx.knossos.net.nz (8.14.4/8.14.4) with ESMTP id x3IN5k1a026923 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NOT) for ; Fri, 19 Apr 2019 11:05:47 +1200 Subject: Re: Multicolumn index for single-column queries? To: pgsql-general@lists.postgresql.org References: <95ade9a3-6846-3f55-f936-85b8bb4b46a8@mail.ru> <87muknwhle.fsf@protecting.net> From: Gavin Flower Organization: ArchiDevSys Message-ID: Date: Fri, 19 Apr 2019 11:05:46 +1200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 In-Reply-To: <87muknwhle.fsf@protecting.net> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-GB List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 19/04/2019 01:47, Harald Fuchs wrote: > Andreas Kretschmer writes: > >> Am 18.04.19 um 08:52 schrieb rihad: >>> Hi. Say there are 2 indexes: >>> >>>     "foo_index" btree (foo_id) >>> >>>     "multi_index" btree (foo_id, approved, expires_at) >>> >>> >>> foo_id is an integer. Some queries involve all three columns in >>> their WHERE clauses, some involve only foo_id. >>> Would it be ok from general performance standpoint to remove >>> foo_index and rely only on multi_index? I know that >>> PG would have to do less work updating just one index compared to >>> updating them both, but wouldn't searches >>> on foo_id alone become slower? >> it depends . >> >> it depends on the queries you are using, on your workload. a >> multi-column-index will be large than an index over just one column, >> therefore you will have more disk-io when you read from such an index. > I think it also depends on the average number of rows having the same foo_id. > The number of rows referenced by an index entry for the multi_index will always be less than or equal to those for the matching foo_index. Also there will be fewer index entries per block for the multi_index.  Which is why the I/O count will be higher; even in the best case, where there is an equal row referenced by the index entries.