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 1gSiHq-00080n-FN for pgsql-general@arkaria.postgresql.org; Fri, 30 Nov 2018 12:52:51 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gSiHn-00084N-3C for pgsql-general@arkaria.postgresql.org; Fri, 30 Nov 2018 12:52:47 +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 1gSiHl-00084G-NB for pgsql-general@lists.postgresql.org; Fri, 30 Nov 2018 12:52:46 +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 1gSiHh-0004qh-Pt for pgsql-general@lists.postgresql.org; Fri, 30 Nov 2018 12:52:45 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id AA08220087; Fri, 30 Nov 2018 07:52:39 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Fri, 30 Nov 2018 07:52:39 -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=MvDikGubIiIr0OInBLc4qFJt2+hY7PwhHXSpl3m7nOk=; b=FjQJ7gi5MYLL bxngKudscd5ghNFUE5FPpDnY/QkBM1lx7Ca+QbudCn/+JVEefdWFUUjj/HKqTvtX 4NsHEndis4D0KVIYAdqWzwNsj1+KAKRXKWHYCEg0g3VCAfiRfxNhHdyt17KGa4gs TAPDHViPt1pzTKJpW9Ln3THgj+mu6SE= 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=MvDikGubIiIr0OInBLc4qFJt2+hY7PwhHXSpl3m7n Ok=; b=oGNdWW42xeUcrfih/pXZDx5+1kVN07/7UWr4gmDPKsazgkLD/V/1+pX++ YXHIXv7UUz7aI9V1WCKxKYWM2GozEyf7k+Y4+wicSUA0SARNfkWNNI9eR8CdwnEO RNZVaSl87+89G9w8mgXD/DW6SW1WagBkTsZEK9EI1I/wNIR03rImN4vOxz/mfqZP 2WmlLgArW2WFgUb5rkSMUVVsuxZrlCDZQG2cZC0XXS4KXbVMWF26lhAyafVatIgS AkwK6d13DDPvYHYCvvZkEvVf5b15buoUV3l0j93cp34v0Pkas3de6+HB6Vk1Qrf6 cPsuwaEhu03nB+xMk71hg0CBDNrzg== 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 7A3BBE462B; Fri, 30 Nov 2018 07:52:38 -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> From: Chris Withers Message-ID: <5d7f8d97-32f3-fd88-44cc-948673579a71@withers.org> Date: Fri, 30 Nov 2018 12:52:37 +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: <20181128224950.GS3415@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 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? > 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. table definition: # \d alerts_alert Table "public.alerts_alert" Column | Type | Modifiers -----------------+--------------------------+----------- tags | jsonb | not null id | character varying(86) | not null earliest_seen | timestamp with time zone | not null latest_seen | timestamp with time zone | not null created | timestamp with time zone | not null modified | timestamp with time zone | not null type | character varying(300) | not null state | character varying(3) | not null until | timestamp with time zone | latest_note | text | not null created_by_id | integer | not null modified_by_id | integer | not null owner_id | integer | owning_group_id | integer | not null latest_new | timestamp with time zone | not null Indexes: "alerts_alert_pkey" PRIMARY KEY, btree (id) "alert_tags_index" gin (tags) "alerts_alert_1efacf1d" btree (latest_seen) "alerts_alert_3103a7d8" btree (until) "alerts_alert_599dcce2" btree (type) "alerts_alert_5e7b1936" btree (owner_id) "alerts_alert_9ae73c65" btree (modified) "alerts_alert_9ed39e2e" btree (state) "alerts_alert_b3da0983" btree (modified_by_id) "alerts_alert_c5151f5a" btree (earliest_seen) "alerts_alert_e2fa5388" btree (created) "alerts_alert_e93cb7eb" btree (created_by_id) "alerts_alert_efea2d76" btree (owning_group_id) "alerts_alert_id_13155e16_like" btree (id varchar_pattern_ops) "alerts_alert_latest_new_e8d1fbde_uniq" btree (latest_new) "alerts_alert_state_90ab480b_like" btree (state varchar_pattern_ops) "alerts_alert_type_3021f46f_like" btree (type varchar_pattern_ops) Foreign-key constraints: "alerts_alert_created_by_id_520608c0_fk_alerts_user_id" FOREIGN KEY (created_by_id) REFERENCES alerts_user(id) DEFERRABLE INITIALLY DEFERRED "alerts_alert_modified_by_id_6db4b04b_fk_alerts_user_id" FOREIGN KEY (modified_by_id) REFERENCES alerts_user(id) DEFERRABLE INITIALLY DEFERRED "alerts_alert_owner_id_0c00548a_fk_alerts_user_id" FOREIGN KEY (owner_id) REFERENCES alerts_user(id) DEFERRABLE INITIALLY DEFERRED "alerts_alert_owning_group_id_a4869b66_fk_auth_group_id" FOREIGN KEY (owning_group_id) REFERENCES auth_group(id) DEFERRABLE INITIALLY DEFERRED Referenced by: TABLE "alerts_alertevent" CONSTRAINT "alerts_alertevent_alert_id_edd734b8_fk_alerts_alert_id" FOREIGN KEY (alert_id) REFERENCES alerts_alert(id) DEFERRABLE INITIALLY DEFERRED Row counts by state: # select state, count(*) from alerts_alert group by 1 order by 1; state | count -------+--------- ACK | 1053 NEW | 1958 RSV | 1528623 (3 rows) here's an example of the "bad" query plan: https://explain.depesz.com/s/cDkp here's an example with all the "state!='RSV'" clauses rewritten as I described: https://explain.depesz.com/s/B9Xi > I'd suggest you check out the wiki article written about this kind of > question: > > https://wiki.postgresql.org/wiki/Slow_Query_Questions Thanks! Chris