AS OF / SNAPSHOT¶
Description¶
The AS OF and SNAPSHOT clauses reconstruct the state of a table as of a specific past date or instant. Both keywords are syntactically and semantically identical in MemoraDB.
Syntax¶
(See the Syntax Notation Guide for notation rules).
Basic syntax¶
Full syntax¶
[!NOTE] Flexible Clause Ordering: In
SELECTstatements, the temporal clause andWHEREclause can be written in either order:
Date-Only vs. Timestamp Semantics¶
A critical detail in MemoraDB's temporal engine is how date literals are interpreted:
1. Date-Only Literals (YYYY-MM-DD)¶
When you provide a bare date without a time portion (e.g., 2026-09-20), the engine interprets this as the end of that whole day (23:59:59.999).
* This ensures that any records inserted or updated at any point during that calendar day are included in the query result.
2. Explicit Timestamp Literals (YYYY-MM-DD HH[:MM[:SS[.mmm]]])¶
When you provide an explicit time (e.g., 2026-09-20 00:00:00.000), the engine evaluates the snapshot at that exact millisecond instant.
-- Evaluates state at the END of September 20 (23:59:59.999)
SELECT * FROM employees AS OF 2026-09-20;
-- Evaluates state at the EXACT START of September 20 (00:00:00.000)
SELECT * FROM employees AS OF 2026-09-20 00:00:00.000;
How It Works¶
- Date Parsing: The date string is parsed into a 64-bit epoch timestamp in milliseconds (
Executor::dayEndMs). - Anchor Version Lookup:
- For each primary key in the table, the engine calls
HistoryIndex::latestBefore(pk, targetMs). - Binary search locates the newest version created on or before
targetMs. - Tombstone Filtering:
- If the anchor version has
deleted == 1, the row is treated as non-existent at that point in time. - If
deleted == 0, the record payload is read from disk atversion.offset. - WHERE & Projection: Any
WHEREconditions,ORDER BY,LIMIT, and column projections are applied to the snapshot records.