From d4e14f21106b2b73773d5dcb3a89b9b6e62147b3 Mon Sep 17 00:00:00 2001 From: Ran Date: Wed, 22 Jul 2020 10:53:25 +0800 Subject: [PATCH] cherry pick #3213 to release-4.0 Signed-off-by: ti-srebot --- TOC.md | 1 + wrong-index-solution.md | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 wrong-index-solution.md diff --git a/TOC.md b/TOC.md index e8fe83cd5a688..57cfdffec08d9 100644 --- a/TOC.md +++ b/TOC.md @@ -113,6 +113,7 @@ + Physical Optimization + [Index Selection](/choose-index.md) + [Statistics](/statistics.md) + + [Wrong Index Solution](/wrong-index-solution.md) + [Distinct Optimization](/agg-distinct-optimization.md) + [Prepare Execution Plan Cache](/sql-prepare-plan-cache.md) + Control Execution Plan diff --git a/wrong-index-solution.md b/wrong-index-solution.md new file mode 100644 index 0000000000000..7d01e42dc8e4a --- /dev/null +++ b/wrong-index-solution.md @@ -0,0 +1,26 @@ +--- +title: Wrong Index Solution +summary: Learn how to solve the wrong index issue. +--- + +# Wrong Index Solution + +If you find that the execution speed of some query does not reach the expectation, the optimizer might choose the wrong index to run the query. + +You can first view the [health state of tables](/statistics.md#health-state-of-tables) in the statistics, and then solve this issue according to the different health states. + +## Low health state + +The low health state means TiDB has not performed the`ANALYZE` statement for a long time. You can update the statistics by running the `ANALYZE` command. After the update, if the optimizer still uses the wrong index, refer to the next section. + +## Near 100% health state + +The near 100% health state suggests that the `ANALYZE` statement is just completed or was completed a short time ago. In this case, the wrong index issue might be related to TiDB's estimation logic for the number of rows. + +For equivalence queries, the cause might be [Count-Min Sketch](/statistics.md#count-min-sketch). You can check whether Count-Min Sketch is the cause and take corresponding solutions. + +If the cause above does not apply to your problem, you can force-select indexes by using the `USE_INDEX` or `use index` optimzer hint (see [USE_INDEX](/optimizer-hints.md#use_indext1_name-idx1_name--idx2_name-) for details). Also, you can change the query behavior by using [SQL Plan Management](/sql-plan-management.md) in a non-intrusive way. + +## Other situations + +Apart from the aforementioned situations, the wrong index issue might also be caused by data updates which renders all the indexes no longer applicable. In such cases, you need to perform analysis on the conditions and data distribution to see whether new indexes can speed up the query. If so, you can add new indexes by running the [`ADD INDEX`](/sql-statements/sql-statement-add-index.md) command.