From 3141d05231fec6954351f0c38f63cebfacef51d4 Mon Sep 17 00:00:00 2001 From: Morgan Tocker Date: Fri, 29 Mar 2019 12:05:43 -0600 Subject: [PATCH] sql: move optimizer hints to separate file (#997) * Update tidb-specific.md * Create optimizer-hints.md * add back accidentally dropped * Add hints to TOC --- TOC.md | 1 + sql/optimizer-hints.md | 35 +++++++++++++++++++++++++++++++++++ sql/tidb-specific.md | 30 ------------------------------ 3 files changed, 36 insertions(+), 30 deletions(-) create mode 100644 sql/optimizer-hints.md diff --git a/TOC.md b/TOC.md index 5fff2a82bdb58..17a1d419c52ff 100644 --- a/TOC.md +++ b/TOC.md @@ -28,6 +28,7 @@ - [SQL Optimization Process](sql/sql-optimizer-overview.md) - [Understand the Query Execution Plan](sql/understanding-the-query-execution-plan.md) - [Introduction to Statistics](sql/statistics.md) + - [Optimizer Hints](sql/optimizer-hints.md) + Language Structure - [Literal Values](sql/literal-values.md) - [Schema Object Names](sql/schema-object-names.md) diff --git a/sql/optimizer-hints.md b/sql/optimizer-hints.md new file mode 100644 index 0000000000000..e73078bd28d1b --- /dev/null +++ b/sql/optimizer-hints.md @@ -0,0 +1,35 @@ +--- +title: Optimizer Hints +summary: Use Optimizer Hints to influence query execution plans +category: user guide +--- + +# Optimizer Hints + +TiDB supports optimizer hints, based on the comment-like syntax introduced in MySQL 5.7. i.e. `/*+ TIDB_XX(t1, t2) */`. Use of optimizer hints is recommended in cases where the TiDB optimizer selects a less optimal query plan. + +> **Note:** MySQL command-line clients earlier than 5.7.7 strip optimizer hints by default. If you want to use the `Hint` syntax in these earlier versions, add the `--comments` option when starting the client. For example: `mysql -h 127.0.0.1 -P 4000 -uroot --comments`. + +### TIDB_SMJ(t1, t2) + +```sql +SELECT /*+ TIDB_SMJ(t1, t2) */ * from t1, t2 where t1.id = t2.id +``` + +This variable is used to remind the optimizer to use the `Sort Merge Join` algorithm. This algorithm takes up less memory, but takes longer to execute. It is recommended if the data size is too large, or there’s insufficient system memory. + +### TIDB_INLJ(t1, t2) + +```sql +SELECT /*+ TIDB_INLJ(t1, t2) */ * from t1, t2 where t1.id = t2.id +``` + +This variable is used to remind the optimizer to use the `Index Nested Loop Join` algorithm. In some scenarios, this algorithm runs faster and takes up fewer system resources, but may be slower and takes up more system resources in some other scenarios. You can try to use this algorithm in scenarios where the result-set is less than 10,000 rows after the outer table is filtered by the WHERE condition. The parameter in `TIDB_INLJ()` is the candidate table for the inner table when you create the query plan. For example, `TIDB_INLJ (t1)` means that TiDB only considers using t1 as the inner table to create a query plan. + +### TIDB_HJ(t1, t2) + +```sql +SELECT /*+ TIDB_HJ(t1, t2) */ * from t1, t2 where t1.id = t2.id +``` + +This variable is used to remind the optimizer to use the `Hash Join` algorithm. This algorithm executes threads concurrently. It runs faster but takes up more memory. diff --git a/sql/tidb-specific.md b/sql/tidb-specific.md index fd5b0e87460b6..a9d9d12167d3c 100644 --- a/sql/tidb-specific.md +++ b/sql/tidb-specific.md @@ -297,36 +297,6 @@ set @@global.tidb_distsql_scan_concurrency = 10 - This variable is used to change the default priority for statements executed on a TiDB server. A use case is to ensure that a particular user that is performing OLAP queries receives lower priority than users performing OLTP queries. - You can set the value of this variable to `NO_PRIORITY`, `LOW_PRIORITY`, `DELAYED` or `HIGH_PRIORITY`. -## Optimizer Hints - -TiDB supports optimizer hints, based on the comment-like syntax introduced in MySQL 5.7. i.e. `/*+ TIDB_XX(t1, t2) */`. Use of optimizer hints is recommended in cases where the TiDB optimizer selects a less optimal query plan. - -> **Note:** MySQL command-line clients earlier than 5.7.7 strip optimizer hints by default. If you want to use the `Hint` syntax in these earlier versions, add the `--comments` option when starting the client. For example: `mysql -h 127.0.0.1 -P 4000 -uroot --comments`. - -### TIDB_SMJ(t1, t2) - -```sql -SELECT /*+ TIDB_SMJ(t1, t2) */ * from t1, t2 where t1.id = t2.id -``` - -This variable is used to remind the optimizer to use the `Sort Merge Join` algorithm. This algorithm takes up less memory, but takes longer to execute. It is recommended if the data size is too large, or there’s insufficient system memory. - -### TIDB_INLJ(t1, t2) - -```sql -SELECT /*+ TIDB_INLJ(t1, t2) */ * from t1, t2 where t1.id = t2.id -``` - -This variable is used to remind the optimizer to use the `Index Nested Loop Join` algorithm. In some scenarios, this algorithm runs faster and takes up fewer system resources, but may be slower and takes up more system resources in some other scenarios. You can try to use this algorithm in scenarios where the result-set is less than 10,000 rows after the outer table is filtered by the WHERE condition. The parameter in `TIDB_INLJ()` is the candidate table for the inner table when you create the query plan. For example, `TIDB_INLJ (t1)` means that TiDB only considers using t1 as the inner table to create a query plan. - -### TIDB_HJ(t1, t2) - -```sql -SELECT /*+ TIDB_HJ(t1, t2) */ * from t1, t2 where t1.id = t2.id -``` - -This variable is used to remind the optimizer to use the `Hash Join` algorithm. This algorithm executes threads concurrently. It runs faster but takes up more memory. - ## SHARD_ROW_ID_BITS For the tables with non-integer PK or without PK, TiDB uses an implicit auto-increment ROW ID. When a large number of `INSERT` operations occur, the data is written into a single Region, causing a write hot spot.