Blog - June 13, 2025

How to get near-O(1) lookups on partitioned timestamp data in databases without scanning the whole table or using indexes

By Chris Muktar

Like most analytics platforms, Userbird partitions data by date.

Each visitor is assigned a session_id that expires at the end of the day—effectively tying every session to a single partition.

We wanted to build a Session Explorer: a tool to let users inspect individual sessions by session_id. But this raises a key question:

If data is partitioned by date, how can we retrieve a session without scanning every partition?

No more full table scans to pull out an ID on time partitioned data

The Solution: Compound Keys

Inspired by DynamoDB best practices, we embedded the partition key (the date) directly into the session_id. This turns the ID itself into a compound key, pointing to both the partition and the specific record.

For example:

20250611f200a47071395f12ce64d206

The first 8 characters (20250611) represent the date. This has two benefits:

  1. It tells us exactly which partition to scan.
  2. It makes session IDs sortable by date, which is useful for time-based queries.

With this structure, fetching a session becomes efficient: we can directly query the relevant partition using the embedded date, with no full-table scan required.

Ready to Try Privacy-First Analytics?

See how Userbird can give you powerful insights without compromising user privacy. No cookies, no tracking consent needed, fully GDPR compliant.

Subscribe to our Newsletter

Chris Muktar cartoon avatar
Chris Muktar

Founder of Userbird.

Chris has been running digital marketing and software businesses since 2007, and is previously a founder of Linkly, a global URL shortener.

Table of Contents