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 1hWObe-0002M2-Qx for pgsql-general@arkaria.postgresql.org; Thu, 30 May 2019 17:12:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hWOaf-0000PJ-9z for pgsql-general@arkaria.postgresql.org; Thu, 30 May 2019 17:11:45 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hWOae-0000PC-M1 for pgsql-general@lists.postgresql.org; Thu, 30 May 2019 17:11:45 +0000 Received: from mail-qk1-x741.google.com ([2607:f8b0:4864:20::741]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hWOab-0001Re-IV for pgsql-general@postgresql.org; Thu, 30 May 2019 17:11:42 +0000 Received: by mail-qk1-x741.google.com with SMTP id w187so4318088qkb.11 for ; Thu, 30 May 2019 10:11:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:subject:from:in-reply-to:date:cc:message-id:references :to; bh=BQs8f8QyIXc+hIxLaTBmVCEK89ozQ/xpMPUOO0tKCLo=; b=JsProUKslE4L9ynfYJviERSmT0raZ378J+TirMTx+49SNF/ajjdEk79vX9xFjHgodw oFUBIgRsB+i960Ab9RmqGkdLqLoKxP35U+j90DSYkuV/REUzmVIknQtM1K6eOVk8HSnW PSfUBeJ2m8A5nJ46tSjrsZY6ibD8KQ4+NbuDqy7D+Dh1JfN7L9c3hTHv5NW0neADD6qE aceI3UHy7cbst/F6F69SF//t2FdVQID/Z3MrpqFgDGs1FC3MsH3hFIQ2Tv0Pot/qxNxN ygOIBwGOQpMyy+w+heXMWRohMJRy00vlGQFjH2uxu1JsToTLYqCNfjCyzhuw3W16ea0L 4k5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :message-id:references:to; bh=BQs8f8QyIXc+hIxLaTBmVCEK89ozQ/xpMPUOO0tKCLo=; b=JN9mCb6isdPW2uzuqEyPcI+RHRckmYjMraNgPhUhOpNdpOMuMGRDGih41cWwihwDvL FSAf7uV4+/t0VUQiA/MVf3otaFdBvuByzQgX0+Z6SAxs/6j+lqucjh9G62/cd0DIZyBo J4WIQ4j04bqwbe2XyR2BAIyEgMmFkI5zk/lL5EXaNd55/1FvfvRj1tzOoeHh7h72o7my JS95glRCCM0mYJgJ+6IXqy5OMuzusHrBk64rg9Qxx0NxHYhHYR2Q55rTzRQ+eavnW7Sx p3mlCxnFaVPhHoYDJ2C+C7wzQuGaL+ftGyVTFjl9VW/uFBP5pglQBGf0CNO13yNNkzE6 yVRQ== X-Gm-Message-State: APjAAAVSSPij7c60ZI1urqz/9W1Dsem9e2bRKjptHkDqzbAjrmtJyBvt vCOj/RGKzmuCEHzhude6O5I= X-Google-Smtp-Source: APXvYqz3FF/hBqwHfdK7AuRzI05XsdzsljCYeeyirCvcUY6k4gKuhTP1EgKJiINtNneOyb/DxMfWrg== X-Received: by 2002:a05:620a:3:: with SMTP id j3mr3994777qki.95.1559236300626; Thu, 30 May 2019 10:11:40 -0700 (PDT) Received: from [10.35.60.64] ([64.164.57.170]) by smtp.gmail.com with ESMTPSA id q2sm1901894qkf.44.2019.05.30.10.11.38 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 30 May 2019 10:11:38 -0700 (PDT) Content-Type: multipart/alternative; boundary="Apple-Mail=_F1495B54-57D1-4CC5-8C2F-81489F3D6766" Mime-Version: 1.0 (Mac OS X Mail 9.3 \(3124\)) Subject: Re: Questions about btree_gin vs btree_gist for low cardinality columns From: Will Hartung In-Reply-To: Date: Thu, 30 May 2019 10:11:36 -0700 Cc: Postgres General Message-Id: <84EE4616-51B6-479D-8577-CD6F672B8260@gmail.com> References: To: Jeremy Finzel X-Mailer: Apple Mail (2.3124) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --Apple-Mail=_F1495B54-57D1-4CC5-8C2F-81489F3D6766 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > On May 29, 2019, at 10:59 AM, Jeremy Finzel wrote: >=20 >=20 >=20 > On Fri, May 24, 2019 at 10:25 AM Jeremy Finzel > wrote: > I have been hoping for clearer direction from the community about = specifically btree_gin indexes for low cardinality columns (as well as = low cardinality multi-column indexes). In general there is very little = discussion about this both online and in the docs. Rather, the emphasis = for GIN indexes discussed is always on full text search of JSON = indexing, not btree_gin indexes. >=20 > However, I have never been happy with the options open to me for = indexing low cardinality columns and was hoping this could be a big win. = Often I use partial indexes as a solution, but I really want to know = how many use cases btree_gin could solve better than either a normal = btree or a partial index. >=20 > Here are my main questions: >=20 > 1. >=20 > "The docs say regarding *index only scans*: The index type must = support index-only scans. B-tree indexes always do. GiST and SP-GiST = indexes support index-only scans for some operator classes but not = others. Other index types have no support. The underlying requirement is = that the index must physically store, or else be able to reconstruct, = the original data value for each index entry. As a counterexample, GIN = indexes cannot support index-only scans because each index entry = typically holds only part of the original data value." >=20 > This is confusing to say "B-tree indexes always do" and "GIN indexes = cannot support index-only scans", when we have a btree_gin index type. = Explanation please ??? >=20 > Is it true that for a btree_gin index on a regular column, "each index = entry typically holds only part of the original data value"? Do these = still not support index only scans? Could they? I can't see why they = shouldn't be able to for a single indexed non-expression field? Well part of the problem is that =E2=80=9CB-tree=E2=80=9D is a pretty = generic term. Pretty much every index is either some form of hash, or = some form of tree. We use the term B-tree, even though pretty much no = major index is actually a binary tree. They almost always have multiple = branches per node. It=E2=80=99s also important to note that over time, while we may use the = term B-Tree generically, there are many specialized trees used today. Historically, the conventional B+Tree is popular for database indexes. A = B+Tree rather than relying on =E2=80=9Cnodes=E2=80=9D per se, really is = based on pages. Nodes alone tend to be too fine grained, so it=E2=80=99s = a game of mapping the nodes to pages on disk. The page size can impact = the structure of the tree. In a typical B+Tree the values of the index are stored in leaves of the = tree, those leaves are then linked together in order. An index scan = works because the data in the leaves matches the data in the database = record.=20 Say you=E2=80=99re doing something like =E2=80=9CSELECT ID FROM TABLE = WHERE ID LIKE =E2=80=98&X&=E2=80=99=E2=80=9D. A basic index won=E2=80=99t = work for a query like that, so the DB will need to scan every row. But = since ID (in this case) is indexed, we have all of the ID=E2=80=99s in = the table stored both in the table rows themselves, but also in the ID = index. Since there are more ID=E2=80=99s per page in the ID index, and = the ID index has all of the data we need (i.e. the IDs), there=E2=80=99s = not reason to table scan the table itself. Rather, you can just scan the = index. Load every page from the index, walk the nodes, and match the = values for &X&. Much less I/O. Now, things like the GIN indexes, while still trees, store the data = differently. Where a B+Tree stores the indexes in the leaf nodes of the = tree, the GIN indexes break the values down and store parts of the keys = in to the individual nodes of the tree. To reconstruct the index value, = you walk all of the nodes to the end of the branch. A B+Tree, each node = represents the range of nodes along the branch. So, if you have = 1-2-3-4-5-6 in a tree, the root of the tree may say 1-3 are on the left = branch, 4-6 are on the right branch. Looking for 2, you go left. There = the node says =E2=80=9C1-2 are on the left branch, 3 is on the right = branch=E2=80=9D. You go left again, find the node that says =E2=80=9C1 = is to the left, 2 is to the right=E2=80=9D. Go right, and you find 2. = Tada! In a GIN index, if you index =E2=80=9CABCDEF=E2=80=9D, there will be a A = node, B node, C node, D node, etc. (And, please, appreciate I am talking = in broad brush generalities here for the sake of exposition, not = specific details of implementation or algorithms). So, simply, by the = time you get to the bottom of an index, all you find is the pointer to = the data, not the actual key that got you there. The Key is the path = through the tree. The reasons for this, especially for things like full text search, is a = lot of words have a lot in common (notably prefixes) so it makes sense = for specialized indexes to basically compact themselves by removing the = prefixes. Basically each node says =E2=80=9Call the words that start = with =E2=80=98the=E2=80=99 (the, them, their, there, thespian) are this = way". So, you=E2=80=99ll see a node =E2=80=9Cthe=E2=80=9D beneath that = you might find the node =E2=80=9Cspian=E2=80=9D (thespian with the = =E2=80=98the=E2=80=99 removed). These indexes are more compact and = better suited for text (vs, say, random GUIDs). Because text has notable = properties of information density and such. For just numbers (especially random numbers), there=E2=80=99s no real = value. This is why =E2=80=9Cscanning the index=E2=80=9D doesn=E2=80=99t work = well on these kinds of structures compared to a classic B-Tree. Nor does = a HASH index. Since, hash indexes store the hash =E2=80=94 not the key = value. Nothing there to scan of value. >=20 > 2.=20 >=20 > Lack of index only scans is definitely a downside. However, I see = basically identical performance, but way less memory and space usage, = for gin indexes. In terms of read-only performance, if index only scans = are not a factor, why not always recommend btree_gin indexes instead of = regular btree for low cardinality fields, which will yield similar = performance but use far, far less space and resources? I simply can=E2=80=99t speak to the applicability of index to low = cardinality columns as those relate to different issues not directly = related to searching the index. If you have 10M rows with a =E2=80=9CSTATUS=E2=80=9D column of 1 or 2, = and an index on that column, then you have a 2 node index with a = bazillion row pointers. Some systems (I can=E2=80=99t speak to PG in = this regard) degenerate in this kind of use case since the index is more = or less designed to work great in unique identifiers than low = cardinality values. The representation of large record pointer lists may = just not be handled well as edge cases. Historically, I try to avoid low cardinality indexes, simply because = I=E2=80=99ve had problems in the past (yes, decades ago, but lessons = learned). In those case I force high cardinality (in this case, maybe = create a combined index of STATUS and ID, which makes an ostensibly low = value index in to a unique one). This is very important for heavily = weight indexes (like where most of the statues are 1, but you mostly = just want to look for the 2=E2=80=99s). But then its a matter of = convincing the optimizer that the index isn=E2=80=99t utterly worthless = no matter what you query for and table scans anyway. Regards, Will Hartung --Apple-Mail=_F1495B54-57D1-4CC5-8C2F-81489F3D6766 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8
On May 29, 2019, at 10:59 AM, Jeremy Finzel <finzelj@gmail.com> = wrote:



On Fri, May 24, 2019 at 10:25 AM Jeremy = Finzel <finzelj@gmail.com> wrote:
I have been hoping for clearer direction from the community = about specifically btree_gin indexes for low cardinality columns (as = well as low cardinality multi-column indexes).  In general there is = very little discussion about this both online and in the docs.  = Rather, the emphasis for GIN indexes discussed is always on full text = search of JSON indexing, not btree_gin indexes.

However, I have never = been happy with the options open to me for indexing low cardinality = columns and was hoping this could be a big win.  Often I use = partial indexes as a solution, but I really want to know how many use = cases btree_gin could solve better than either a normal btree or a = partial index.

Here are my main questions:

1.

"The docs say regarding *index only = scans*: The index type must support index-only scans. B-tree = indexes always do. GiST and SP-GiST indexes support index-only scans for = some operator classes but not others. Other index types have no support. = The underlying requirement is that the index must physically store, or = else be able to reconstruct, the original data value for each index = entry. As a counterexample, GIN indexes cannot support index-only scans = because each index entry typically holds only part of the original data = value."

This = is confusing to say "B-tree indexes always do" and "GIN indexes cannot = support index-only scans", when we have a btree_gin index type.  = Explanation please ???

Is it true that for a btree_gin index on a regular column, = "each index entry typically holds only part of the original data = value"?  Do these still not support index only scans?  Could = they?  I can't see why they shouldn't be able to for a single = indexed non-expression = field?

Well part of the problem is that =E2=80=9CB-tree=E2=80= =9D is a pretty generic term. Pretty much every index is either some = form of hash, or some form of tree. We use the term B-tree, even though = pretty much no major index is actually a binary tree. They almost always = have multiple branches per node.

It=E2=80=99s also important to note that over = time, while we may use the term B-Tree generically, there are many = specialized trees used today.

Historically, the conventional B+Tree is popular = for database indexes. A B+Tree rather than relying on =E2=80=9Cnodes=E2=80= =9D per se, really is based on pages. Nodes alone tend to be too fine = grained, so it=E2=80=99s a game of mapping the nodes to pages on disk. = The page size can impact the structure of the tree.

In a typical B+Tree the values of the index are = stored in leaves of the tree, those leaves are then linked together in = order. An index scan works because the data in the leaves matches the = data in the database record. 

Say= you=E2=80=99re doing something like =E2=80=9CSELECT ID FROM TABLE WHERE = ID LIKE =E2=80=98&X&=E2=80=99=E2=80=9D. A basic index won=E2=80=99= t work for a query like that, so the DB will need to scan every row. But = since ID (in this case) is indexed, we have all of the ID=E2=80=99s in = the table stored both in the table rows themselves, but also in the ID = index. Since there are more ID=E2=80=99s per page in the ID index, and = the ID index has all of the data we need (i.e. the IDs), there=E2=80=99s = not reason to table scan the table itself. Rather, you can just scan the = index. Load every page from the index, walk the nodes, and match the = values for &X&. Much less I/O.

Now, things like the GIN indexes, while still = trees, store the data differently. Where a B+Tree stores the indexes in = the leaf nodes of the tree, the GIN indexes break the values down and = store parts of the keys in to the individual nodes of the tree. To = reconstruct the index value, you walk all of the nodes to the end of the = branch. A B+Tree, each node represents the range of nodes along the = branch. So, if you have 1-2-3-4-5-6 in a tree, the root of the tree may = say 1-3 are on the left branch, 4-6 are on the right branch. Looking for = 2, you go left. There the node says =E2=80=9C1-2 are on the left branch, = 3 is on the right branch=E2=80=9D. You go left again, find the node that = says =E2=80=9C1 is to the left, 2 is to the right=E2=80=9D. Go right, = and you find 2. Tada!

In a GIN = index, if you index =E2=80=9CABCDEF=E2=80=9D, there will be a A node, B = node, C node, D node, etc. (And, please, appreciate I am talking in = broad brush generalities here for the sake of exposition, not specific = details of implementation or algorithms). So, simply, by the time you = get to the bottom of an index, all you find is the pointer to the data, = not the actual key that got you there. The Key is the path through the = tree.

The reasons for this, = especially for things like full text search, is a lot of words have a = lot in common (notably prefixes) so it makes sense for specialized = indexes to basically compact themselves by removing the prefixes. = Basically each node says =E2=80=9Call the words that start with = =E2=80=98the=E2=80=99 (the, them, their, there, thespian) are this way". = So, you=E2=80=99ll see a node =E2=80=9Cthe=E2=80=9D beneath that you = might find the node =E2=80=9Cspian=E2=80=9D (thespian with the =E2=80=98th= e=E2=80=99 removed). These indexes are more compact and better suited = for text (vs, say, random GUIDs). Because text has notable properties of = information density and such.

For = just numbers (especially random numbers), there=E2=80=99s no real = value.

This is why =E2=80=9Cscanning = the index=E2=80=9D doesn=E2=80=99t work well on these kinds of = structures compared to a classic B-Tree. Nor does a HASH index. Since, = hash indexes store the hash =E2=80=94 not the key value. Nothing there = to scan of value.


2. 

Lack of index only scans = is definitely a downside.  However, I see basically identical = performance, but way less memory and space usage, for gin indexes.  = In terms of read-only performance, if index only scans are not a factor, = why not always recommend btree_gin indexes instead of regular btree for = low cardinality fields, which will yield similar performance but use = far, far less space and = resources?
I simply can=E2=80=99t speak to the applicability = of index to low cardinality columns as those relate to different issues = not directly related to searching the index.

If you have 10M rows with a =E2=80=9CSTATUS=E2=80=9D= column of 1 or 2, and an index on that column, then you have a 2 node = index with a bazillion row pointers. Some systems (I can=E2=80=99t speak = to PG in this regard) degenerate in this kind of use case since the = index is more or less designed to work great in unique identifiers than = low cardinality values. The representation of large record pointer lists = may just not be handled well as edge cases.

Historically, I try to avoid low cardinality = indexes, simply because I=E2=80=99ve had problems in the past (yes, = decades ago, but lessons learned). In those case I force high = cardinality (in this case, maybe create a combined index of STATUS and = ID, which makes an ostensibly low value index in to a unique one). This = is very important for heavily weight indexes (like where most of the = statues are 1, but you mostly just want to look for the 2=E2=80=99s). = But then its a matter of convincing the optimizer that the index isn=E2=80= =99t utterly worthless no matter what you query for and table scans = anyway.

Regards,

Will Hartung

= --Apple-Mail=_F1495B54-57D1-4CC5-8C2F-81489F3D6766--