SELECT statement is used to read data from TiDB.
Description of the syntax elements
DISTINCTROW modifiers specify whether duplicate rows should be returned. ALL (the default) specifies that all matching rows should be returned.
HIGH_PRIORITY gives the current statement higher priority than other statements.
|To guarantee compatibility with MySQL, TiDB parses this syntax, but will ignore it.|
SQL_NO_CACHE are used to control whether to cache the request results to the
BlockCache of TiKV (RocksDB). For a one-time query on a large amount of data, such as the
count(*) query, it is recommended to fill in
SQL_NO_CACHE to avoid flushing the hot user data in
STRAIGHT_JOIN forces the optimizer to do a union query in the order of the tables used in the
FROM clause. When the optimizer chooses a join order that is not good, you can use this syntax to speed up the execution of the query.
select_expr indicates a column to retrieve. including the column names and expressions.
\* represents all the columns.
FROM table_references clause indicates the table (such as
select * from t;), or tables (such as
select * from t1 join t2;) or even 0 tables (such as
select 1+1 from dual; which is equivalent to
select 1+1;) from which to retrieve rows.
WHERE clause, if given, indicates the condition or conditions that rows must satisfy to be selected. The result contains only the data that meets the condition(s).
GROUP BY statement is used to group the result-set.
HAVING clause and the
WHERE clause are both used to filter the results. The
HAVING clause filters the results of
GROUP BY, while the
WHERE clause filter the results before aggregation.
ORDER BY clause is used to sort the data in ascending or descending order, based on columns, expressions or items in the
LIMIT clause can be used to constrain the number of rows.
LIMIT takes one or two numeric arguments. With one argument, the argument specifies the maximum number of rows to return, the first row to return is the first row of the table by default; with two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return.
SELECT FOR UPDATE clause locks all the data in the result sets to detect concurrent updates from other transactions. Data that match the query conditions but do not exist in the result sets are not read-locked, such as the row data written by other transactions after the current transaction is started. TiDB uses the Optimistic Transaction Model. The transaction conflicts are not detected in the statement execution phase. Therefore, the current transaction does not block other transactions from executing
SELECT FOR UPDATE like other databases such as PostgreSQL. In the committing phase, the rows read by
SELECT FOR UPDATE are committed in two phases, which means they can also join the conflict detection. If write conflicts occur, the commit fails for all transactions that include the
SELECT FOR UPDATE clause. If no conflict is detected, the commit succeeds. And a new version is generated for the locked rows, so that write conflicts can be detected when other uncommitted transactions are being committed later.
LOCK IN SHARE MODE
|To guarantee compatibility, TiDB parses these three modifiers, but will ignore them.|
mysql> CREATE TABLE t1 (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, c1 INT NOT NULL);
Query OK, 0 rows affected (0.11 sec)
mysql> INSERT INTO t1 (c1) VALUES (1),(2),(3),(4),(5);
Query OK, 5 rows affected (0.03 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM t1;
| id | c1 |
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
| 5 | 5 |
5 rows in set (0.00 sec)
This statement is understood to be fully compatible with MySQL. Any compatibility differences should be reported via an issue on GitHub.