Back to The Four Attributes

Attributes · The Four Attributes · Practice Question

Name All the kdb+ Attributes and Describe Cases Where Each Is Used

Sign in to track your progress

Question

What are the four kdb+ attribute types? For each one, describe its internal mechanism and a concrete use case.

Solution

/ Sorted `s#
L: `s# 1 2 3 5 8 13
attr L     / `s
/ Binary search for membership and lookups — O(log n)
/ Used: columns in ascending order, e.g. time, for fast range queries

/ Unique `u#
ids: `u# `AAPL`GOOG`MSFT
attr ids   / `u
/ Hash table — fast lookup by value
/ Used: sym column in instrument reference/master tables

/ Parted `p#
syms: `p# `AAPL`AAPL`GOOG`GOOG`MSFT
attr syms  / `p
/ Block index — maps each value to its start position
/ Used: sym column in HDB date partitions after EOD sort

/ Grouped `g#
syms: `g# `AAPL`GOOG`AAPL`MSFT`GOOG
attr syms  / `g
/ Inverted index — maps each value to all its row positions
/ Used: sym column in the RDB for fast intraday lookups

Explanation

Sorted `s# — requires the list to be in ascending order. kdb+ performs binary search (O(log n)) instead of linear scan (O(n)) for membership tests and range lookups. Applying s# to an unsorted list throws an error. The attribute is lost if you append a value smaller than the current maximum.

Primary use cases: columns that are naturally in ascending order, such as a time column of data arriving in time order, so that range queries (where time within ...) use binary search; and step-function dictionaries. (asc applies it automatically.)

Unique `u# — maintains a hash table mapping each value to its row position, so lookups do not scan the list. The attribute is kept on append as long as the new value is unique; appending a duplicate does not signal an error but silently removes the attribute.

Primary use case: key columns in reference/master tables where each value appears exactly once — instrument identifiers, currency codes, exchange codes.

Parted `p# — requires that all occurrences of each distinct value are grouped together (contiguous). kdb+ builds a block index: for each distinct value, the index records its starting row position. Lookups find the block and read only those rows.

Primary use case: the sym column in HDB date partitions. After the EOD sort (xasc by sym), all AAPL rows are together, all GOOG rows are together, etc. Applying p# enables block reads for sym-filtered queries, avoiding a full partition scan.

Grouped `g# — maintains an inverted index: a dictionary mapping each distinct value to the list of all its row positions. No structural constraint on the data — values can appear in any order, any number of times. The index is updated on every append.

Primary use case: the sym column in the RDB. Market data arrives in unpredictable symbol order throughout the day; g# provides fast symbol lookups without requiring the data to be sorted.