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 1gSknL-0000C3-Fc for pgsql-general@arkaria.postgresql.org; Fri, 30 Nov 2018 15:33:31 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gSknK-0006TJ-6x for pgsql-general@arkaria.postgresql.org; Fri, 30 Nov 2018 15:33:30 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gSknJ-0006TC-S4 for pgsql-general@lists.postgresql.org; Fri, 30 Nov 2018 15:33:30 +0000 Received: from tamriel.snowman.net ([96.255.250.162]) by makus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gSknH-0003VD-HA for pgsql-general@lists.postgresql.org; Fri, 30 Nov 2018 15:33:28 +0000 Received: by tamriel.snowman.net (Postfix, from userid 1000) id 3D20A5F7A7; Fri, 30 Nov 2018 10:33:27 -0500 (EST) Date: Fri, 30 Nov 2018 10:33:27 -0500 From: Stephen Frost To: Chris Withers Cc: pgsql-general@lists.postgresql.org Subject: Re: surprising query optimisation Message-ID: <20181130153327.GA3415@tamriel.snowman.net> References: <03a0af57-3ebc-5501-10ef-6a639c7c34b8@withers.org> <20181128224950.GS3415@tamriel.snowman.net> <5d7f8d97-32f3-fd88-44cc-948673579a71@withers.org> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="i0lx7FdJFH1KG+55" Content-Disposition: inline In-Reply-To: <5d7f8d97-32f3-fd88-44cc-948673579a71@withers.org> User-Agent: Mutt/1.5.24 (2015-08-30) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --i0lx7FdJFH1KG+55 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Greetings, * Chris Withers (chris@withers.org) wrote: > On 28/11/2018 22:49, Stephen Frost wrote: > >* Chris Withers (chris@withers.org) wrote: > >>We have an app that deals with a lot of queries, and we've been slowly > >>seeing performance issues emerge. We take a lot of free form queries fr= om > >>users and stumbled upon a very surprising optimisation. > >> > >>So, we have a 'state' column which is a 3 character string column with = an > >>index on it. Despite being a string, this column is only used to store = one > >>of three values: 'NEW', 'ACK', or 'RSV'. > > > >Sounds like a horrible field to have an index on. >=20 > That's counter-intuitive for me. What leads you to say this and what would > you do/recommend instead? For this, specifically, it's because you end up with exactly what you have: a large index with tons of duplicate values. Indexes are particularly good when you have high-cardinality fields. Now, if you have a skewed index, where there's one popular value and a few much less popular ones, then that's where you really want a partial index (as I suggest earlier) so that queries against the non-popular value(s) is able to use the index and the index is much smaller. Of course, for this to work you need to set up the partial index correctly and make sure that your queries end up using it. Thanks! Stephen --i0lx7FdJFH1KG+55 Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iQIcBAEBCgAGBQJcAVhGAAoJEO1sijiDR2RVeUYQAKNiXpuc138lqdx0ghHkjRxs d8i/hI74eTYuCDMo7GIpfDoihIZ0lv+5bQvq7veEDZ9WFU97+bZIqcCTjOU/bfgF ow6A2Xf7MtZlsrRFgXpybFD/6SoYYRqRzWycSLsIh0NhjK4ES8qclMCU3oF7yvjZ utDjcw+IbW5OubOSo5uIiLowu8+ccGszhuf2XZP7oN5trO8PFAithYu1EGxloXt0 k0pz/O5GE12aDL1VEEBUM4HlyssDgYrcoiEnon3kM+KZiOxfhtGTP7jfFc4EUNNd pEMOdom1eEJeF/W+s6CdyrutexDauntZehxlNhV4Xxvna+z3eHvw8crkWy1ZYVPg QfdpTXtbc4XSomwTJ9VwGozxTQEWBzb5mRnaZA8N++w8dOVeEnYGGjZAyl2WIx3x BVr9MCI4aY4ns5vCvQcMY0aSzoKgU8pJ89vbcOiEzvzkiBf/fJuh11o/yfQeE8Gh IgnvjzC6PO1klegTaI43NyZbz1USdf7Ot5I/vfEM70f6MkgdppeZlE3pyrhfyLIr IspVzYUi0R6wuZLeLew6c2Us504UkMCOSQzOeEMrgTx5LjmSTV5eeOpxGhp4aj7u U3TwygWDj3Joid3jI8S4ifH2YNokB35Zp+4gCnp0LfNleK5lBNRooVl7aWpUs4na sq7s8iT9RjUtC0Ji8Z8Q =9Vvo -----END PGP SIGNATURE----- --i0lx7FdJFH1KG+55--