Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you only have 1 row in the table, the cardinality for the index should be 1, of course. It's just counting the number of unique values. </p> <p>If you think of an index as a lookup-table based on buckets (like a hash), then the cardinality is the number of buckets.</p> <p>Here's how it works: When you build an index over a set of columns <code>(a,b,c,d)</code>, then the database goes over all the rows in the table, looking at the ordered quadruplets of those 4 columns, for each row. Let's say your table looks like this:</p> <pre><code>a b c d e -- -- -- -- -- 1 1 1 1 200 1 1 1 1 300 1 2 1 1 200 1 3 1 1 200 </code></pre> <p>So what the database looks at is just the 4 columns (a,b,c,d):</p> <pre><code>a b c d -- -- -- -- 1 1 1 1 1 2 1 1 1 3 1 1 </code></pre> <p>See that there are only 3 unique rows left? Those will become our buckets, but we'll get back to that. In reality, there's also a record id, or row identifier for each row in the table. So our original table looks like this:</p> <pre><code>(row id) a b c d e -------- -- -- -- -- -- 00000001 1 1 1 1 200 00000002 1 1 1 1 300 00000003 1 2 1 1 200 00000004 1 3 1 1 200 </code></pre> <p>So when we look at only the 4 columns of (a,b,c,d), we're really looking also at the row id:</p> <pre><code>(row id) a b c d -------- -- -- -- -- 00000001 1 1 1 1 00000002 1 1 1 1 00000003 1 2 1 1 00000004 1 3 1 1 </code></pre> <p>But we want to do lookup by (a,b,c,d) and not by row id, so we produce something like this:</p> <pre><code>(a,b,c,d) (row id) --------- -------- 1,1,1,1 00000001 1,1,1,1 00000002 1,2,1,1 00000003 1,3,1,1 00000004 </code></pre> <p>And finally, we group all the row ids of rows that have identicle (a,b,c,d) values together:</p> <pre><code>(a,b,c,d) (row id) --------- --------------------- 1,1,1,1 00000001 and 00000002 1,2,1,1 00000003 1,3,1,1 00000004 </code></pre> <p>See that? The values of (a,b,c,d), which are (1,1,1,1) (1,2,1,1) and (1,3,1,1) have become keys for our lookup table into the rows of the original table.</p> <p>Actually, none of this really happens, but it should give you a good idea on how a "naive" (i.e. straight-forward) implementation of an index might be done.</p> <p>But the bottom line is this: cardinality just measures how many unique rows there are in an index. And in our example that was the number of keys in our lookup table, which was 3.</p> <p>Hope that helps!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload