pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Chris Withers <chris@withers.org>
To: Stephen Frost <sfrost@snowman.net>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: surprising query optimisation
Date: Wed, 5 Dec 2018 11:37:27 +0000
Message-ID: <d2c8a815-7636-7fcf-b1bb-9ade172ddb70@withers.org> (raw)
In-Reply-To: <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>
	<20181130153327.GA3415@tamriel.snowman.net>

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




view thread (19+ messages)  latest in thread

Message-ID: <d2c8a815-7636-7fcf-b1bb-9ade172ddb70@withers.org>
Permalink:  ../d2c8a815-7636-7fcf-b1bb-9ade172ddb70@withers.org/
Also on:    postgresql.org/message-id/d2c8a815-7636-7fcf-b1bb-9ade172ddb70@withers.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: chris@withers.org, sfrost@snowman.net
  Subject: Re: surprising query optimisation
  In-Reply-To: <d2c8a815-7636-7fcf-b1bb-9ade172ddb70@withers.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> <d2c8a815-7636-7fcf-b1bb-9ade172ddb70@withers.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox