Skip to content
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

[Copy] developer guide en edition #8724

Merged
merged 23 commits into from
May 26, 2022
Prev Previous commit
Next Next commit
[Debug] dead link
  • Loading branch information
Icemap committed May 25, 2022
commit 67ea8e70addc433436106905129f8c61e25ba5de
2 changes: 1 addition & 1 deletion develop/dev-guide-choose-driver-or-orm.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,4 @@ In addition, you need to specify the TiDB dialect in your [Hibernate configurati

> **Note:**
>
> If you are unable to upgrade your `Hibernate` version, use the MySQL 5.7 dialect `org.hibernate.dialect.MySQL57Dialect` instead. However, this setting might cause unpredictable results and the absence of some TiDB-specific features, such as [sequences](/common/sql-statements/sql-statement-create-sequence.md).
> If you are unable to upgrade your `Hibernate` version, use the MySQL 5.7 dialect `org.hibernate.dialect.MySQL57Dialect` instead. However, this setting might cause unpredictable results and the absence of some TiDB-specific features, such as [sequences](/sql-statements/sql-statement-create-sequence.md).
6 changes: 3 additions & 3 deletions develop/dev-guide-create-database.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ For example, to create a database named `bookshop` if it does not exist, use the
CREATE DATABASE IF NOT EXISTS `bookshop`;
```

For more information and examples of the `CREATE DATABASE` statement, see the [`CREATE DATABASE`](/common/sql-statements/sql-statement-create-database.md) document.
For more information and examples of the `CREATE DATABASE` statement, see the [`CREATE DATABASE`](/sql-statements/sql-statement-create-database.md) document.

To execute the library build statement as the `root` user, run the following command:

Expand All @@ -47,7 +47,7 @@ mysql

## View databases

To view the databases in a cluster, use the [`SHOW DATABASES`](/common/sql-statements/sql-statement-show-databases.md) statement.
To view the databases in a cluster, use the [`SHOW DATABASES`](/sql-statements/sql-statement-show-databases.md) statement.

For example:

Expand Down Expand Up @@ -79,7 +79,7 @@ The following is an example output:
## Rules in database creation

- Follow the [Database Naming Conventions](/develop/dev-guide-object-naming-guidelines.md) and name your database meaningfully.
- TiDB comes with a default database named `test`. However, it is not recommended that you use it in a production environment if you do not have to. You can create your own database using the `CREATE DATABASE` statement and change the current database using the [`USE {databasename};`](/common/sql-statements/sql-statement-use.md) statement in a SQL session.
- TiDB comes with a default database named `test`. However, it is not recommended that you use it in a production environment if you do not have to. You can create your own database using the `CREATE DATABASE` statement and change the current database using the [`USE {databasename};`](/sql-statements/sql-statement-use.md) statement in a SQL session.
- Use the `root` user to create objects such as database, roles, and users. Grant only the necessary privileges to roles and users.
- As a best practice, it is recommended that you use a **MySQL command-line client** or a **MySQL GUI client** instead of a driver or ORM to execute database schema changes.

Expand Down
8 changes: 4 additions & 4 deletions develop/dev-guide-create-secondary-indexes.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ In TiDB, you can either [add a secondary index to an existing table](#add-a-seco

## Add a secondary index to an existing table

To add a secondary index to an existing table, you can use the [CREATE INDEX](/common/sql-statements/sql-statement-create-index.md) statement as follows:
To add a secondary index to an existing table, you can use the [CREATE INDEX](/sql-statements/sql-statement-create-index.md) statement as follows:

{{< copyable "sql" >}}

Expand All @@ -40,7 +40,7 @@ Parameter description:

## Create a secondary index when creating a new table

To create a secondary index at the same time as table creation, you can add a clause containing the `KEY` keyword to the end of the [CREATE TABLE](/common/sql-statements/sql-statement-create-table.md) statement:
To create a secondary index at the same time as table creation, you can add a clause containing the `KEY` keyword to the end of the [CREATE TABLE](/sql-statements/sql-statement-create-table.md) statement:

{{< copyable "sql" >}}

Expand Down Expand Up @@ -96,7 +96,7 @@ To support the searching by year feature, you need to write a SQL statement to *
SELECT * FROM `bookshop`.`books` WHERE `published_at` >= '2022-01-01 00:00:00' AND `published_at` < '2023-01-01 00:00:00';
```

To check the execution plan of the SQL statement, you can use the [`EXPLAIN`](/common/sql-statements/sql-statement-explain.md) statement.
To check the execution plan of the SQL statement, you can use the [`EXPLAIN`](/sql-statements/sql-statement-explain.md) statement.

{{< copyable "sql" >}}

Expand Down Expand Up @@ -152,7 +152,7 @@ In the output, **IndexRangeScan** is displayed instead of **TableFullScan**, whi
>
> TiDB also supports explicit use of indexes when querying, and you can use [Optimizer Hints](/optimizer-hints.md) or [SQL Plan Management (SPM)](/sql-plan-management.md) to artificially control the use of indexes. But if you do not know well about indexes, optimizer hints, or SPM, **DO NOT** use this feature to avoid any unexpected results.

To query the indexes on a table, you can use the [SHOW INDEXES](/common/sql-statements/sql-statement-show-indexes.md) statement:
To query the indexes on a table, you can use the [SHOW INDEXES](/sql-statements/sql-statement-show-indexes.md) statement:

{{< copyable "sql" >}}

Expand Down
8 changes: 4 additions & 4 deletions develop/dev-guide-create-table.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ A [primary key](/constraints.md#primary-key) is a column or a set of columns in

The **primary key** is defined in the `CREATE TABLE` statement. The [primary key constraint](/constraints.md#primary-key) requires that all constrained columns contain only non-NULL values.

A table can be created without a **primary key** or with a non-integer **primary key**. In this case, TiDB creates a `_tidb_rowid` as an **implicit primary key**. The implicit primary key `_tidb_rowid`, because of its monotonically increasing nature, might cause write hotspots in write-intensive scenarios. Therefore, if your application is write-intensive, consider sharding data using the [`SHARD_ROW_ID_BITS`](/shard-row-id-bits.md) and [`PRE_SPLIT_REGIONS`](/common/sql-statements/sql-statement-split-region.md#pre_split_regions) parameters. However, this might lead to read amplification, so you need to make your own trade-off.
A table can be created without a **primary key** or with a non-integer **primary key**. In this case, TiDB creates a `_tidb_rowid` as an **implicit primary key**. The implicit primary key `_tidb_rowid`, because of its monotonically increasing nature, might cause write hotspots in write-intensive scenarios. Therefore, if your application is write-intensive, consider sharding data using the [`SHARD_ROW_ID_BITS`](/shard-row-id-bits.md) and [`PRE_SPLIT_REGIONS`](/sql-statements/sql-statement-split-region.md#pre_split_regions) parameters. However, this might lead to read amplification, so you need to make your own trade-off.

When the **primary key** of a table is an [integer type](/data-type-numeric.md#integer-types) and `AUTO_INCREMENT` is used, hotspots cannot be avoided by using `SHARD_ROW_ID_BITS`. If you need to avoid hotspots and do not need a continuous and incremental primary key, you can use [`AUTO_RANDOM`](/auto-random.md) instead of `AUTO_INCREMENT` to eliminate row ID continuity.

Expand Down Expand Up @@ -296,7 +296,7 @@ Then you can go on to perform the following query:
SELECT HOUR(`rated_at`), AVG(`score`) FROM `bookshop`.`ratings` GROUP BY HOUR(`rated_at`);
```

You can also execute the [`EXPLAIN ANALYZE`](/common/sql-statements/sql-statement-explain-analyze.md) statement to see whether this statement is using the **TiFlash**:
You can also execute the [`EXPLAIN ANALYZE`](/sql-statements/sql-statement-explain-analyze.md) statement to see whether this statement is using the **TiFlash**:

{{< copyable "sql" >}}

Expand Down Expand Up @@ -337,7 +337,7 @@ mysql
< init.sql
```

To view all tables under the `bookshop` database, use the [`SHOW TABLES`](/common/sql-statements/sql-statement-show-tables.md#show-full-tables) statement.
To view all tables under the `bookshop` database, use the [`SHOW TABLES`](/sql-statements/sql-statement-show-tables.md#show-full-tables) statement.

{{< copyable "sql" >}}

Expand Down Expand Up @@ -387,7 +387,7 @@ This section provides guidelines you need to follow when creating a table.
- It is not recommended to use complex data types.
- For the fields to be joined, ensure that the data types are consistent and avoid implicit conversion.
- Avoid defining **primary keys** on a single monotonic data column. If you use a single monotonic data column (for example, a column with the `AUTO_INCREMENT` attribute) to define the **primary key**, it might impact the write performance. If possible, use `AUTO_RANDOM` instead of `AUTO_INCREMENT`, which discards the continuous and incremental attribute of the primary key.
- If you really need to create an index on a single monotonic data column at write-intensive scenarios, instead of defining this monotonic data column as the **primary key**, you can use `AUTO_RANDOM` to create the **primary key** for that table, or use [`SHARD_ROW_ID_BITS`](/shard-row-id-bits.md) and [`PRE_SPLIT_REGIONS`](/common/sql-statements/sql-statement-split-region.md#pre_split_regions) to shard `_tidb_rowid`.
- If you really need to create an index on a single monotonic data column at write-intensive scenarios, instead of defining this monotonic data column as the **primary key**, you can use `AUTO_RANDOM` to create the **primary key** for that table, or use [`SHARD_ROW_ID_BITS`](/shard-row-id-bits.md) and [`PRE_SPLIT_REGIONS`](/sql-statements/sql-statement-split-region.md#pre_split_regions) to shard `_tidb_rowid`.

### Guidelines to follow when selecting clustered index

Expand Down
6 changes: 3 additions & 3 deletions develop/dev-guide-delete-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ summary: Learn about the SQL syntax, best practices, and examples for deleting d

# Delete Data

This document describes how to use the [DELETE](/common/sql-statements/sql-statement-delete.md) SQL statement to delete the data in TiDB.
This document describes how to use the [DELETE](/sql-statements/sql-statement-delete.md) SQL statement to delete the data in TiDB.

## Before you start

Expand All @@ -30,15 +30,15 @@ DELETE FROM {table} WHERE {filter}
| `{table}` | Table name |
| `{filter}` | Matching conditions of the filter|

This example only shows a simple use case of `DELETE`. For detailed information, see [DELETE syntax](/common/sql-statements/sql-statement-delete.md).
This example only shows a simple use case of `DELETE`. For detailed information, see [DELETE syntax](/sql-statements/sql-statement-delete.md).

## Best practices

The following are some best practices to follow when you delete data:

- Always specify the `WHERE` clause in the `DELETE` statement. If the `WHERE` clause is not specified, TiDB will delete **_ALL ROWS_** in the table.
- Use [bulk-delete](#bulk-delete) when you delete a large number of rows (for example, more than ten thousand), because TiDB limits the size of a single transaction ([txn-total-size-limit](/tidb-configuration-file.md#txn-total-size-limit), 100 MB by default).
- If you delete all the data in a table, do not use the `DELETE` statement. Instead, use the [`TRUNCATE`](/common/sql-statements/sql-statement-truncate.md) statement.
- If you delete all the data in a table, do not use the `DELETE` statement. Instead, use the [`TRUNCATE`](/sql-statements/sql-statement-truncate.md) statement.
- For performance considerations, see [Performance Considerations](#performance-considerations).

## Example
Expand Down
2 changes: 1 addition & 1 deletion develop/dev-guide-index-best-practice.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ CREATE TABLE `books` (

> **Warning:**
>
> Currently, expression index is an experimental feature, and it needs to be enabled in the TiDB configuration file. For more details, see [expression index](/common/sql-statements/sql-statement-create-index.md#expression-index).
> Currently, expression index is an experimental feature, and it needs to be enabled in the TiDB configuration file. For more details, see [expression index](/sql-statements/sql-statement-create-index.md#expression-index).

- Try to use a covering index, in which the columns in the index contain the columns to be queried, and avoid querying all columns with `SELECT *` statements.

Expand Down
2 changes: 1 addition & 1 deletion develop/dev-guide-overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Before you start working with TiDB, you need to understand some important mechan

TiDB supports distributed transactions and offers both [optimistic transaction](/optimistic-transaction.md) and [pessimistic transaction](/pessimistic-transaction.md) modes. The current version of TiDB uses the **pessimistic transaction** mode by default, which allows you to transact with TiDB as you would with a traditional monolithic database (for example, MySQL).

You can start a transaction using [`BEGIN`](/common/sql-statements/sql-statement-begin.md), explicitly specify a **pessimistic transaction** using `BEGIN PESSIMISTIC`, or explicitly specify an **optimistic transaction** using `BEGIN OPTIMISTIC`. After that, you can either commit ([`COMMIT`](/common/sql-statements/sql-statement-commit.md)) or roll back ([`ROLLBACK`](/common/sql-statements/sql-statement-rollback.md)) the transaction.
You can start a transaction using [`BEGIN`](/sql-statements/sql-statement-begin.md), explicitly specify a **pessimistic transaction** using `BEGIN PESSIMISTIC`, or explicitly specify an **optimistic transaction** using `BEGIN OPTIMISTIC`. After that, you can either commit ([`COMMIT`](/sql-statements/sql-statement-commit.md)) or roll back ([`ROLLBACK`](/sql-statements/sql-statement-rollback.md)) the transaction.

TiDB guarantees atomicity for all statements between the start of `BEGIN` and the end of `COMMIT` or `ROLLBACK`, that is, all statements that are executed during this period either succeed or fail as a whole. This is used to ensure data consistency you need for application development.

Expand Down
10 changes: 5 additions & 5 deletions develop/dev-guide-prepared-statement.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ summary: Learn about how to use the TiDB prepared statements.

# Prepared Statements

A [prepared statement](/common/sql-statements/sql-statement-prepare.md) templatizes multiple SQL statements in which only parameters are different. It separates the SQL statements from the parameters. You can use it to improve the following aspects of SQL statements:
A [prepared statement](/sql-statements/sql-statement-prepare.md) templatizes multiple SQL statements in which only parameters are different. It separates the SQL statements from the parameters. You can use it to improve the following aspects of SQL statements:

- **Security**: Because parameters and statements are separated, the risk of [SQL injection](https://en.wikipedia.org/wiki/SQL_injection) attacks is avoided.
- **Performance**: Because the statement is parsed in advance on the TiDB server, only parameters are passed for subsequent executions, saving the cost of parsing the entire SQL statements, splicing SQL statement strings, and network transmission.
Expand All @@ -29,11 +29,11 @@ PREPARE {prepared_statement_name} FROM '{prepared_statement_sql}';
| `{prepared_statement_name}` | name of the prepared statement|
| `{prepared_statement_sql}` | the prepared statement SQL with a question mark as a placeholder |

See [PREPARE statement](/common/sql-statements/sql-statement-prepare.md) for more information.
See [PREPARE statement](/sql-statements/sql-statement-prepare.md) for more information.

### Use the prepared statement

A prepared statement can only use **user variables** as parameters, so use the [`SET` statement](/common/sql-statements/sql-statement-set-variable.md) to set the variables before the [`EXECUTE` statement](/common/sql-statements/sql-statement-execute.md) can call the prepared statement.
A prepared statement can only use **user variables** as parameters, so use the [`SET` statement](/sql-statements/sql-statement-set-variable.md) to set the variables before the [`EXECUTE` statement](/sql-statements/sql-statement-execute.md) can call the prepared statement.

{{< copyable "sql" >}}

Expand All @@ -48,7 +48,7 @@ EXECUTE {prepared_statement_name} USING @{parameter_name};
| `{parameter_value}` | user variable value |
| `{prepared_statement_name}` | The name of the preprocessing statement, which must be the same as the name defined in the [Create a prepared statement](#create-a-prepared-statement) |

See the [`EXECUTE` statement](/common/sql-statements/sql-statement-execute.md) for more information.
See the [`EXECUTE` statement](/sql-statements/sql-statement-execute.md) for more information.

### Delete the prepared statement

Expand All @@ -62,7 +62,7 @@ DEALLOCATE PREPARE {prepared_statement_name};
| :-------------------------: | :-------------------------------------------------------------------: |
| `{prepared_statement_name}` | The name of the preprocessing statement, which must be the same as the name defined in the [Create a prepared statement](#create-a-prepared-statement) |

See the [`DEALLOCATE` statement](/common/sql-statements/sql-statement-deallocate.md) for more information.
See the [`DEALLOCATE` statement](/sql-statements/sql-statement-deallocate.md) for more information.

## Examples

Expand Down
6 changes: 3 additions & 3 deletions develop/dev-guide-sample-application-spring-boot.md
Original file line number Diff line number Diff line change
Expand Up @@ -820,12 +820,12 @@ The entity class has several annotations that give Hibernate additional informat
- `@Table` relates this entity class to the `player_jpa` table using the annotation attribute `name`.
- `@Id` declares that this property is related to the primary key column of the table.
- `@GeneratedValue` indicates that the value of this column is generated automatically and should not be set manually. The attribute `generator` is used to specify the name of the generator as `player_id`.
- `@SequenceGenerator` declares a generator that uses [sequence](/common/sql-statements/sql-statement-create-sequence.md), and uses the annotation attribute `name` to declare the name of the generator as `player_id` (consistent with the name specified in `@GeneratedValue`). The annotation attribute `sequenceName` is used to specify the name of the sequence in the database. Finally, the annotation attribute `allocationSize` is used to declare the sequence's step size to be 1.
- `@SequenceGenerator` declares a generator that uses [sequence](/sql-statements/sql-statement-create-sequence.md), and uses the annotation attribute `name` to declare the name of the generator as `player_id` (consistent with the name specified in `@GeneratedValue`). The annotation attribute `sequenceName` is used to specify the name of the sequence in the database. Finally, the annotation attribute `allocationSize` is used to declare the sequence's step size to be 1.
- `@Column` declares each private attribute as a column of the `player_jpa` table, and uses the annotation attribute `name` to determine the name of the column corresponding to the attribute.

#### Repository

To abstract the database layer, Spring applications use the [`Repository`](https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories) interface, or a sub-interface of the `Repository`. This interface maps to a database object, such as a table. JPA implements some pre-built methods, such as [`INSERT`](/common/sql-statements/sql-statement-insert.md), or [`SELECT`](/common/sql-statements/sql-statement-select.md) using the primay key.
To abstract the database layer, Spring applications use the [`Repository`](https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories) interface, or a sub-interface of the `Repository`. This interface maps to a database object, such as a table. JPA implements some pre-built methods, such as [`INSERT`](/sql-statements/sql-statement-insert.md), or [`SELECT`](/sql-statements/sql-statement-select.md) using the primay key.

{{< copyable "" >}}

Expand Down Expand Up @@ -888,7 +888,7 @@ In `getPlayerAndLock`, an annotation [`@Lock`](https://docs.spring.io/spring-dat
@Query(value = "SELECT * FROM player_jpa WHERE id = :id FOR UPDATE", nativeQuery = true)
```

The SQL statement above uses `FOR UPDATE` to add locks directly. You can also dive deeper into the principles with the TiDB [`SELECT` statement](/common/sql-statements/sql-statement-select.md).
The SQL statement above uses `FOR UPDATE` to add locks directly. You can also dive deeper into the principles with the TiDB [`SELECT` statement](/sql-statements/sql-statement-select.md).

### Logic implementation

Expand Down
Loading