diff --git a/sql-statements/sql-statement-show-create-table.md b/sql-statements/sql-statement-show-create-table.md index b6a34109cf69..fe59d54e5c75 100644 --- a/sql-statements/sql-statement-show-create-table.md +++ b/sql-statements/sql-statement-show-create-table.md @@ -10,13 +10,10 @@ aliases: ['/docs-cn/dev/sql-statements/sql-statement-show-create-table/','/docs- ## 语法图 -**ShowCreateTableStmt:** - -![ShowCreateTableStmt](/media/sqlgram/ShowCreateTableStmt.png) - -**TableName:** - -![TableName](/media/sqlgram/TableName.png) +```ebnf+diagram +ShowCreateTableStmt ::= + "SHOW" "CREATE" "TABLE" (SchemaName ".")? TableName +``` ## 示例 @@ -33,17 +30,15 @@ Query OK, 0 rows affected (0.12 sec) {{< copyable "sql" >}} ```sql -SHOW CREATE TABLE t1; +SHOW CREATE TABLE t1\G ``` ``` -+-------+------------------------------------------------------------------------------------------------------------+ -| Table | Create Table | -+-------+------------------------------------------------------------------------------------------------------------+ -| t1 | CREATE TABLE `t1` ( +*************************** 1. row *************************** + Table: t1 +Create Table: CREATE TABLE `t1` ( `a` int(11) DEFAULT NULL -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin | -+-------+------------------------------------------------------------------------------------------------------------+ +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin 1 row in set (0.00 sec) ``` diff --git a/sql-statements/sql-statement-show-indexes.md b/sql-statements/sql-statement-show-indexes.md index 15e37ac51ed8..5edd3d8fcf0a 100644 --- a/sql-statements/sql-statement-show-indexes.md +++ b/sql-statements/sql-statement-show-indexes.md @@ -10,25 +10,14 @@ aliases: ['/docs-cn/dev/sql-statements/sql-statement-show-indexes/','/docs-cn/de ## 语法图 -**ShowIndexStmt:** +```ebnf+diagram +ShowIndexStmt ::= + "SHOW" ( "INDEX" | "INDEXES" | "KEYS" ) ("FROM" | "IN" ) TableName (("FROM" | "IN") SchemaName )? ShowLikeOrWhere? -![ShowIndexStmt](/media/sqlgram/ShowIndexStmt.png) - -**ShowIndexKwd:** - -![ShowIndexKwd](/media/sqlgram/ShowIndexKwd.png) - -**FromOrIn:** - -![FromOrIn](/media/sqlgram/FromOrIn.png) - -**TableName:** - -![TableName](/media/sqlgram/TableName.png) - -**ShowLikeOrWhereOpt:** - -![ShowLikeOrWhereOpt](/media/sqlgram/ShowLikeOrWhereOpt.png) +ShowLikeOrWhere ::= + "LIKE" SimpleExpr +| "WHERE" Expression +``` ## 示例 @@ -99,3 +88,7 @@ SHOW KEYS FROM t1; * [SHOW CREATE TABLE](/sql-statements/sql-statement-show-create-table.md) * [DROP INDEX](/sql-statements/sql-statement-drop-index.md) * [CREATE INDEX](/sql-statements/sql-statement-create-index.md) +* [`information_schema.TIDB_INDEXES`](/information-schema/information-schema-tidb-indexes.md) +* [`information_schema.TIDB_INDEX_USAGE`](/information-schema/information-schema-tidb-index-usage.md) +* [`information_schema.KEY_COLUMN_USAGE`](/information-schema/information-schema-key-column-usage.md) +* [`sys.schema_unused_indexes`](/sys-schema/sys-schema-unused-indexes.md) diff --git a/sql-statements/sql-statement-show-table-next-rowid.md b/sql-statements/sql-statement-show-table-next-rowid.md index 117e9b89d5f3..c8a2cef0e034 100644 --- a/sql-statements/sql-statement-show-table-next-rowid.md +++ b/sql-statements/sql-statement-show-table-next-rowid.md @@ -8,20 +8,17 @@ aliases: ['/docs-cn/dev/sql-statements/sql-statement-show-table-next-rowid/'] `SHOW TABLE NEXT_ROW_ID` 语句用于显示用户表中某些特殊列的详情,主要包含以下几种类型: -* TiDB 创建的 `AUTO_INCREMENT` 类型列,即 `_tidb_rowid` 列 +* TiDB 创建的 [`AUTO_INCREMENT`](/auto-increment.md) 类型列,即 `_tidb_rowid` 列 * 用户创建的 `AUTO_INCREMENT` 类型列 * 用户创建的 [`AUTO_RANDOM`](/auto-random.md) 类型列 * 用户创建的 [`SEQUENCE`](/sql-statements/sql-statement-create-sequence.md) 对象信息 ## 语法图 -**ShowTableNextRowIDStmt:** - -![ShowTableNextRowIDStmt](/media/sqlgram/ShowTableNextRowIDStmt.png) - -**TableName:** - -![TableName](/media/sqlgram/TableName.png) +```ebnf+diagram +ShowTableNextRowIDStmt ::= + "SHOW" "TABLE" (SchemaName ".")? TableName "NEXT_ROW_ID" +``` ## 示例 @@ -30,12 +27,12 @@ aliases: ['/docs-cn/dev/sql-statements/sql-statement-show-table-next-rowid/'] {{< copyable "sql" >}} ```sql -create table t(a int); +CREATE TABLE t(a int); Query OK, 0 rows affected (0.06 sec) ``` ```sql -show table t next_row_id; +SHOW TABLE t NEXT_ROW_ID; +---------+------------+-------------+--------------------+ | DB_NAME | TABLE_NAME | COLUMN_NAME | NEXT_GLOBAL_ROW_ID | +---------+------------+-------------+--------------------+ @@ -44,16 +41,18 @@ show table t next_row_id; 1 row in set (0.00 sec) ``` -下面示例中,表中写入了数据,负责写入的 TiDB Server 一次性向存储层请求了 30000 个 ID 缓存起来,NEXT_GLOBAL_ROW_ID 值为 `30001`。 +下面示例中,表中写入了数据,负责写入的 TiDB Server 一次性向存储层请求了 30000 个 ID 缓存起来,NEXT_GLOBAL_ROW_ID 值为 `30001`。ID 的数量由 [`AUTO_ID_CACHE`](/auto-increment.md#auto_id_cache) 控制。 + +{{< copyable "sql" >}} ```sql -insert into t values (), (), (); +INSERT INTO t VALUES (), (), (); Query OK, 3 rows affected (0.02 sec) Records: 3 Duplicates: 0 Warnings: 0 ``` ```sql -show table t next_row_id; +SHOW TABLE t NEXT_ROW_ID; +---------+------------+-------------+--------------------+ | DB_NAME | TABLE_NAME | COLUMN_NAME | NEXT_GLOBAL_ROW_ID | +---------+------------+-------------+--------------------+ diff --git a/sql-statements/sql-statement-show-table-regions.md b/sql-statements/sql-statement-show-table-regions.md index 932e9e14c636..0acc0856f702 100644 --- a/sql-statements/sql-statement-show-table-regions.md +++ b/sql-statements/sql-statement-show-table-regions.md @@ -18,25 +18,13 @@ SHOW TABLE [table_name] INDEX [index_name] REGIONS [WhereClauseOptional]; ### 语法图 -**ShowTableRegionStmt:** +```ebnf+diagram +ShowTableRegionStmt ::= + "SHOW" "TABLE" TableName PartitionNameList? ("INDEX" IndexName)? "REGIONS" ("WHERE" Expression)? -![ShowTableRegionStmt](/media/sqlgram/ShowTableRegionStmt.png) - -**TableName:** - -![TableName](/media/sqlgram/TableName.png) - -**PartitionNameListOpt:** - -![PartitionNameListOpt](/media/sqlgram/PartitionNameListOpt.png) - -**WhereClauseOptional:** - -![WhereClauseOptional](/media/sqlgram/WhereClauseOptional.png) - -**WhereClause:** - -![WhereClause](/media/sqlgram/WhereClause.png) +TableName ::= + (SchemaName ".")? Identifier +``` `SHOW TABLE REGIONS` 会返回如下列: @@ -152,7 +140,7 @@ SHOW TABLE t1 REGIONS; {{< copyable "sql" >}} ```sql -show table t regions; +SHOW TABLE t REGIONS; ``` ```sql @@ -179,7 +167,7 @@ show table t regions; {{< copyable "sql" >}} ```sql -show table t regions where leader_store_id =1; +SHOW TABLE t REGIONS WHERE leader_store_id =1; ``` ``` @@ -195,7 +183,7 @@ show table t regions where leader_store_id =1; {{< copyable "sql" >}} ```sql -split table t index name between ("a") and ("z") regions 2; +SPLIT TABLE t INDEX name BETWEEN ("a") AND ("z") REGIONS 2; ``` ``` @@ -212,7 +200,7 @@ split table t index name between ("a") and ("z") regions 2; {{< copyable "sql" >}} ```sql -show table t regions; +SHOW TABLE t REGIONS; ``` ``` diff --git a/sql-statements/sql-statement-split-region.md b/sql-statements/sql-statement-split-region.md index c71782e2cdff..c1a6b87a0786 100644 --- a/sql-statements/sql-statement-split-region.md +++ b/sql-statements/sql-statement-split-region.md @@ -14,33 +14,26 @@ summary: TiDB 中的 Split Region 功能可以解决表数据超过默认 Region ## 语法图 -**SplitRegionStmt:** +```ebnf+diagram +SplitRegionStmt ::= + "SPLIT" SplitSyntaxOption "TABLE" TableName PartitionNameList? ("INDEX" IndexName)? SplitOption -![SplitRegionStmt](/media/sqlgram/SplitRegionStmt.png) +SplitSyntaxOption ::= + ("REGION" "FOR")? "PARTITION"? -**SplitSyntaxOption:** +TableName ::= + (SchemaName ".")? Identifier -![SplitSyntaxOption](/media/sqlgram/SplitSyntaxOption.png) +PartitionNameList ::= + "PARTITION" "(" PartitionName ("," PartitionName)* ")" -**TableName:** +SplitOption ::= + ("BETWEEN" RowValue "AND" RowValue "REGIONS" NUM +| "BY" RowValue ("," RowValue)* ) -![TableName](/media/sqlgram/TableName.png) - -**PartitionNameListOpt:** - -![PartitionNameListOpt](/media/sqlgram/PartitionNameListOpt.png) - -**SplitOption:** - -![SplitOption](/media/sqlgram/SplitOption.png) - -**RowValue:** - -![RowValue](/media/sqlgram/RowValue.png) - -**Int64Num:** - -![Int64Num](/media/sqlgram/Int64Num.png) +RowValue ::= + "(" ValuesOpt ")" +``` ## Split Region 的使用 @@ -78,7 +71,7 @@ Split Region 有 2 种不同的语法,具体如下: > **注意:** > > 以下会话变量会影响 `SPLIT` 语句的行为,需要特别注意: -> +> > * `tidb_wait_split_region_finish`:打散 Region 的时间可能较长,由 PD 调度以及 TiKV 的负载情况所决定。这个变量用来设置在执行 `SPLIT REGION` 语句时,是否同步等待所有 Region 都打散完成后再返回结果给客户端。默认 `1` 代表等待打散完成后再返回结果。`0` 代表不等待 Region 打散完成就返回结果。 > * `tidb_wait_split_region_timeout`:这个变量用来设置 `SPLIT REGION` 语句的执行超时时间,单位是秒,默认值是 300 秒,如果超时还未完成 `Split` 操作,就返回一个超时错误。 @@ -104,16 +97,12 @@ t22_r11 例如,对于表 t,如果想要从 `minInt64`~`maxInt64` 之间均匀切割出 16 个 Region,可以用以下语句: -{{< copyable "sql" >}} - ```sql SPLIT TABLE t BETWEEN (-9223372036854775808) AND (9223372036854775807) REGIONS 16; ``` 该语句会把表 t 从 minInt64 到 maxInt64 之间均匀切割出 16 个 Region。如果已知主键的范围没有这么大,比如只会在 0~1000000000 之间,那可以用 0 和 1000000000 分别代替上面的 minInt64 和 maxInt64 来切分 Region。 -{{< copyable "sql" >}} - ```sql SPLIT TABLE t BETWEEN (0) AND (1000000000) REGIONS 16; ``` @@ -122,8 +111,6 @@ SPLIT TABLE t BETWEEN (0) AND (1000000000) REGIONS 16; 如果已知数据不是均匀分布的,比如想要 -inf ~ 10000 切一个 Region,10000 ~ 90000 切一个 Region,90000 ~ +inf 切一个 Region,可以通过手动指定点来切分 Region,示例如下: -{{< copyable "sql" >}} - ```sql SPLIT TABLE t BY (10000), (90000); ``` @@ -152,8 +139,6 @@ t22_i5abc 如果索引 idx 的列也是整数类型,可以用如下 SQL 语句切分索引数据: -{{< copyable "sql" >}} - ```sql SPLIT TABLE t INDEX idx BETWEEN (-9223372036854775808) AND (9223372036854775807) REGIONS 16; ``` @@ -162,8 +147,6 @@ SPLIT TABLE t INDEX idx BETWEEN (-9223372036854775808) AND (9223372036854775807) 如果索引 idx1 的列是 varchar 类型,希望根据前缀字母来切分索引数据: -{{< copyable "sql" >}} - ```sql SPLIT TABLE t INDEX idx1 BETWEEN ("a") AND ("z") REGIONS 25; ``` @@ -172,8 +155,6 @@ SPLIT TABLE t INDEX idx1 BETWEEN ("a") AND ("z") REGIONS 25; 上面的切分方法,以 y 和 z 前缀的索引数据都会写到 region 25,因为 `z` 并不是一个上界,真正的上界是 `z` 在 ASCII 码中的下一位 `{`,所以更准确的切分方法如下: -{{< copyable "sql" >}} - ```sql SPLIT TABLE t INDEX idx1 BETWEEN ("a") AND ("{") REGIONS 26; ``` @@ -182,8 +163,6 @@ SPLIT TABLE t INDEX idx1 BETWEEN ("a") AND ("{") REGIONS 26; 如果索引 idx2 的列是 timestamp/datetime 等时间类型,希望根据时间区间,按年为间隔切分索引数据,示例如下: -{{< copyable "sql" >}} - ```sql SPLIT TABLE t INDEX idx2 BETWEEN ("2010-01-01 00:00:00") AND ("2020-01-01 00:00:00") REGIONS 10; ``` @@ -192,8 +171,6 @@ SPLIT TABLE t INDEX idx2 BETWEEN ("2010-01-01 00:00:00") AND ("2020-01-01 00:00: 如果希望按照天为间隔切分索引,示例如下: -{{< copyable "sql" >}} - ```sql SPLIT TABLE t INDEX idx2 BETWEEN ("2020-06-01 00:00:00") AND ("2020-07-01 00:00:00") REGIONS 30; ``` @@ -206,16 +183,12 @@ SPLIT TABLE t INDEX idx2 BETWEEN ("2020-06-01 00:00:00") AND ("2020-07-01 00:00: 比如索引 `idx3 (a, b)` 包含 2 列,a 是 timestamp,b 是 int。如果只想根据 a 列做时间范围的切分,可以用切分单列时间索引的 SQL 语句来切分,`lower_value` 和 `upper_velue` 中不指定 b 列的值即可。 -{{< copyable "sql" >}} - ```sql SPLIT TABLE t INDEX idx3 BETWEEN ("2010-01-01 00:00:00") AND ("2020-01-01 00:00:00") REGIONS 10; ``` 如果想在时间相同的情况下,根据 b 列再做一次切分,在切分时指定 b 列的值即可。 -{{< copyable "sql" >}} - ```sql SPLIT TABLE t INDEX idx3 BETWEEN ("2010-01-01 00:00:00", "a") AND ("2010-01-01 00:00:00", "z") REGIONS 10; ``` @@ -234,8 +207,6 @@ SPLIT TABLE t INDEX `PRIMARY` BETWEEN (-9223372036854775808) AND (92233720368547 假如有 idx4 (a,b),其中 a 列是 varchar 类型,b 列是 timestamp 类型。 -{{< copyable "sql" >}} - ```sql SPLIT TABLE t1 INDEX idx4 BY ("a", "2000-01-01 00:00:01"), ("b", "2019-04-17 14:26:19"), ("c", ""); ``` @@ -255,16 +226,12 @@ region4 [("c", "") , maxIndexValue ) - 均匀切分的语法如下: - {{< copyable "sql" >}} - ```sql SPLIT [PARTITION] TABLE t [PARTITION] [(partition_name_list...)] [INDEX index_name] BETWEEN (lower_value) AND (upper_value) REGIONS region_num ``` - 不均匀切分的语法如下: - {{< copyable "sql" >}} - ```sql SPLIT [PARTITION] TABLE table_name [PARTITION (partition_name_list...)] [INDEX index_name] BY (value_list) [, (value_list)] ... ``` @@ -273,18 +240,14 @@ region4 [("c", "") , maxIndexValue ) 1. 首先创建一个分区表。如果你要建一个 Hash 分区表,分成 2 个 partition,示例语句如下: - {{< copyable "sql" >}} - ```sql - create table t (a int,b int,index idx(a)) partition by hash(a) partitions 2; + CREATE TABLE t (a INT, b INT, INDEX idx(a)) PARTITION BY HASH(a) PARTITIONS 2; ``` - 此时建完表后会为每个 partition 都单独 split 一个 Region,用 `SHOW TABLE REGIONS` 语法查看该表的 Region 如下: - - {{< copyable "sql" >}} + 此时建完表后会为每个 partition 都单独 split 一个 Region,用 [`SHOW TABLE REGIONS`](/sql-statements/sql-statement-show-table-regions.md) 语法查看该表的 Region 如下: ```sql - show table t regions; + SHOW TABLE t REGIONS; ``` ```sql @@ -298,10 +261,8 @@ region4 [("c", "") , maxIndexValue ) 2. 用 `SPLIT` 语法为每个 partition 切分 Region。如果你要将各个 partition 的 [0,10000] 范围内的数据切分成 4 个 Region,示例语句如下: - {{< copyable "sql" >}} - ```sql - split partition table t between (0) and (10000) regions 4; + SPLIT PARTITION TABLE t BETWEEN (0) AND (10000) REGIONS 4; ``` 其中,`0` 和 `10000` 分别代表你想要打散的热点数据对应的上、下边界的 `row_id`。 @@ -312,10 +273,8 @@ region4 [("c", "") , maxIndexValue ) 3. 用 `SHOW TABLE REGIONS` 语法查看该表的 Region。如下会发现该表现在一共有 10 个 Region,每个 partition 分别有 5 个 Region,其中 4 个 Region 是表的行数据,1 个 Region 是表的索引数据。 - {{< copyable "sql" >}} - ```sql - show table t regions; + SHOW TABLE t REGIONS; ``` ```sql @@ -337,10 +296,8 @@ region4 [("c", "") , maxIndexValue ) 4. 如果你要给每个分区的索引切分 Region,如将索引 `idx` 的 [1000,10000] 范围切分成 2 个 Region,示例语句如下: - {{< copyable "sql" >}} - ```sql - split partition table t index idx between (1000) and (10000) regions 2; + SPLIT PARTITION TABLE t INDEX idx BETWEEN (1000) AND (10000) REGIONS 2; ``` #### Split 单个分区的 Region 示例 @@ -349,37 +306,29 @@ region4 [("c", "") , maxIndexValue ) 1. 首先创建一个分区表。如果你要建一个 Range 分区表,分成 3 个 partition,示例语句如下: - {{< copyable "sql" >}} - ```sql - create table t ( a int, b int, index idx(b)) partition by range( a ) ( - partition p1 values less than (10000), - partition p2 values less than (20000), - partition p3 values less than (MAXVALUE) ); + CREATE TABLE t ( a INT, b INT, INDEX idx(b)) PARTITION BY RANGE( a ) ( + PARTITION p1 VALUES LESS THAN (10000), + PARTITION p2 VALUES LESS THAN (20000), + PARTITION p3 VALUES LESS THAN (MAXVALUE) ); ``` 2. 如果你要将 `p1` 分区的 [0,10000] 范围内的数据预切分 2 个 Region,示例语句如下: - {{< copyable "sql" >}} - ```sql - split partition table t partition (p1) between (0) and (10000) regions 2; + SPLIT PARTITION TABLE t PARTITION (p1) BETWEEN (0) AND (10000) REGIONS 2; ``` 3. 如果你要将 `p2` 分区的 [10000,20000] 范围内的数据预切分 2 个 Region,示例语句如下: - {{< copyable "sql" >}} - ```sql - split partition table t partition (p2) between (10000) and (20000) regions 2; + SPLIT PARTITION TABLE t PARTITION (p2) BETWEEN (10000) AND (20000) REGIONS 2; ``` 4. 用 `SHOW TABLE REGIONS` 语法查看该表的 Region 如下: - {{< copyable "sql" >}} - ```sql - show table t regions; + SHOW TABLE t REGIONS; ``` ```sql @@ -396,10 +345,8 @@ region4 [("c", "") , maxIndexValue ) 5. 如果你要将 `p1` 和 `p2` 分区的索引 `idx` 的 [0,20000] 范围预切分 2 个 Region,示例语句如下: - {{< copyable "sql" >}} - ```sql - split partition table t partition (p1,p2) index idx between (0) and (20000) regions 2; + SPLIT PARTITION TABLE t PARTITION (p1,p2) INDEX idx BETWEEN (0) AND (20000) REGIONS 2; ``` ## pre_split_regions @@ -416,10 +363,8 @@ region4 [("c", "") , maxIndexValue ) ### pre_split_regions 示例 -{{< copyable "sql" >}} - ```sql -create table t (a int, b int,index idx1(a)) shard_row_id_bits = 4 pre_split_regions=2; +CREATE TABLE t (a INT, b INT, INDEX idx1(a)) SHARD_ROW_ID_BITS = 4 PRE_SPLIT_REGIONS=2; ``` 该语句在建表后,会对这个表 t 预切分出 4 + 1 个 Region。4 (2^2) 个 Region 是用来存 table 的行数据的,1 个 Region 是用来存 idx1 索引的数据。 diff --git a/sql-statements/sql-statement-update.md b/sql-statements/sql-statement-update.md index 25beb7a61bdc..ab594b4dd610 100644 --- a/sql-statements/sql-statement-update.md +++ b/sql-statements/sql-statement-update.md @@ -10,29 +10,22 @@ aliases: ['/docs-cn/dev/sql-statements/sql-statement-update/','/docs-cn/dev/refe ## 语法图 -**UpdateStmt:** +```ebnf+diagram +UpdateStmt ::= + "UPDATE" UpdateOption +( TableRef "SET" Assignment ("," Assignment)* WhereClause? OrderBy? Limit? +| TableRefs "SET" Assignment ("," Assignment)* WhereClause? +) -![UpdateStmt](/media/sqlgram/UpdateStmt.png) +UpdateOption ::= + OptimizerHints? ("LOW_PRIORITY" | "HIGH_PRIORITY" | "DELAYED")? "IGNORE"? -**PriorityOpt:** +TableRef ::= + ( TableFactor | JoinTable ) -![PriorityOpt](/media/sqlgram/PriorityOpt.png) - -**TableRef:** - -![TableRef](/media/sqlgram/TableRef.png) - -**TableRefs:** - -![TableRefs](/media/sqlgram/TableRefs.png) - -**AssignmentList:** - -![AssignmentList](/media/sqlgram/AssignmentList.png) - -**WhereClauseOptional:** - -![WhereClauseOptional](/media/sqlgram/WhereClauseOptional.png) +TableRefs ::= + EscapedTableRef ("," EscapedTableRef)* +``` ## 示例