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 1gS8ea-00060M-L0 for pgsql-general@arkaria.postgresql.org; Wed, 28 Nov 2018 22:49:56 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gS8eZ-0000O5-B6 for pgsql-general@arkaria.postgresql.org; Wed, 28 Nov 2018 22:49:55 +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 1gS8eY-0000Nx-Vx for pgsql-general@lists.postgresql.org; Wed, 28 Nov 2018 22:49:55 +0000 Received: from tamriel.snowman.net ([96.255.250.162]) by makus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gS8eV-0006SV-W0 for pgsql-general@lists.postgresql.org; Wed, 28 Nov 2018 22:49:53 +0000 Received: by tamriel.snowman.net (Postfix, from userid 1000) id 0DFC55F7AC; Wed, 28 Nov 2018 17:49:51 -0500 (EST) Date: Wed, 28 Nov 2018 17:49:50 -0500 From: Stephen Frost To: Chris Withers Cc: pgsql-general@lists.postgresql.org Subject: Re: surprising query optimisation Message-ID: <20181128224950.GS3415@tamriel.snowman.net> References: <03a0af57-3ebc-5501-10ef-6a639c7c34b8@withers.org> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="c2Li+dg8gNw8MPCF" Content-Disposition: inline In-Reply-To: <03a0af57-3ebc-5501-10ef-6a639c7c34b8@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 --c2Li+dg8gNw8MPCF Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Greetings, * 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 from > users and stumbled upon a very surprising optimisation. >=20 > 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. > One of our most common queries clauses is "state!=3D'RSV'" and we've found > that by substituting this clause with "state=3D'ACK' or state=3D'NEW'" wh= erever > it was used, we've dropped the postgres server's load average from 20 down > to 4 and the CPU usage from 60% in user space down to <5%. You've changed the question you're asking the database. PG doesn't *know* that there's only those three values, but it probably has a pretty good guess about how many records are ACK and how many are NEW thanks to those being in the MCV list. > This seems counter-intuitive to me, so thought I'd ask here. Why would th= is > be likely to make such a difference? We're currently on 9.4, is this > something that's likely to be different (better? worse?) if we got all the > way up to 10 or 11? When you change what you're asking, PG is going to change how it gives you the answer and sometimes that'll be faster and other times it won't be. Really though, if you want something more than wild speculation, posting the 'explain analyze' of each query along with the actual table definitions and sizes and such would be the best way to get it. I'd suggest you check out the wiki article written about this kind of question: https://wiki.postgresql.org/wiki/Slow_Query_Questions Thanks! Stephen --c2Li+dg8gNw8MPCF Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iQIcBAEBCgAGBQJb/xuNAAoJEO1sijiDR2RV1w0QAJm0zzzLcfN8gvT55mJfzHCK G8c7NgbpiS85A970HT/3yg1EEcDyQAfHGumhL3EAYH1kAlB7RcFK6U1+GOw0CP+8 qjTTVWHQDqF8z8RkTxDwtutiK4j6a0cGK4esYR8k9ip4fA15DMqvptEGraz6f+mv eqdaFny6z9cxfduIj0lOsvimwJioqwqDKjNqDM4gpZMy4lAsQF4wfdC1sPr0JXmm A3rwnpa0Az8sbC/OSjOPaNv5a7h8u6U4ZuOk8UcqBp8RkvfbpTEF2lYPiYmQTCJf e0fodAFl6AdUUm4aMpsaLxo3GLXh2C3CYr9hu6oNkZM9Qn2VG1qh6QgarZ5HPfRK kMZ8usRNBEmXaO5iz/dR+0mkrBbLKoH5rk9lk18EWWPCyaJxwUWdiQ6zJjo09G+M JqcH4O4IYUEHCQflSwBJXfYQDHQoH+LefgYpy1vFbcud0zMXpPqfF2TSvE8IAqiI jspm3Vji/rE9zK2KYy2GRSOi40i1ug8CYcI3lk6hqDO8C9dfnO9o5Yvn6NrYE+J3 UCsONZU7b1yYGbBBDGqg8Sutwr0MlPRoEAB3xFSqj8GyO44xw6PM88PZTLiIkagx MS9DDdBj/2rN604RWSgb64Hj4jjYlSgW5jLffiPocW3TYWQYNdJTrGHuCFRYK+U3 RC/XjS0nivBV963tCTbM =7K6h -----END PGP SIGNATURE----- --c2Li+dg8gNw8MPCF--