From 9d7aaec0ff636fd176900ec79debb08663bb04fc Mon Sep 17 00:00:00 2001 From: Ran Date: Thu, 9 Jul 2020 19:01:13 +0800 Subject: [PATCH 1/3] perf-tuning: add wrong-index-solution --- TOC.md | 1 + wrong-index-solution.md | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 wrong-index-solution.md diff --git a/TOC.md b/TOC.md index 1f1d4a3a1e183..e8548b7be2377 100644 --- a/TOC.md +++ b/TOC.md @@ -108,6 +108,7 @@ + [Join Reorder](/join-reorder.md) + Physical Optimization + [Statistics](/statistics.md) + + [Wrong Index Solution](/wrong-index-solution.md) + Control Execution Plan + [Optimizer Hints](/optimizer-hints.md) + [SQL Plan Management](/sql-plan-management.md) diff --git a/wrong-index-solution.md b/wrong-index-solution.md new file mode 100644 index 0000000000000..6ba46b0214f21 --- /dev/null +++ b/wrong-index-solution.md @@ -0,0 +1,27 @@ +--- +title: Wrong Index Solution +summary: Learn how to solve the wrong index issue. +category: performance +--- + +# Wrong Index Solution + +If you find that the execution speed of some query does not reach the expectation, it might be caused by the wrong usage of indexes. + +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 wrong usage of indexes still persists, 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 executing `USE_INDEX` or `use index` (see [USE_INDEX](/optimizer-hints.md#use_indext1_name-idx1_name--idx2_name-) for details). In addition, you can also non-intrusively change the query behavior by using [SQL Plan Management](/sql-plan-management.md). + +## 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. From 55287175776439cab6b488dce2a3196bc0ab0dc5 Mon Sep 17 00:00:00 2001 From: Ran Date: Thu, 9 Jul 2020 19:29:04 +0800 Subject: [PATCH 2/3] fix lint --- wrong-index-solution.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wrong-index-solution.md b/wrong-index-solution.md index 6ba46b0214f21..d55beb4dd8f09 100644 --- a/wrong-index-solution.md +++ b/wrong-index-solution.md @@ -8,7 +8,7 @@ category: performance If you find that the execution speed of some query does not reach the expectation, it might be caused by the wrong usage of indexes. -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. +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 From ab3dd2b8e6c4e7471e6641cff0c981357d654f47 Mon Sep 17 00:00:00 2001 From: Ran Date: Mon, 20 Jul 2020 10:53:26 +0800 Subject: [PATCH 3/3] Apply suggestions from code review Co-authored-by: Keke Yi <40977455+yikeke@users.noreply.github.com> --- wrong-index-solution.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/wrong-index-solution.md b/wrong-index-solution.md index d55beb4dd8f09..7d01e42dc8e4a 100644 --- a/wrong-index-solution.md +++ b/wrong-index-solution.md @@ -1,18 +1,17 @@ --- title: Wrong Index Solution summary: Learn how to solve the wrong index issue. -category: performance --- # Wrong Index Solution -If you find that the execution speed of some query does not reach the expectation, it might be caused by the wrong usage of indexes. +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 wrong usage of indexes still persists, refer to the next section. +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 @@ -20,7 +19,7 @@ The near 100% health state suggests that the `ANALYZE` statement is just complet 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 executing `USE_INDEX` or `use index` (see [USE_INDEX](/optimizer-hints.md#use_indext1_name-idx1_name--idx2_name-) for details). In addition, you can also non-intrusively change the query behavior by using [SQL Plan Management](/sql-plan-management.md). +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