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 1gUVUo-0007NW-KT for pgsql-general@arkaria.postgresql.org; Wed, 05 Dec 2018 11:37:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gUVUm-0001D7-SS for pgsql-general@arkaria.postgresql.org; Wed, 05 Dec 2018 11:37:36 +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_SHA1:256) (Exim 4.89) (envelope-from ) id 1gUVUl-0001D0-Pa for pgsql-general@lists.postgresql.org; Wed, 05 Dec 2018 11:37:36 +0000 Received: from out4-smtp.messagingengine.com ([66.111.4.28]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gUVUh-0005xj-EM for pgsql-general@lists.postgresql.org; Wed, 05 Dec 2018 11:37:35 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 834BD21D8B; Wed, 5 Dec 2018 06:37:29 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Wed, 05 Dec 2018 06:37:29 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=withers.org; h= subject:to:cc:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=mesmtp; bh=nAor5egsNvgxLxOnLAT1CuEWwFjYKNVZVTpHhEO9wSo=; b=D1wc98IVM9JB Bq9rH4kZUsig4LHdw3HIDiVB0oSBLjo+n9To0FTTy096UaV7H8Kfu1zDDIPXOUWS gq4W9KC2qovVKKERGTopLix7HIML0z24nw/z7CK0EYOtjc9rIR68PzYlVBdYwRON RUh74TX8Hf41do6cd5vBUdeg7Q8hpx8= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; bh=nAor5egsNvgxLxOnLAT1CuEWwFjYKNVZVTpHhEO9w So=; b=mdwICfQ7lvTMAf45ikGlioUUlAno2cK8f50p1WFVyOgn8gwOhK1KVirvI CDXyBgtr1fUOX860HkRHoWB28bx3q3/nWn2Cxqs0m0ldYPJi+2Lkzu46Dv92I6YC rE8eANbklPwlw3G+mND/qwdFDleoEDBFZcm0K4rdUVh2lmgSkUd3e8r1WlKPtsS8 7KQ98JNooHCAvOceswJgNuam2kvVRia2jXEGQUU0DMHf0uwX+uqjfHlFeA1Nypyh FUQaDFLnO2k9uqqKFFapcVzTxS1oIvAPlFHvtF5p6EXe3ACikKLOiUoV/N20WLU9 VFsBvzyQ3Lhgu6zuG9n7RhfM1ThNQ== X-ME-Sender: X-ME-Proxy: Received: from [172.25.22.95] (unknown [213.174.214.6]) by mail.messagingengine.com (Postfix) with ESMTPA id 95174E446A; Wed, 5 Dec 2018 06:37:28 -0500 (EST) Subject: Re: surprising query optimisation To: Stephen Frost Cc: pgsql-general@lists.postgresql.org References: <03a0af57-3ebc-5501-10ef-6a639c7c34b8@withers.org> <20181128224950.GS3415@tamriel.snowman.net> <5d7f8d97-32f3-fd88-44cc-948673579a71@withers.org> <20181130153327.GA3415@tamriel.snowman.net> From: Chris Withers Message-ID: Date: Wed, 5 Dec 2018 11:37:27 +0000 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:64.0) Gecko/20100101 Thunderbird/64.0 MIME-Version: 1.0 In-Reply-To: <20181130153327.GA3415@tamriel.snowman.net> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 30/11/2018 15:33, Stephen Frost wrote: > 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 from >>>> 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. >> >> 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. Interesting! In my head, for some reason, I'd always assumed a btree index would break down a char field based on the characters within it. Does that never happen? If I changed this to be an enum field, would != still perform poorly or can the query optimiser spot that it's an enum and just look for the other options? cheers, Chris