From 3f035416b229373e9f1238738e587f07dcafd6c1 Mon Sep 17 00:00:00 2001 From: qw4990 Date: Thu, 22 May 2025 14:46:42 -0700 Subject: [PATCH 01/23] new doc for instance-level plan cache --- sql-prepared-plan-cache.md | 67 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/sql-prepared-plan-cache.md b/sql-prepared-plan-cache.md index 1b85d938d6416..146ec71f56f18 100644 --- a/sql-prepared-plan-cache.md +++ b/sql-prepared-plan-cache.md @@ -17,6 +17,73 @@ When the execution plan cache is enabled, in the first execution every `Prepare` TiDB also supports execution plan caching for some non-`PREPARE` statements, similar to the `Prepare`/`Execute` statements. For more details, refer to [Non-prepared plan cache](/sql-non-prepared-plan-cache.md). +## Session and Instance Level Prepared Plan Cache + +TiDB supports both session-level plan cache and instance-level plan cache. Session Plan Cache maintains a separate cache for each session, while Instance Plan Cache maintains a single shared cache across all sessions in an instance. + +You can use [`tidb_enable_instance_plan_cache`](/system-variables.md#tidb_enable_instance_plan_cache-new-in-v840) to enable or disable instance-level plan cache. When it's `ON`, instance-level plan cache will be used, otherwise session-level plan cache will be used. + +Plans can be shared across different sessions when [`tidb_enable_instance_plan_cache`](/system-variables.md#tidb_enable_instance_plan_cache-new-in-v840) is `ON`: + +```sql +-- Execute the following SQL statements in session 1. +mysql> SET GLOBAL tidb_enable_instance_plan_cache=1; +Query OK, 0 rows affected (0.01 sec) + +mysql> PREPARE st FROM "SELECT a FROM t"; +Query OK, 0 rows affected (0.01 sec) + +mysql> EXECUTE st; +Empty set (0.00 sec) + +-- Execute the following SQL statements in session 2. +mysql> PREPARE st FROM "SELECT a FROM t"; +Query OK, 0 rows affected (0.00 sec) + +mysql> EXECUTE st; +Empty set (0.00 sec) + +mysql> SELECT @@last_plan_from_cache; ++------------------------+ +| @@last_plan_from_cache | ++------------------------+ +| 1 | ++------------------------+ +``` + +After v8.5.0, TiDB introduces two system tables to help you view entries in instance-level plan cache: + +* `information_schema.tidb_plan_cache`: shows entries of instance plan cache in the current TiDB instance. +* `information_schema.cluster_tidb_plan_cache`: shows entries of instance plan cache from all available TiDB instances. + +```sql +mysql> select *, tidb_decode_binary_plan(binary_plan) from information_schema.tidb_plan_cache\G; +*************************** 1. row *************************** + SQL_DIGEST: 3689d7f367e2fdaf53c962c378efdf47799143b9af12f47e13ec247332269eac + SQL_TEXT: select a from t where a Date: Thu, 22 May 2025 14:51:52 -0700 Subject: [PATCH 02/23] new doc for instance-level plan cache --- sql-prepared-plan-cache.md | 47 ++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/sql-prepared-plan-cache.md b/sql-prepared-plan-cache.md index 146ec71f56f18..bb7bf044cc041 100644 --- a/sql-prepared-plan-cache.md +++ b/sql-prepared-plan-cache.md @@ -19,7 +19,7 @@ TiDB also supports execution plan caching for some non-`PREPARE` statements, sim ## Session and Instance Level Prepared Plan Cache -TiDB supports both session-level plan cache and instance-level plan cache. Session Plan Cache maintains a separate cache for each session, while Instance Plan Cache maintains a single shared cache across all sessions in an instance. +TiDB supports both session-level plan cache and instance-level plan cache. Session-level plan cache maintains a separate cache for each session, while instance-level plan cache maintains a single shared cache across all sessions in an instance. You can use [`tidb_enable_instance_plan_cache`](/system-variables.md#tidb_enable_instance_plan_cache-new-in-v840) to enable or disable instance-level plan cache. When it's `ON`, instance-level plan cache will be used, otherwise session-level plan cache will be used. @@ -27,21 +27,24 @@ Plans can be shared across different sessions when [`tidb_enable_instance_plan_c ```sql -- Execute the following SQL statements in session 1. -mysql> SET GLOBAL tidb_enable_instance_plan_cache=1; -Query OK, 0 rows affected (0.01 sec) +mysql> PREPARE st FROM "SELECT a FROM t WHERE a PREPARE st FROM "SELECT a FROM t"; -Query OK, 0 rows affected (0.01 sec) +mysql> SET @a=1; +Query OK, 0 rows affected (0.00 sec) -mysql> EXECUTE st; -Empty set (0.00 sec) +mysql> EXECUTE st USING @a; +Empty set (0.01 sec) -- Execute the following SQL statements in session 2. -mysql> PREPARE st FROM "SELECT a FROM t"; +mysql> PREPARE st FROM "SELECT a FROM t WHERE a EXECUTE st; -Empty set (0.00 sec) +mysql> SET @a=30; +Query OK, 0 rows affected (0.00 sec) + +mysql> EXECUTE st USING @a; +Empty set (0.01 sec) mysql> SELECT @@last_plan_from_cache; +------------------------+ @@ -57,29 +60,29 @@ After v8.5.0, TiDB introduces two system tables to help you view entries in inst * `information_schema.cluster_tidb_plan_cache`: shows entries of instance plan cache from all available TiDB instances. ```sql -mysql> select *, tidb_decode_binary_plan(binary_plan) from information_schema.tidb_plan_cache\G; +mysql> select *, tidb_decode_binary_plan(binary_plan) from information_schema.tidb_plan_cache\G *************************** 1. row *************************** SQL_DIGEST: 3689d7f367e2fdaf53c962c378efdf47799143b9af12f47e13ec247332269eac - SQL_TEXT: select a from t where a Date: Thu, 22 May 2025 14:59:20 -0700 Subject: [PATCH 03/23] new doc for instance-level plan cache --- sql-prepared-plan-cache.md | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/sql-prepared-plan-cache.md b/sql-prepared-plan-cache.md index bb7bf044cc041..e695621a7f76f 100644 --- a/sql-prepared-plan-cache.md +++ b/sql-prepared-plan-cache.md @@ -276,6 +276,20 @@ The following is an example of the **Plan Cache Memory Usage** and **Plan Cache ![grafana_panels](/media/planCache-memoryUsage-planNum-panels.png) + + +Due to memory limit, plan cache might be missed sometimes. You can check the status by viewing the [`Plan Cache Miss OPS` metric](/grafana-tidb-dashboard.md) in the Grafana dashboard. + + + + + +Due to memory limit, plan cache might be missed sometimes. + + + +### Session Level Plan Cache Memory Management + Starting from v7.1.0, you can control the maximum number of plans that can be cached in each session by configuring the system variable [`tidb_session_plan_cache_size`](/system-variables.md#tidb_session_plan_cache_size-new-in-v710). For different environments, the recommended value is as follows and you can adjust it according to the monitoring panels: @@ -299,20 +313,18 @@ When the unused memory of the TiDB server is less than a certain threshold, the You can control the threshold by configuring the system variable `tidb_prepared_plan_cache_memory_guard_ratio`. The threshold is 0.1 by default, which means when the unused memory of the TiDB server is less than 10% of the total memory (90% of the memory is used), the memory protection mechanism is triggered. - - -Due to memory limit, plan cache might be missed sometimes. You can check the status by viewing the [`Plan Cache Miss OPS` metric](/grafana-tidb-dashboard.md) in the Grafana dashboard. - - - - +### Instance Level Plan Cache Memory Management -Due to memory limit, plan cache might be missed sometimes. +Use `tidb_instance_plan_cache_max_size` to set the total memory limitation of Instance Plan Cache. For example, `set global tidb_instance_plan_cache_max_size=200MiB` . - +TiDB purges the Instance Plan Cache periodically, and you can set `tidb_instance_plan_cache_reserved_percentage` to control the amount of memory to purge each time. For example, the default percentage is `0.1`, and if the max size is `200MiB`, then TiDB purges `200*0.1=20MiB` memory each time. ## Clear execution plan cache +> **Note:** +> +> The statement only support clean session-level plan cache, we'll support instance-level plan cache soon. + You can clear execution plan cache by executing the `ADMIN FLUSH [SESSION | INSTANCE] PLAN_CACHE` statement. In this statement, `[SESSION | INSTANCE]` specifies whether the plan cache is cleared for the current session or the whole TiDB instance. If the scope is not specified, the preceding statement applies to the `SESSION` cache by default. From 9682d5e1b2b7010c2c7fed8375bf54b0889b01f9 Mon Sep 17 00:00:00 2001 From: qw4990 Date: Thu, 22 May 2025 15:02:55 -0700 Subject: [PATCH 04/23] new doc for instance-level plan cache --- sql-prepared-plan-cache.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql-prepared-plan-cache.md b/sql-prepared-plan-cache.md index e695621a7f76f..2e5f3a2208fa0 100644 --- a/sql-prepared-plan-cache.md +++ b/sql-prepared-plan-cache.md @@ -323,7 +323,7 @@ TiDB purges the Instance Plan Cache periodically, and you can set `tidb_instance > **Note:** > -> The statement only support clean session-level plan cache, we'll support instance-level plan cache soon. +> The statement only supports cleaning session-level plan cache, we'll support instance-level plan cache soon. You can clear execution plan cache by executing the `ADMIN FLUSH [SESSION | INSTANCE] PLAN_CACHE` statement. From 6d6c7dfe02e29f96f8af04825caeb2e206f12396 Mon Sep 17 00:00:00 2001 From: Yuanjia Zhang Date: Thu, 22 May 2025 15:08:14 -0700 Subject: [PATCH 05/23] Update sql-prepared-plan-cache.md Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --- sql-prepared-plan-cache.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql-prepared-plan-cache.md b/sql-prepared-plan-cache.md index 2e5f3a2208fa0..ea748f6bdac76 100644 --- a/sql-prepared-plan-cache.md +++ b/sql-prepared-plan-cache.md @@ -21,7 +21,7 @@ TiDB also supports execution plan caching for some non-`PREPARE` statements, sim TiDB supports both session-level plan cache and instance-level plan cache. Session-level plan cache maintains a separate cache for each session, while instance-level plan cache maintains a single shared cache across all sessions in an instance. -You can use [`tidb_enable_instance_plan_cache`](/system-variables.md#tidb_enable_instance_plan_cache-new-in-v840) to enable or disable instance-level plan cache. When it's `ON`, instance-level plan cache will be used, otherwise session-level plan cache will be used. +You can use [`tidb_enable_instance_plan_cache`](/system-variables.md#tidb_enable_instance_plan_cache-new-in-v840) to enable or disable instance-level plan cache. When this variable is set to `ON`, instance-level plan cache is used; otherwise, session-level plan cache is used. Plans can be shared across different sessions when [`tidb_enable_instance_plan_cache`](/system-variables.md#tidb_enable_instance_plan_cache-new-in-v840) is `ON`: From ac463b74a6c394a67e714435619fbdabb989ca4d Mon Sep 17 00:00:00 2001 From: Yuanjia Zhang Date: Thu, 22 May 2025 15:08:24 -0700 Subject: [PATCH 06/23] Update sql-prepared-plan-cache.md Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --- sql-prepared-plan-cache.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql-prepared-plan-cache.md b/sql-prepared-plan-cache.md index ea748f6bdac76..c0f58807ab5ee 100644 --- a/sql-prepared-plan-cache.md +++ b/sql-prepared-plan-cache.md @@ -54,7 +54,7 @@ mysql> SELECT @@last_plan_from_cache; +------------------------+ ``` -After v8.5.0, TiDB introduces two system tables to help you view entries in instance-level plan cache: +Starting from v8.5.0, TiDB provides two system tables that allow you to view entries in the instance-level plan cache: * `information_schema.tidb_plan_cache`: shows entries of instance plan cache in the current TiDB instance. * `information_schema.cluster_tidb_plan_cache`: shows entries of instance plan cache from all available TiDB instances. From 1b39aa6b71aba3d9c8048684968a0a07ff2080b2 Mon Sep 17 00:00:00 2001 From: Yuanjia Zhang Date: Thu, 22 May 2025 15:08:36 -0700 Subject: [PATCH 07/23] Update sql-prepared-plan-cache.md Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --- sql-prepared-plan-cache.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql-prepared-plan-cache.md b/sql-prepared-plan-cache.md index c0f58807ab5ee..b5a2a522bc74e 100644 --- a/sql-prepared-plan-cache.md +++ b/sql-prepared-plan-cache.md @@ -278,7 +278,7 @@ The following is an example of the **Plan Cache Memory Usage** and **Plan Cache -Due to memory limit, plan cache might be missed sometimes. You can check the status by viewing the [`Plan Cache Miss OPS` metric](/grafana-tidb-dashboard.md) in the Grafana dashboard. +Due to memory limits, plan cache entries might be missed. You can check this status by viewing the [`Plan Cache Miss OPS` metric](/grafana-tidb-dashboard.md) in the Grafana dashboard. From 90c8b8c183ad69e2b9f2a80598538d31d75d6780 Mon Sep 17 00:00:00 2001 From: Yuanjia Zhang Date: Thu, 22 May 2025 15:08:45 -0700 Subject: [PATCH 08/23] Update sql-prepared-plan-cache.md Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --- sql-prepared-plan-cache.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql-prepared-plan-cache.md b/sql-prepared-plan-cache.md index b5a2a522bc74e..360cd44ea2fed 100644 --- a/sql-prepared-plan-cache.md +++ b/sql-prepared-plan-cache.md @@ -284,7 +284,7 @@ Due to memory limits, plan cache entries might be missed. You can check this sta -Due to memory limit, plan cache might be missed sometimes. +Due to memory limits, plan cache entries might be missed. From 07616369504a7b526e7641ca1e8ab1d9dfeb898f Mon Sep 17 00:00:00 2001 From: Yuanjia Zhang Date: Thu, 22 May 2025 15:08:53 -0700 Subject: [PATCH 09/23] Update sql-prepared-plan-cache.md Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --- sql-prepared-plan-cache.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql-prepared-plan-cache.md b/sql-prepared-plan-cache.md index 360cd44ea2fed..9138b61355019 100644 --- a/sql-prepared-plan-cache.md +++ b/sql-prepared-plan-cache.md @@ -315,7 +315,7 @@ You can control the threshold by configuring the system variable `tidb_prepared_ ### Instance Level Plan Cache Memory Management -Use `tidb_instance_plan_cache_max_size` to set the total memory limitation of Instance Plan Cache. For example, `set global tidb_instance_plan_cache_max_size=200MiB` . +You can use the `tidb_instance_plan_cache_max_size` system variable to set the total memory limit for the Instance Plan Cache. For example, `set global tidb_instance_plan_cache_max_size=200MiB`. TiDB purges the Instance Plan Cache periodically, and you can set `tidb_instance_plan_cache_reserved_percentage` to control the amount of memory to purge each time. For example, the default percentage is `0.1`, and if the max size is `200MiB`, then TiDB purges `200*0.1=20MiB` memory each time. From 7607d843f55bd7584cda2bd467f969edcf4e8cfe Mon Sep 17 00:00:00 2001 From: Yuanjia Zhang Date: Thu, 22 May 2025 15:08:59 -0700 Subject: [PATCH 10/23] Update sql-prepared-plan-cache.md Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --- sql-prepared-plan-cache.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql-prepared-plan-cache.md b/sql-prepared-plan-cache.md index 9138b61355019..ff43bc52f755c 100644 --- a/sql-prepared-plan-cache.md +++ b/sql-prepared-plan-cache.md @@ -317,7 +317,7 @@ You can control the threshold by configuring the system variable `tidb_prepared_ You can use the `tidb_instance_plan_cache_max_size` system variable to set the total memory limit for the Instance Plan Cache. For example, `set global tidb_instance_plan_cache_max_size=200MiB`. -TiDB purges the Instance Plan Cache periodically, and you can set `tidb_instance_plan_cache_reserved_percentage` to control the amount of memory to purge each time. For example, the default percentage is `0.1`, and if the max size is `200MiB`, then TiDB purges `200*0.1=20MiB` memory each time. +TiDB purges the Instance Plan Cache periodically. You can use the `tidb_instance_plan_cache_reserved_percentage` system variable to control the amount of memory purged each time. For example, if the default percentage is `0.1` and the maximum cache size is `200MiB`, TiDB purges `200 * 0.1 = 20MiB` of memory each time. ## Clear execution plan cache From 094bbbc1eca4fa0018a6363d2b5ff47c0fbcdf04 Mon Sep 17 00:00:00 2001 From: Yuanjia Zhang Date: Thu, 22 May 2025 15:09:06 -0700 Subject: [PATCH 11/23] Update sql-prepared-plan-cache.md Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --- sql-prepared-plan-cache.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql-prepared-plan-cache.md b/sql-prepared-plan-cache.md index ff43bc52f755c..350153a19659c 100644 --- a/sql-prepared-plan-cache.md +++ b/sql-prepared-plan-cache.md @@ -323,7 +323,7 @@ TiDB purges the Instance Plan Cache periodically. You can use the `tidb_instance > **Note:** > -> The statement only supports cleaning session-level plan cache, we'll support instance-level plan cache soon. +> The statement currently only supports clearing session-level plan cache. Support for clearing instance-level plan cache will be added in a future release. You can clear execution plan cache by executing the `ADMIN FLUSH [SESSION | INSTANCE] PLAN_CACHE` statement. From c19bccadc1a65673a65a416f5b2a1bd86dce5a3d Mon Sep 17 00:00:00 2001 From: Yuanjia Zhang Date: Thu, 22 May 2025 21:12:30 -0700 Subject: [PATCH 12/23] Update sql-prepared-plan-cache.md Co-authored-by: Aolin --- sql-prepared-plan-cache.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql-prepared-plan-cache.md b/sql-prepared-plan-cache.md index 350153a19659c..038bf9edbd62b 100644 --- a/sql-prepared-plan-cache.md +++ b/sql-prepared-plan-cache.md @@ -21,7 +21,7 @@ TiDB also supports execution plan caching for some non-`PREPARE` statements, sim TiDB supports both session-level plan cache and instance-level plan cache. Session-level plan cache maintains a separate cache for each session, while instance-level plan cache maintains a single shared cache across all sessions in an instance. -You can use [`tidb_enable_instance_plan_cache`](/system-variables.md#tidb_enable_instance_plan_cache-new-in-v840) to enable or disable instance-level plan cache. When this variable is set to `ON`, instance-level plan cache is used; otherwise, session-level plan cache is used. +To enable or disable instance-level plan cache, you can use the [`tidb_enable_instance_plan_cache`](/system-variables.md#tidb_enable_instance_plan_cache-new-in-v840) system variable. When you set this variable to `ON`, instance-level plan cache is used. Otherwise, session-level plan cache is used. Plans can be shared across different sessions when [`tidb_enable_instance_plan_cache`](/system-variables.md#tidb_enable_instance_plan_cache-new-in-v840) is `ON`: From 290c8651e1ea607fc3036338bc39777749b4b69c Mon Sep 17 00:00:00 2001 From: Yuanjia Zhang Date: Thu, 22 May 2025 21:12:36 -0700 Subject: [PATCH 13/23] Update sql-prepared-plan-cache.md Co-authored-by: Aolin --- sql-prepared-plan-cache.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql-prepared-plan-cache.md b/sql-prepared-plan-cache.md index 038bf9edbd62b..5eb078508baff 100644 --- a/sql-prepared-plan-cache.md +++ b/sql-prepared-plan-cache.md @@ -17,7 +17,7 @@ When the execution plan cache is enabled, in the first execution every `Prepare` TiDB also supports execution plan caching for some non-`PREPARE` statements, similar to the `Prepare`/`Execute` statements. For more details, refer to [Non-prepared plan cache](/sql-non-prepared-plan-cache.md). -## Session and Instance Level Prepared Plan Cache +## Session-level and instance-level Prepared Plan Cache TiDB supports both session-level plan cache and instance-level plan cache. Session-level plan cache maintains a separate cache for each session, while instance-level plan cache maintains a single shared cache across all sessions in an instance. From 47a2319fd3e9065f78c959aeaaff7bbb1cc8443f Mon Sep 17 00:00:00 2001 From: Yuanjia Zhang Date: Thu, 22 May 2025 21:12:41 -0700 Subject: [PATCH 14/23] Update sql-prepared-plan-cache.md Co-authored-by: Aolin --- sql-prepared-plan-cache.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql-prepared-plan-cache.md b/sql-prepared-plan-cache.md index 5eb078508baff..c2521316c6c36 100644 --- a/sql-prepared-plan-cache.md +++ b/sql-prepared-plan-cache.md @@ -23,7 +23,7 @@ TiDB supports both session-level plan cache and instance-level plan cache. Sessi To enable or disable instance-level plan cache, you can use the [`tidb_enable_instance_plan_cache`](/system-variables.md#tidb_enable_instance_plan_cache-new-in-v840) system variable. When you set this variable to `ON`, instance-level plan cache is used. Otherwise, session-level plan cache is used. -Plans can be shared across different sessions when [`tidb_enable_instance_plan_cache`](/system-variables.md#tidb_enable_instance_plan_cache-new-in-v840) is `ON`: +When you set `tidb_enable_instance_plan_cache` to `ON`, plans can be shared across different sessions. The following is an example: ```sql -- Execute the following SQL statements in session 1. From 8e4cf506bb594b4bbf4cdb8c4afa3f69f5ee26b7 Mon Sep 17 00:00:00 2001 From: Yuanjia Zhang Date: Thu, 22 May 2025 21:13:05 -0700 Subject: [PATCH 15/23] Update sql-prepared-plan-cache.md Co-authored-by: Aolin --- sql-prepared-plan-cache.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sql-prepared-plan-cache.md b/sql-prepared-plan-cache.md index c2521316c6c36..19e0749399bab 100644 --- a/sql-prepared-plan-cache.md +++ b/sql-prepared-plan-cache.md @@ -57,7 +57,9 @@ mysql> SELECT @@last_plan_from_cache; Starting from v8.5.0, TiDB provides two system tables that allow you to view entries in the instance-level plan cache: * `information_schema.tidb_plan_cache`: shows entries of instance plan cache in the current TiDB instance. -* `information_schema.cluster_tidb_plan_cache`: shows entries of instance plan cache from all available TiDB instances. +* `information_schema.cluster_tidb_plan_cache`: shows entries of instance plan cache across all available TiDB instances. + +The following example shows how to view entries in `information_schema.tidb_plan_cache`: ```sql mysql> select *, tidb_decode_binary_plan(binary_plan) from information_schema.tidb_plan_cache\G From b7bf00d2a16e2ed45582c1bbdfb2a2f8ad6d1e47 Mon Sep 17 00:00:00 2001 From: Yuanjia Zhang Date: Thu, 22 May 2025 21:13:12 -0700 Subject: [PATCH 16/23] Update sql-prepared-plan-cache.md Co-authored-by: Aolin --- sql-prepared-plan-cache.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql-prepared-plan-cache.md b/sql-prepared-plan-cache.md index 19e0749399bab..857dfc64970f8 100644 --- a/sql-prepared-plan-cache.md +++ b/sql-prepared-plan-cache.md @@ -62,7 +62,7 @@ Starting from v8.5.0, TiDB provides two system tables that allow you to view ent The following example shows how to view entries in `information_schema.tidb_plan_cache`: ```sql -mysql> select *, tidb_decode_binary_plan(binary_plan) from information_schema.tidb_plan_cache\G +mysql> SELECT *, tidb_decode_binary_plan(binary_plan) FROM information_schema.tidb_plan_cache\G *************************** 1. row *************************** SQL_DIGEST: 3689d7f367e2fdaf53c962c378efdf47799143b9af12f47e13ec247332269eac SQL_TEXT: SELECT a FROM t WHERE a Date: Thu, 22 May 2025 21:13:58 -0700 Subject: [PATCH 17/23] Update sql-prepared-plan-cache.md Co-authored-by: Aolin --- sql-prepared-plan-cache.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql-prepared-plan-cache.md b/sql-prepared-plan-cache.md index 857dfc64970f8..61e183bd8284e 100644 --- a/sql-prepared-plan-cache.md +++ b/sql-prepared-plan-cache.md @@ -87,7 +87,7 @@ tidb_decode_binary_plan(binary_plan): | └─TableFullScan_5 | 10000.00 | 4070000.00 | cop[tikv] | table:t | keep order:false, stats:pseudo | ``` -## Prepared Plan Cache Restrictions +## Prepared Plan Cache restrictions In the current version of TiDB, if a `Prepare` statement meets any of the following conditions, the query or the plan is not cached: From 3e3713c87b4deffbfe92ae4e5e7fdc0d145a23ac Mon Sep 17 00:00:00 2001 From: Yuanjia Zhang Date: Thu, 22 May 2025 21:42:25 -0700 Subject: [PATCH 18/23] Update sql-prepared-plan-cache.md Co-authored-by: Aolin --- sql-prepared-plan-cache.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql-prepared-plan-cache.md b/sql-prepared-plan-cache.md index 61e183bd8284e..6b686dc463df9 100644 --- a/sql-prepared-plan-cache.md +++ b/sql-prepared-plan-cache.md @@ -54,7 +54,7 @@ mysql> SELECT @@last_plan_from_cache; +------------------------+ ``` -Starting from v8.5.0, TiDB provides two system tables that allow you to view entries in the instance-level plan cache: +Starting from v8.5.0, TiDB provides two system tables to view entries in the instance-level plan cache: * `information_schema.tidb_plan_cache`: shows entries of instance plan cache in the current TiDB instance. * `information_schema.cluster_tidb_plan_cache`: shows entries of instance plan cache across all available TiDB instances. From 231abf4f33e9abab40f23ebffdfd64778857e96a Mon Sep 17 00:00:00 2001 From: Yuanjia Zhang Date: Thu, 22 May 2025 21:42:30 -0700 Subject: [PATCH 19/23] Update sql-prepared-plan-cache.md Co-authored-by: Aolin --- sql-prepared-plan-cache.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql-prepared-plan-cache.md b/sql-prepared-plan-cache.md index 6b686dc463df9..c7282e2a5b75e 100644 --- a/sql-prepared-plan-cache.md +++ b/sql-prepared-plan-cache.md @@ -315,7 +315,7 @@ When the unused memory of the TiDB server is less than a certain threshold, the You can control the threshold by configuring the system variable `tidb_prepared_plan_cache_memory_guard_ratio`. The threshold is 0.1 by default, which means when the unused memory of the TiDB server is less than 10% of the total memory (90% of the memory is used), the memory protection mechanism is triggered. -### Instance Level Plan Cache Memory Management +### Memory management of instance-level plan cache You can use the `tidb_instance_plan_cache_max_size` system variable to set the total memory limit for the Instance Plan Cache. For example, `set global tidb_instance_plan_cache_max_size=200MiB`. From ab5657701be28f53aed69391b178ae4aa80f8dd8 Mon Sep 17 00:00:00 2001 From: Yuanjia Zhang Date: Thu, 22 May 2025 21:42:37 -0700 Subject: [PATCH 20/23] Update sql-prepared-plan-cache.md Co-authored-by: Aolin --- sql-prepared-plan-cache.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/sql-prepared-plan-cache.md b/sql-prepared-plan-cache.md index c7282e2a5b75e..16944d0945016 100644 --- a/sql-prepared-plan-cache.md +++ b/sql-prepared-plan-cache.md @@ -317,7 +317,11 @@ You can control the threshold by configuring the system variable `tidb_prepared_ ### Memory management of instance-level plan cache -You can use the `tidb_instance_plan_cache_max_size` system variable to set the total memory limit for the Instance Plan Cache. For example, `set global tidb_instance_plan_cache_max_size=200MiB`. +You can use the [`tidb_instance_plan_cache_max_size`](/system-variables.md#tidb_instance_plan_cache_max_size-new-in-v840) system variable to set the maximum memory usage for the instance-level plan cache. For example, to set the cache limit to 200 MiB: + +```sql +SET GLOBAL tidb_instance_plan_cache_max_size = 200MiB +``` TiDB purges the Instance Plan Cache periodically. You can use the `tidb_instance_plan_cache_reserved_percentage` system variable to control the amount of memory purged each time. For example, if the default percentage is `0.1` and the maximum cache size is `200MiB`, TiDB purges `200 * 0.1 = 20MiB` of memory each time. From 1bb55a2dc7af9c5dc516d9433aeba192124c94ab Mon Sep 17 00:00:00 2001 From: Yuanjia Zhang Date: Thu, 22 May 2025 21:42:42 -0700 Subject: [PATCH 21/23] Update sql-prepared-plan-cache.md Co-authored-by: Aolin --- sql-prepared-plan-cache.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql-prepared-plan-cache.md b/sql-prepared-plan-cache.md index 16944d0945016..13ccb30db73ca 100644 --- a/sql-prepared-plan-cache.md +++ b/sql-prepared-plan-cache.md @@ -323,7 +323,7 @@ You can use the [`tidb_instance_plan_cache_max_size`](/system-variables.md#tidb_ SET GLOBAL tidb_instance_plan_cache_max_size = 200MiB ``` -TiDB purges the Instance Plan Cache periodically. You can use the `tidb_instance_plan_cache_reserved_percentage` system variable to control the amount of memory purged each time. For example, if the default percentage is `0.1` and the maximum cache size is `200MiB`, TiDB purges `200 * 0.1 = 20MiB` of memory each time. +TiDB purges the instance-level plan cache periodically. You can control the amount of memory purged each time by using the [`tidb_instance_plan_cache_reserved_percentage`](/system-variables.md#tidb_instance_plan_cache_reserved_percentage-new-in-v840) system variable. For example, if the default percentage is `0.1` and the maximum cache size is `200MiB`, TiDB purges `200 * 0.1 = 20MiB` of memory each time. ## Clear execution plan cache From 8734d046846a4b8961b039bd6f6c82987405cd18 Mon Sep 17 00:00:00 2001 From: Yuanjia Zhang Date: Thu, 22 May 2025 21:42:47 -0700 Subject: [PATCH 22/23] Update sql-prepared-plan-cache.md Co-authored-by: Aolin --- sql-prepared-plan-cache.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql-prepared-plan-cache.md b/sql-prepared-plan-cache.md index 13ccb30db73ca..0d7ec313defff 100644 --- a/sql-prepared-plan-cache.md +++ b/sql-prepared-plan-cache.md @@ -329,7 +329,7 @@ TiDB purges the instance-level plan cache periodically. You can control the amou > **Note:** > -> The statement currently only supports clearing session-level plan cache. Support for clearing instance-level plan cache will be added in a future release. +> Currently, you can only use the `ADMIN FLUSH [SESSION | INSTANCE] PLAN_CACHE` statement to clear session-level plan cache. Support for clearing instance-level plan cache will be available in a future release. You can clear execution plan cache by executing the `ADMIN FLUSH [SESSION | INSTANCE] PLAN_CACHE` statement. From 9a3dd2a66bfaf3a7f0c1e6a68c177173cab08379 Mon Sep 17 00:00:00 2001 From: Yuanjia Zhang Date: Thu, 22 May 2025 21:43:09 -0700 Subject: [PATCH 23/23] Update sql-prepared-plan-cache.md Co-authored-by: Aolin --- sql-prepared-plan-cache.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql-prepared-plan-cache.md b/sql-prepared-plan-cache.md index 0d7ec313defff..e16dd437216f8 100644 --- a/sql-prepared-plan-cache.md +++ b/sql-prepared-plan-cache.md @@ -290,7 +290,7 @@ Due to memory limits, plan cache entries might be missed. -### Session Level Plan Cache Memory Management +### Memory management of session-level plan cache Starting from v7.1.0, you can control the maximum number of plans that can be cached in each session by configuring the system variable [`tidb_session_plan_cache_size`](/system-variables.md#tidb_session_plan_cache_size-new-in-v710). For different environments, the recommended value is as follows and you can adjust it according to the monitoring panels: