Attributes · Performance Benchmarks · Practice Question
What Is `\ts` and Why Is Workspace Important When Profiling Queries?
Question
What is the difference between \t and \ts in kdb+? Why is workspace a useful metric when profiling query performance?
Solution
trade: ([] sym:10000000?`$string til 5000; price:10000000?100.0)
/ \t: elapsed time only (milliseconds)
\t select from trade where sym=`42
/ \ts: elapsed time and workspace (milliseconds; bytes)
\ts select from trade where sym=`42
/ e.g. 30 16777904 — 30ms, and about 16.8MB of workspace
/ Useful pattern: compare before and after adding an attribute
update `g#sym from `trade
\ts select from trade where sym=`42
/ e.g. 0 2400 — a fraction of a millisecond, and a few KBThe exact numbers depend on your machine.
Explanation
\t expression returns the elapsed time in milliseconds. \ts expression returns two numbers: the elapsed time in milliseconds and the memory, in bytes, used to evaluate the expression.
Why workspace matters:
Elapsed time alone can be misleading. A query that takes 5ms but allocates 500MB of workspace will:
- Cause memory pressure if run concurrently by multiple clients
- Exhaust available workspace (
wsfullerror) if the database is large or the query is part of a loop
Workspace reveals what the query is doing internally. The benchmarks from KX's columnar database white paper illustrate this clearly:
| Scenario | Time | Workspace |
|---|---|---|
In-memory sym filter, no g#, 100M rows | 467ms | 1.2GB |
In-memory sym filter, with g#, 100M rows | 20ms | 8.9MB |
HDB, date first then sym, no p# | 78ms | 75.5MB |
HDB, date first then sym, with p# | 0ms | 445KB |
The workspace numbers reveal why each case is slow: without attributes, kdb+ allocates memory proportional to the number of rows scanned. With the right attribute, kdb+ reads only the relevant rows and allocates almost nothing.
Workspace and the buddy allocator:
Workspace grows in powers of two because kdb+ uses a buddy memory allocator. If you see workspace values like 1KB, 2KB, 4KB, 8KB... stepping up, this is the allocator rounding up to the next power of two. Very large workspace values (hundreds of MB, gigabytes) with simple-looking queries are a reliable signal that an attribute is missing.