pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Laurenz Albe <laurenz.albe@cybertec.at>
To: Олег Самойлов <splarv@ya.ru>
To: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: WTF with hash index?
Date: Tue, 13 Nov 2018 18:55:49 +0100
Message-ID: <53b360cd94eaf9823e3b8746c953351f54cdde80.camel@cybertec.at> (raw)
In-Reply-To: <A841C4BC-A878-497E-AD9B-4DE0830DCC68@ya.ru>
References: <A841C4BC-A878-497E-AD9B-4DE0830DCC68@ya.ru>

Олег Самойлов wrote:
> \set table_size 1000000
> begin;
> create table gender (gender varchar);
> 
> insert into gender (gender) select case when random<0.50 then 'female' when random<0.99 then 'male' else 'other' end from (select random() as random, generate_series(1,:table_size)) as subselect;
> 
> create index gender_btree on gender using btree (gender);
> create index gender_hash on gender using hash (gender);
> commit;
> vacuum full analyze;
> 
> Vacuum full is not necessary here, just a little vodoo programming. I expected that the hash index will be much smaller and quicker than the btree index, because it doesn’t keep values inside itself, only hashes. But:
> 
> => \d+
>                    List of relations
>  Schema |  Name  | Type  | Owner | Size  | Description
> --------+--------+-------+-------+-------+-------------
>  public | gender | table | olleg | 35 MB |
> (1 row)
> 
> => \di+
>                           List of relations
>  Schema |     Name     | Type  | Owner | Table  | Size  | Description
> --------+--------------+-------+-------+--------+-------+-------------
>  public | gender_btree | index | olleg | gender | 21 MB |
>  public | gender_hash  | index | olleg | gender | 47 MB |
> (2 rows)
> 
> The hash index not only is more than the btree index, but also is bigger than the table itself. What is wrong with the hash index?

I guess the problem here is that there are so few distinct values, so
all the index items end up in only three hash buckets, forming large
linked lists.

I can't tell off-hand why that would make the index so large though.

Anyway, indexes are pretty useless in such a case.

Is the behavior the same if you have many distinct values?

Yours,
Laurenz Albe
-- 
Cybertec | https://www.cybertec-postgresql.com





view thread (10+ messages)  latest in thread

Message-ID: <53b360cd94eaf9823e3b8746c953351f54cdde80.camel@cybertec.at>
Permalink:  ../53b360cd94eaf9823e3b8746c953351f54cdde80.camel@cybertec.at/
Also on:    postgresql.org/message-id/53b360cd94eaf9823e3b8746c953351f54cdde80.camel@cybertec.at

 · 

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: laurenz.albe@cybertec.at, splarv@ya.ru
  Subject: Re: WTF with hash index?
  In-Reply-To: <53b360cd94eaf9823e3b8746c953351f54cdde80.camel@cybertec.at>
  References: <A841C4BC-A878-497E-AD9B-4DE0830DCC68@ya.ru> <53b360cd94eaf9823e3b8746c953351f54cdde80.camel@cybertec.at>

* 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