|
| 1 | +# [Improved Query Performance with Variant Indexes (1997)](https://scholar.google.com/scholar?cluster=3279297021955127822) |
| 2 | +This paper surveys three types of indexes: value-list indexes (old), bit-sliced |
| 3 | +indexes (new), and projection indexes (new). It then shows how to compute |
| 4 | +aggregates, range predicates, and OLAP queries using these three types of |
| 5 | +indexes. |
| 6 | + |
| 7 | +## Value-List Indexes |
| 8 | +A **Value-List index** is a B+ tree index. Each leaf of a Value-List index |
| 9 | +either stores a list of record ids (RIDs) or a bitmap. |
| 10 | + |
| 11 | +A **bitmap** on a set $T$ of $n$ tuples compactly represents a subset of $T$. |
| 12 | +It is implemented as an $M$-length bitstring and a mapping $m: T \to [0, M-1]$. |
| 13 | +If $t_i$ is present in the subset, then the $m(t_i)$th bit in the bitstring is |
| 14 | +set. Note that $m(t_i)$ does not have to be $i$. Often times, if a tuple $t$ is |
| 15 | +the $i$th tuple on page $p$, then $m(t)$ is a number $j$ where the high order |
| 16 | +bits of $j$ are $p$ and the low order bits of $j$ are $i$. |
| 17 | + |
| 18 | +The leaf entry for key $k$ in a bitmap Value-List index is a bitmap indicating |
| 19 | +which tuples have key $k$. If the index key of the B+ tree has only a few |
| 20 | +values, then a bitmap B+ tree can take up less space than an RID B+ tree. |
| 21 | + |
| 22 | +Moreover, bitwise operations over a bitmap can be computed very efficiently. |
| 23 | +This comes in handy. For example, imagine we have the query `SELECT * FROM R |
| 24 | +WHERE a and b`. If we compute two bitmaps $f_a$ and $f_b$ indicating which |
| 25 | +tuples of `R` satisfy `a` and `b`, then we can quickly compute the bitwise AND |
| 26 | +of $f_a$ and $f_b$. |
| 27 | + |
| 28 | +Imagine that we can fit 1000 bits on a single page. We can segment the rows of |
| 29 | +a table into sets of 1000. This lets us compress RID lists and also avoid some |
| 30 | +bitstring operations (see paper for details). |
| 31 | + |
| 32 | +## Projection Indexes |
| 33 | +A **projection index** on a column is just that column stored contiguously. For |
| 34 | +example, if we had the following table `R(a, b, c)`: |
| 35 | + |
| 36 | +``` |
| 37 | ++---+---+---+ |
| 38 | +| a | b | c | |
| 39 | ++---+---+---+ |
| 40 | +| 1 | 2 | 3 | |
| 41 | +| 2 | 3 | 4 | |
| 42 | +| 3 | 4 | 5 | |
| 43 | +| 4 | 5 | 6 | |
| 44 | +| 5 | 6 | 7 | |
| 45 | ++---+---+---+ |
| 46 | +``` |
| 47 | + |
| 48 | +then a projection index on `b` would be |
| 49 | + |
| 50 | +``` |
| 51 | ++---+ |
| 52 | +| b | |
| 53 | ++---+ |
| 54 | +| 2 | |
| 55 | +| 3 | |
| 56 | +| 4 | |
| 57 | +| 5 | |
| 58 | +| 6 | |
| 59 | ++---+ |
| 60 | +``` |
| 61 | + |
| 62 | +## Bit-Sliced Indexes |
| 63 | +Imagine a column of integers that looks something like this: |
| 64 | + |
| 65 | +``` |
| 66 | ++---+ |
| 67 | +| 0 | |
| 68 | +| 1 | |
| 69 | +| 2 | |
| 70 | +| 3 | |
| 71 | +| 4 | |
| 72 | ++---+ |
| 73 | +``` |
| 74 | + |
| 75 | +We can view each integer as a bitstring: |
| 76 | + |
| 77 | +``` |
| 78 | ++-----+ |
| 79 | +| 000 | |
| 80 | +| 001 | |
| 81 | +| 010 | |
| 82 | +| 011 | |
| 83 | +| 100 | |
| 84 | ++-----+ |
| 85 | +``` |
| 86 | + |
| 87 | +A **bit-sliced index** stores a bitstring for every column of bits. For |
| 88 | +example, a bit-sliced index on the column above would store `00001` (first |
| 89 | +column), `00110` (second column), and `01010` (third column). |
| 90 | + |
| 91 | +## Computing Aggregates with Indexes |
| 92 | +Imagine we want to compute the query `SELECT SUM(c) FROM R WHERE p` for some |
| 93 | +predicate `p`. Imagine we have already computed a bitmap $f_p$ indicating which |
| 94 | +tuples satisfy `p`. Here's how compute the query with the various indexes: |
| 95 | + |
| 96 | +1. **No index.** Without any index, we're forced to read through `R`. Assuming |
| 97 | + that only a fraction of the tuples in `R` satisfy `p`, some pages of `R` end |
| 98 | + up not having any satisfied tuples, so we don't have to read those. |
| 99 | +2. **Value-List bitmap index.** We iterate over every key $k$ to retrieve a |
| 100 | + bitamap $f_k$ and compute the bitwise AND of $f_k$ and $f_p$. We compute the |
| 101 | + popcount of this AND, multiply it by $k$, and add it to our running sum. |
| 102 | +3. **Projection index.** We iterate through the projection index and add any |
| 103 | + value with a bit set in $f_p$. |
| 104 | +4. **Bit-sliced index.** For each column $c_i$, we add $\text{popcount}(i) * |
| 105 | + 2^i$ to our sum. |
| 106 | + |
| 107 | +There are other algorithms to compute other aggregate functions as well (see |
| 108 | +paper). Here is a summary of the best index for each aggregate: |
| 109 | + |
| 110 | +| Aggregate | Best Index | |
| 111 | +| --------- | --------------- | |
| 112 | +| sum | bit-sliced | |
| 113 | +| count | no index needed | |
| 114 | +| average | bit-sliced | |
| 115 | +| max/min | value-list | |
| 116 | +| median | value-list | |
| 117 | + |
| 118 | +## Computing Range Predicates with Indexes |
| 119 | +Imagine we want to compute the query `SELECT * FROM c > 100 AND p` where for |
| 120 | +some arbitrary predicate `p`. Given a bitmap $f_p$ indicating which tuples |
| 121 | +satisfy `p`, we want to compute a bitmap $f$ indicating which tuples satisfy |
| 122 | +`p` and the range predicate `c > 100`. |
| 123 | + |
| 124 | +1. **Value-List bitmap index.** We OR together every bitmap $b$ for every key |
| 125 | + $k$ that satisfies the range predicate and then AND it with $f_p$. |
| 126 | +2. **Projection index.** We iterate through the values indicated by $f_p$ and |
| 127 | + see which satisfy the range predicate. |
| 128 | +3. **Bit-sliced index.** We perform some intense bit tricks (see paper). |
| 129 | + |
| 130 | +In summary, Value-List indexes are best for narrow ranges and bit-sliced |
| 131 | +indexes are best for wide ranges. |
| 132 | + |
| 133 | +TODO(mwhittaker): Read and summarize the last three sections of this paper. |
| 134 | +They are pretty dense and a little boring. |
| 135 | + |
| 136 | +<script type="text/javascript" async |
| 137 | + src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-MML-AM_CHTML"> |
| 138 | +</script> |
0 commit comments