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 1gMcuv-0000AS-Su for pgsql-general@arkaria.postgresql.org; Tue, 13 Nov 2018 17:56:02 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gMcut-00017z-Bg for pgsql-general@arkaria.postgresql.org; Tue, 13 Nov 2018 17:55:59 +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 1gMcut-00017s-42 for pgsql-general@lists.postgresql.org; Tue, 13 Nov 2018 17:55:59 +0000 Received: from mx2.mailbox.org ([80.241.60.215]) by magus.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gMcum-0002r4-Cd for pgsql-general@lists.postgresql.org; Tue, 13 Nov 2018 17:55:58 +0000 Received: from smtp2.mailbox.org (unknown [IPv6:2001:67c:2050:105:465:1:2:0]) (using TLSv1.2 with cipher ECDHE-RSA-CHACHA20-POLY1305 (256/256 bits)) (No client certificate requested) by mx2.mailbox.org (Postfix) with ESMTPS id 1E2A9A11DC; Tue, 13 Nov 2018 18:55:51 +0100 (CET) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp2.mailbox.org ([80.241.60.241]) by spamfilter02.heinlein-hosting.de (spamfilter02.heinlein-hosting.de [80.241.56.116]) (amavisd-new, port 10030) with ESMTP id 7iiUT660FNXm; Tue, 13 Nov 2018 18:55:50 +0100 (CET) Message-ID: <53b360cd94eaf9823e3b8746c953351f54cdde80.camel@cybertec.at> Subject: Re: WTF with hash index? From: Laurenz Albe To: =?UTF-8?Q?=D0=9E=D0=BB=D0=B5=D0=B3_?= =?UTF-8?Q?=D0=A1=D0=B0=D0=BC=D0=BE=D0=B9=D0=BB=D0=BE=D0=B2?= , "pgsql-generallists.postgresql.org" Date: Tue, 13 Nov 2018 18:55:49 +0100 In-Reply-To: References: Organization: Cybertec Content-Type: text/plain; charset="UTF-8" Mime-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Олег Самойлов 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