Understanding the basics of keyed tables

Hi,

Just a couple of questions about keyed tables, which I am sure will be trivial to someone who is already familiar with KDB.

I couldn’t find the answers I was looking for in the documentation, or Q For Mortals.

I also consulted Q Tips and Machine Learning and Big Data with kdb+/q, and I found conflicting information about what a keyed-table actually is.

My primary question is “what is a keyed table?”. I want to understand in detail what it is and how it works.

I have seen I think at least three different descriptions, which all conflict and cannot all be simultaneously true.

This is what I have read:

  • A keyed table is a dictionary with a single key and a single value. The key is a table. The value is a table. In effect, this is the same as a pair of tables.
  • A keyed table is a dictionary with many keys and many values. Each key is a table row, each value is a table row. This is weird, because KDB is a columnar data storage engine. So it cannot be implemented like this, because this is a description of row-major data and not column-major data.
  • A keyed table is a dictionary just as a regular table is a dictionary, however some columns are marked as being part of the key. This is a bit like a table with attributes, with a new attribute meaning “this column is part of the key” (and in which order).

I am sure that none of these is completely accurate.

It is also worth mentioning that in explaining what a keyed table is, there are at least 3 ways to explain it.

  • The data layout in memory and implementation of a keyed table
  • An explanation about what a keyed table is “logically”
  • An explanation about what a keyed table is “semantically”

Is anyone able to clarify exactly what a keyed table is?

1/2 have some partial correct pieces mixed with untruths. Ignore 3.

A kdb+ dictionary has many keys and values. In memory the keys are stored as a single vector and the values are stored as a single vector.

// key and value vectors
q)k:`a`b`c
q)v:1 2 3

// Construct a dictionary
q)d:k!v
q)d
a| 1
b| 2
c| 3

// Access value using a key
q)d`b
2

A kdb+ keyed table has many keys and values. The keys are a table. The values are a table. Tables are made up of vectors (.i.e columns).

// Create a keyed table
q)kt:([k1:`a`b`c; k2:`d`e`f] v1:1 2 3;v2:4 5 6)
q)kt
k1 k2| v1 v2
-----| -----
a  d | 1  4
b  e | 2  5
c  f | 3  6

// The keys are a table
q)key kt
k1 k2
-----
a  d
b  e
c  f

// The values are a table
q)value kt
v1 v2
-----
1  4
2  5
3  6

// Lookup on a key searches the key table, and then extracts the corresponding row in the value table
q)kt[(`b;`e)]
v1| 2
v2| 5