diff --git a/basic-features.md b/basic-features.md index ef0fbcc0adcc4..4583c860cf667 100644 --- a/basic-features.md +++ b/basic-features.md @@ -92,7 +92,7 @@ This document lists the features supported in each TiDB version. Note that suppo | [Auto increment](/auto-increment.md) | Y | Y | Y | Y | Y | Y | Y | Y | | [Auto random](/auto-random.md) | Y | Y | Y | Y | Y | Y | Y | Y | | [DDL algorithm assertions](/sql-statements/sql-statement-alter-table.md) | Y | Y | Y | Y | Y | Y | Y | Y | -| [Multi-schema change: add columns](/system-variables.md#tidb_enable_change_multi_schema) | Experimental | Experimental | Experimental | Experimental | Experimental | Experimental | Experimental | Experimental | +| Multi-schema change: add columns | Experimental | Experimental | Experimental | Experimental | Experimental | Experimental | Experimental | Experimental | | [Change column type](/sql-statements/sql-statement-modify-column.md) | Y | Y | Y | Y | Y | Y | N | N | | [Temporary tables](/temporary-tables.md) | Y | Y | Y | Y | N | N | N | N | diff --git a/develop/dev-guide-use-common-table-expression.md b/develop/dev-guide-use-common-table-expression.md index da7304ef13b35..dd28f9eb4ea7c 100644 --- a/develop/dev-guide-use-common-table-expression.md +++ b/develop/dev-guide-use-common-table-expression.md @@ -172,6 +172,10 @@ First, check out the books written by the author (ID is `2299112019`) in the CTE Note that the query in `books_authored_by_rm` executes only once, and then TiDB creates a temporary space to cache its result. When the queries in `books_with_average_ratings` and `books_with_orders` refer to `books_authored_by_rm`, TiDB gets its result directly from this temporary space. +> **Tip:** +> +> If the efficiency of the default CTE queries is not good, you can use the [`MERGE()`](/optimizer-hints.md#merge) hint to expand the CTE subquery to the outer query to improve the efficiency. + ### Recursive CTE Recursive CTE can be defined using the following syntax: diff --git a/optimizer-hints.md b/optimizer-hints.md index 2f0ffcb5523bc..6b42daa777d07 100644 --- a/optimizer-hints.md +++ b/optimizer-hints.md @@ -294,6 +294,27 @@ SHOW WARNINGS; +---------+------+-------------------------------------------------------------------------------------------------------------------+ ``` +### MERGE() + +Using the `MERGE()` hint in queries with common table expressions (CTE) can disable the materialization of the subqueries and expand the subquery inlines into CTE. This hint is only applicable to non-recursive CTE. In some scenarios, using `MERGE()` brings higher execution efficiency than the default behavior of allocating a temporary space. For example, pushing down query conditions or in nesting CTE queries: + +```sql +-- Uses the hint to push down the predicate of the outer query. +WITH CTE AS (SELECT /*+ MERGE() */ * FROM tc WHERE tc.a < 60) SELECT * FROM CTE WHERE CTE.a < 18; + +-- Uses the hint in a nested CTE query to expand a CTE inline into the outer query. +WITH CTE1 AS (SELECT * FROM t1), CTE2 AS (WITH CTE3 AS (SELECT /*+ MERGE() */ * FROM t2), CTE4 AS (SELECT * FROM t3) SELECT * FROM CTE3, CTE4) SELECT * FROM CTE1, CTE2; +``` + +> **Note:** +> +> `MERGE()` is only applicable to simple CTE queries. It is not applicable in the following situations: +> +> - [Recursive CTE](/develop/dev-guide-use-common-table-expression.md#recursive-cte) +> - Subqueries with inlines that cannot be expanded, such as aggregate operators, window functions, and `DISTINCT`. +> +> When the number of CTE references is too high, the query performance might be lower than the default materialization behavior. + ## Hints that take effect in the whole query This category of hints can only follow behind the **first** `SELECT`, `UPDATE` or `DELETE` keyword, which is equivalent to modifying the value of the specified system variable when this query is executed. The priority of the hint is higher than that of existing system variables.