-
Notifications
You must be signed in to change notification settings - Fork 711
add document about stale read transaction #5809
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c6f9181
add stale read docs
CharLotteiu c2722ce
Apply suggestions from code review
CharLotteiu ffc2f0f
Apply suggestions from code review
CharLotteiu 167871e
Update read-historical-data.md
CharLotteiu ccd3b05
fix typo
CharLotteiu 6797554
Apply suggestions from code review
CharLotteiu e9eb8c4
Update TOC.md
TomShawn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,261 @@ | ||
| --- | ||
| title: Read Historical Data Using the `AS OF TIMESTAMP` Clause | ||
| summary: Learn how to read historical data using the `AS OF TIMESTAMP` statement clause. | ||
| --- | ||
|
|
||
| # Read Historical Data Using the `AS OF TIMESTAMP` Clause | ||
|
|
||
| This document describes how to perform the [Stale Read](/stale-read.md) feature using the `AS OF TIMESTAMP` clause to read historical data in TiDB, including specific usage examples and strategies for saving historical data. | ||
|
|
||
| TiDB supports reading historical data through a standard SQL interface, which is the `AS OF TIMESTAMP` SQL clause, without the need for special clients or drivers. After data is updated or deleted, you can read the historical data before the update or deletion using this SQL interface. | ||
|
|
||
| > **Note:** | ||
| > | ||
| > When reading historical data, TiDB returns the data with the old table structure even if the current table structure is different. | ||
|
|
||
| ## Syntax | ||
|
|
||
| You can use the `AS OF TIMESTAMP` clause in the following three ways: | ||
|
|
||
| - [`SELECT ... FROM ... AS OF TIMESTAMP`](/sql-statements/sql-statement-select.md) | ||
| - [`START TRANSACTION READ ONLY AS OF TIMESTAMP`](/sql-statements/sql-statement-start-transaction.md) | ||
| - [`SET TRANSACTION READ ONLY AS OF TIMESTAMP`](/sql-statements/sql-statement-set-transaction.md) | ||
|
|
||
| If you want to specify an exact point of time, you can set a datetime value or use a time function in the `AS OF TIMESTAMP` clause. The format of datetime is like "2016-10-08 16:45:26.999", with millisecond as the minimum time unit, but for most of the time, the time unit of second is enough for specifying a datetime, such as "2016-10-08 16:45:26". You can also get the current time to the millisecond using the `NOW(3)` function. If you want to read the data of several seconds ago, it is **recommended** to use an expression such as `NOW() - INTERVAL 10 SECOND`. | ||
|
|
||
| If you want to specify a time range, you can use the `TIDB_BOUNDED_STALENESS()` function in the clause. When this function is used, TiDB selects a suitable timestamp within the specified time range. "Suitable" means there are no transactions that start before this timestamp and have not been committed on the accessed replica, that is, TiDB can perform read operations on the accessed replica and the read operations are not blocked. You need to use `TIDB_BOUNDED_STALENESS(t1, t2)` to call this function. `t1` and `t2` are the two ends of the time range, which can be specified using either datetime values or time functions. | ||
|
|
||
| Here are some examples of the `AS OF TIMESTAMP` clause: | ||
|
|
||
| - `AS OF TIMESTAMP '2016-10-08 16:45:26'`: Tells TiDB to read the latest data stored at 16:45:26 on October 8, 2016. | ||
| - `AS OF TIMESTAMP NOW() - INTERVAL 10 SECOND`: Tells TiDB to read the latest data stored 10 seconds ago. | ||
| - `AS OF TIMESTAMP TIDB_BOUNDED_STALENESS('2016-10-08 16:45:26', '2016-10-08 16:45:29')`: Tells TiDB to read the data as new as possible within the time range of 16:45:26 to 16:45:29 on October 8, 2016. | ||
| - `AS OF TIMESTAMP TIDB_BOUNDED_STALENESS(NOW() - INTERVAL 20 SECOND, NOW())`: Tells TiDB to read the data as new as possible within the time range of 20 seconds ago to the present. | ||
|
|
||
| Note that in addition to specifying a timestamp, the most common use of the `AS OF TIMESTAMP` clause is to read data that is several seconds old. If this approach is used, it is recommended to read historical data older than 5 seconds. | ||
|
|
||
| ## Usage examples | ||
|
|
||
| This section describes different ways to use the `AS OF TIMESTAMP` clause with several examples. It first introduces how to prepare the data for recovery, and then shows how to use `AS OF TIMESTAMP` in `SELECT`, `START TRANSACTION READ ONLY AS OF TIMESTAMP`, and `SET TRANSACTION READ ONLY AS OF TIMESTAMP` respectively. | ||
|
|
||
| ### Prepare data sample | ||
|
|
||
| To prepare data for recovery, create a table first and insert several rows of data: | ||
|
|
||
| ```sql | ||
| create table t (c int); | ||
| ``` | ||
|
|
||
| ``` | ||
| Query OK, 0 rows affected (0.01 sec) | ||
| ``` | ||
|
|
||
| ```sql | ||
| insert into t values (1), (2), (3); | ||
| ``` | ||
|
|
||
| ``` | ||
| Query OK, 3 rows affected (0.00 sec) | ||
| ``` | ||
|
|
||
| View the data in the table: | ||
|
|
||
| ```sql | ||
| select * from t; | ||
| ``` | ||
|
|
||
| ``` | ||
| +------+ | ||
| | c | | ||
| +------+ | ||
| | 1 | | ||
| | 2 | | ||
| | 3 | | ||
| +------+ | ||
| 3 rows in set (0.00 sec) | ||
| ``` | ||
|
|
||
| View the current time: | ||
|
|
||
| ```sql | ||
| select now(); | ||
| ``` | ||
|
|
||
| ``` | ||
| +---------------------+ | ||
| | now() | | ||
| +---------------------+ | ||
| | 2021-05-26 16:45:26 | | ||
| +---------------------+ | ||
| 1 row in set (0.00 sec) | ||
| ``` | ||
|
|
||
| Update the data in a row: | ||
|
|
||
| ```sql | ||
| update t set c=22 where c=2; | ||
| ``` | ||
|
|
||
| ``` | ||
| Query OK, 1 row affected (0.00 sec) | ||
| ``` | ||
|
|
||
| Confirm that the data of the row is updated: | ||
|
|
||
| ```sql | ||
| select * from t; | ||
| ``` | ||
|
|
||
| ``` | ||
| +------+ | ||
| | c | | ||
| +------+ | ||
| | 1 | | ||
| | 22 | | ||
| | 3 | | ||
| +------+ | ||
| 3 rows in set (0.00 sec) | ||
| ``` | ||
|
|
||
| ### Read historical data using the `SELECT` statement | ||
|
|
||
| You can use the [`SELECT ... FROM ... AS OF TIMESTAMP`](/sql-statements/sql-statement-select.md) statement to read data from a time point in the past. | ||
|
|
||
| ```sql | ||
| select * from t as of timestamp '2021-05-26 16:45:26'; | ||
| ``` | ||
|
|
||
| ``` | ||
| +------+ | ||
| | c | | ||
| +------+ | ||
| | 1 | | ||
| | 2 | | ||
| | 3 | | ||
| +------+ | ||
| 3 rows in set (0.00 sec) | ||
| ``` | ||
|
|
||
| > **Note:** | ||
| > | ||
| > When reading multiple tables using one `SELECT` statement, you need to make sure that the format of TIMESTAMP EXPRESSIONs is consistent. For example, `select * from t as of timestamp NOW() - INTERVAL 2 SECOND, c as of timestamp NOW() - INTERVAL 2 SECOND;`. In addition, you must specify the `AS OF` information for the relevant table in the `SELECT` statement; otherwise, the `SELECT` statement reads the latest data by default. | ||
|
|
||
| ### Read historical data using the `START TRANSACTION READ ONLY AS OF TIMESTAMP` statement | ||
|
|
||
| You can use the [`START TRANSACTION READ ONLY AS OF TIMESTAMP`](/sql-statements/sql-statement-start-transaction.md) statement to start a read-only transaction based on a time point in the past. The transaction reads historical data of the given time. | ||
|
|
||
| ```sql | ||
| start transaction read only as of timestamp '2021-05-26 16:45:26'; | ||
| ``` | ||
|
|
||
| ``` | ||
| Query OK, 0 rows affected (0.00 sec) | ||
| ``` | ||
|
|
||
| ```sql | ||
| select * from t; | ||
| ``` | ||
|
|
||
| ``` | ||
| +------+ | ||
| | c | | ||
| +------+ | ||
| | 1 | | ||
| | 2 | | ||
| | 3 | | ||
| +------+ | ||
| 3 rows in set (0.00 sec) | ||
| ``` | ||
|
|
||
| ```sql | ||
| commit; | ||
| ``` | ||
|
|
||
| ``` | ||
| Query OK, 0 rows affected (0.00 sec) | ||
| ``` | ||
|
|
||
| After the transaction is committed, you can read the latest data. | ||
|
|
||
| ```sql | ||
| select * from t; | ||
| ``` | ||
|
|
||
| ``` | ||
| +------+ | ||
| | c | | ||
| +------+ | ||
| | 1 | | ||
| | 22 | | ||
| | 3 | | ||
| +------+ | ||
| 3 rows in set (0.00 sec) | ||
| ``` | ||
|
|
||
| > **Note:** | ||
| > | ||
| > If you start a transaction with the statement `START TRANSACTION READ ONLY AS OF TIMESTAMP`, it is a read-only transaction. Write operations are rejected in this transaction. | ||
|
|
||
| ### Read historical data using the `SET TRANSACTION READ ONLY AS OF TIMESTAMP` statement | ||
|
|
||
| You can use the [`SET TRANSACTION READ ONLY AS OF TIMESTAMP`](/sql-statements/sql-statement-set-transaction.md) statement to set the next transaction as a read-only transaction based on a specified time point in the past. The transaction reads historical data of the given time. | ||
|
|
||
| ```sql | ||
| set transaction read only as of timestamp '2021-05-26 16:45:26'; | ||
| ``` | ||
|
|
||
| ``` | ||
| Query OK, 0 rows affected (0.00 sec) | ||
| ``` | ||
|
|
||
| ```sql | ||
| begin; | ||
| ``` | ||
|
|
||
| ``` | ||
| Query OK, 0 rows affected (0.00 sec) | ||
| ``` | ||
|
|
||
| ```sql | ||
| select * from t; | ||
| ``` | ||
|
|
||
| ``` | ||
| +------+ | ||
| | c | | ||
| +------+ | ||
| | 1 | | ||
| | 2 | | ||
| | 3 | | ||
| +------+ | ||
| 3 rows in set (0.00 sec) | ||
| ``` | ||
|
|
||
| ```sql | ||
| commit; | ||
| ``` | ||
|
|
||
| ``` | ||
| Query OK, 0 rows affected (0.00 sec) | ||
| ``` | ||
|
|
||
| After the transaction is committed, you can read the latest data. | ||
|
|
||
| ```sql | ||
| select * from t; | ||
| ``` | ||
|
|
||
| ``` | ||
| +------+ | ||
| | c | | ||
| +------+ | ||
| | 1 | | ||
| | 22 | | ||
| | 3 | | ||
| +------+ | ||
| 3 rows in set (0.00 sec) | ||
| ``` | ||
|
|
||
| > **Note:** | ||
| > | ||
| > If you start a transaction with the statement `SET TRANSACTION READ ONLY AS OF TIMESTAMP`, it is a read-only transaction. Write operations are rejected in this transaction. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| --- | ||
| title: Local Read under Three Data Centers Deployment | ||
| summary: Learn how to use the Stale Read feature to read local data under three DCs deployment and thus reduce cross-center requests. | ||
| --- | ||
|
|
||
| # Local Read under Three Data Centers Deployment | ||
|
|
||
| In the model of three data centers, a Region has three replicas which are isolated in each data center. However, due to the requirement of strongly consistent read, TiDB must access the Leader replica of the corresponding data for every query. If the query is generated in a data center different from that of the Leader replica, TiDB needs to read data from another data center, thus causing the access latency to increase. | ||
|
|
||
| This document describes how to use the [Stale Read](/stale-read.md) feature to avoid cross-center access and reduce the access latency at the expense of real-time data availability. | ||
|
|
||
| ## Deploy a TiDB cluster of three data centers | ||
|
|
||
| For the three-data-center deployment method, refer to [Multiple Data Centers in One City Deployment](/multi-data-centers-in-one-city-deployment.md). | ||
|
|
||
| Note that if both the TiKV and TiDB nodes have the configuration item `labels` configured, the TiKV and TiDB nodes in the same data center must have the same value for the `zone` label. For example, if a TiKV node and a TiDB node are both in the data center `dc-1`, then the two nodes need to be configured with the following label: | ||
|
|
||
| ``` | ||
| [labels] | ||
| zone=dc-1 | ||
| ``` | ||
|
|
||
| ## Perform local read using Stale Read | ||
|
|
||
| [Stale Read](/stale-read.md) is a mechanism that TiDB provides for the users to read historical data. Using this mechanism, you can read the corresponding historical data of a specific point in time or within a specified time range, and thus save the latency brought by data replication between storage nodes. When using Stale Read in some scenarios of geo-distributed deployment, TiDB accesses the replica in the current data center to read the corresponding data at the expense of some real-time performance, which avoids network latency brought by cross-center connection and reduces the access latency for the entire query process. | ||
|
|
||
| When TiDB receives a Stale Read query, if the `zone` label of that TiDB node is configured, then TiDB sends the request to the TiKV node with the same `zone` label where the corresponding data replica resides. | ||
|
|
||
| For how to perform Stale Read, see [Perform Stale Read using the `AS OF TIMESTAMP` clause](/as-of-timestamp.md). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.