-
Notifications
You must be signed in to change notification settings - Fork 657
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
planner: add documents for cost model variables (#9802)
- Loading branch information
Showing
5 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
--- | ||
title: Cost Model | ||
summary: Learn how the cost model used by TiDB works during physical optimization. | ||
--- | ||
|
||
# Cost Model | ||
|
||
TiDB uses a cost model to choose an index and operator during [physical optimization](/sql-physical-optimization.md). The process is illustrated in the following diagram: | ||
|
||
![CostModel](/media/cost-model.png) | ||
|
||
TiDB calculates the access cost of each index and the execution cost of each physical operator in plans (such as HashJoin and IndexJoin) and chooses the minimum cost plan. | ||
|
||
The following is a simplified example to explain how the cost model works. Suppose that there is a table `t`: | ||
|
||
```sql | ||
mysql> SHOW CREATE TABLE t; | ||
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | ||
| Table | Create Table | | ||
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | ||
| t | CREATE TABLE `t` ( | ||
`a` int(11) DEFAULT NULL, | ||
`b` int(11) DEFAULT NULL, | ||
`c` int(11) DEFAULT NULL, | ||
KEY `b` (`b`), | ||
KEY `c` (`c`) | ||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin | | ||
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | ||
1 row in set (0.00 sec) | ||
``` | ||
|
||
When executing the `SELECT * FROM t WHERE b < 100 and c < 100` statement, suppose that TiDB estimates 20 rows meet the `b < 100` condition and 500 rows meet `c < 100`, and the length of `INT` type indexes is 8. Then TiDB calculates the cost for two indexes: | ||
|
||
+ The cost of index `b` = row count of `b < 100` \* length of index `b` = 20 * 8 = 160 | ||
+ The cost of index `c` = row count of `c < 100` \* length of index `c` = 500 * 8 = 4000 | ||
|
||
Because the cost of index `b` is lower, TiDB chooses `b` as the index. | ||
|
||
The preceding example is simplified and only used to explain the basic principle. In real SQL executions, the TiDB cost model is more complex. | ||
|
||
## Cost Model Version 2 | ||
|
||
> **Warning:** | ||
> | ||
> - Cost Model Version 2 is currently an experimental feature. It is not recommended that you use it for production environments. | ||
> - Switching the version of the cost model might cause changes to query plans. | ||
TiDB v6.2.0 introduces Cost Model Version 2, a new cost model. | ||
|
||
Cost Model Version 2 provides a more accurate regression calibration of the cost formula, adjusts some of the cost formulas, and is more accurate than the previous version of the cost formula. | ||
|
||
To switch the version of cost model, you can set the [`tidb_cost_model_version`](/system-variables.md#tidb_cost_model_version-new-in-v620) variable. |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters