Querying History
Dolt databases allow you to query the data at any point in the commit history. There are several ways to do so.
Please note: when querying history, the unit of snapshot is the dolt commit. SQL transaction commits do not create a dolt commit by default.
Querying past snapshots with AS OF
AS OF
Dolt SQL supports a variant of SQL 2011 syntax to query non-HEAD revisions of a database via the AS OF
clause:
The AS OF
expression must name a valid Dolt reference, such as a commit hash, branch name, or other reference. Timestamp / date values are also supported. Each table in a query can use a different AS OF
clause.
In addition to this AS OF
syntax for SELECT
statements, Dolt also supports various extensions to the standard MySQL syntax to examine the schemas of snapshots:
Note that AS OF
always names a revision at a specific Dolt commit. Changes on a branch's working set that have not been committed to that head via call dolt_commit()
or similar are not visible via this syntax.
Specifying a revision in the database name
You can connect to any commit in the database history by including its commit hash in the name of the database, like this:
mysql://127.0.0.1:3306/mydb/ia1ibijq8hq1llr7u85uivsi5lh3310p
The database will be read-only in this case. You can do the same thing on an existing connection with a USE
statement.
Or specify the commit hash directly in the query. This is equivalent to AS OF
, but works in some queries where the AS OF
syntax is not supported.
There are other variations on this as well. See the docs on using branches for more details.
Note that this syntax applied to a branch will name that branch's working set and therefore includes any changes not yet committed to the HEAD of the branch.
Querying history using dolt system tables
For every table in the database, dolt also provides a set of system tables that you can query to see past values of rows, diffs between revisions, and more.
the dolt_history
tables provide a row for every revision of a row in a table.
To query how rows changed between two commits, use the dolt_commit_diff
and dolt_diff
tables.
For more information, see the system table docs.
Querying historical view data
Database views are an edge case for historical queries. When you have a database view whose definition has changed, querying it with AS OF
will use the current definition of the view, but use rows from the tables as they existed at the revision provided.
To query the historical definition of a view, you must checkout the database at a particular commit. You can do this by changing your branch, e.g.:
You can also do this without changing your session's branch by using a commit hash-qualified database identifier when referencing the view.
Consider this example:
Last updated