【GreatSQL优化器-17】DYNAMIC RANGE

打印 上一主题 下一主题

主题 921|帖子 921|积分 2765

【GreatSQL优化器-17】DYNAMIC RANGE

一、DYNAMIC RANGE介绍

GreatSQL 的优化器有一种扫描方式是动态范围扫描方式,类似于“已读乱回”模式,这种模式是在表有多个索引的情况下,对驱动表连接的时候部门选择索引的情况。优化器没有找到好的索引可以使用,但发现在知道前面表的列值后,大概会使用某些索引。对于前面表中的每个行组合,优化器查抄是否可以使用 range 或 index merge 访问方法来检索行。虽然这不是很快,但比执行完全没有索引的连接要快。
下面用一个简朴的例子来说明直方图怎么应用在优化器。
  1. CREATE TABLE t1 (c1 INT PRIMARY KEY, c2 INT,date1 DATETIME);
  2. INSERT INTO t1 VALUES (1,10,'2021-03-25 16:44:00.123456'),(2,1,'2022-03-26 16:44:00.123456'),(3,4,'2023-03-27 16:44:00.123456'),(5,5,'2024-03-25 16:44:00.123456'),(7,null,'2020-03-25 16:44:00.123456'),(8,10,'2020-10-25 16:44:00.123456'),(11,16,'2023-03-25 16:44:00.123456');
  3. CREATE TABLE t2 (cc1 INT PRIMARY KEY, cc2 INT);
  4. INSERT INTO t2 VALUES (1,3),(2,1),(3,2),(4,3),(5,15);
  5. CREATE TABLE t3 (ccc1 INT, ccc2 varchar(100));
  6. INSERT INTO t3 VALUES (1,'aa1'),(2,'bb1'),(3,'cc1'),(4,'dd1'),(null,'ee');
  7. CREATE INDEX idx1 ON t1(c2);
  8. CREATE INDEX idx2 ON t1(c2,date1);
  9. CREATE INDEX idx2_1 ON t2(cc2);
  10. CREATE INDEX idx3_1 ON t3(ccc1);
  11. greatsql> EXPLAIN SELECT * FROM t1 join t3 ON t1.c1=t3.ccc1 or t1.c2<5;
  12. +----+-------------+-------+------------+------+-------------------+------+---------+------+------+----------+------------------------------------------------+
  13. | id | select_type | table | partitions | type | possible_keys     | key  | key_len | ref  | rows | filtered | Extra                                          |
  14. +----+-------------+-------+------------+------+-------------------+------+---------+------+------+----------+------------------------------------------------+
  15. |  1 | SIMPLE      | t3    | NULL       | ALL  | idx3_1            | NULL | NULL    | NULL |    5 |   100.00 | NULL                                           |
  16. |  1 | SIMPLE      | t1    | NULL       | ALL  | PRIMARY,idx1,idx2 | NULL | NULL    | NULL |    7 |    43.67 | Range checked for each record (index map: 0x7) |
  17. -- 这里的结果出现了Range checked,说明进行了DYNAMIC_RANGE
  18. +----+-------------+-------+------------+------+-------------------+------+---------+------+------+----------+------------------------------------------------+
  19. greatsql> SELECT * FROM t1 join t3 ON t1.c1=t3.ccc1 or t1.c2<5;
  20. +----+------+---------------------+------+------+
  21. | c1 | c2   | date1               | ccc1 | ccc2 |
  22. +----+------+---------------------+------+------+
  23. |  1 |   10 | 2021-03-25 16:44:00 |    1 | aa1  |
  24. |  2 |    1 | 2022-03-26 16:44:00 |    1 | aa1  |
  25. |  3 |    4 | 2023-03-27 16:44:00 |    1 | aa1  |
  26. |  2 |    1 | 2022-03-26 16:44:00 |    2 | bb1  |
  27. |  3 |    4 | 2023-03-27 16:44:00 |    2 | bb1  |
  28. |  2 |    1 | 2022-03-26 16:44:00 |    3 | cc1  |
  29. |  3 |    4 | 2023-03-27 16:44:00 |    3 | cc1  |
  30. |  2 |    1 | 2022-03-26 16:44:00 |    4 | dd1  |
  31. |  3 |    4 | 2023-03-27 16:44:00 |    4 | dd1  |
  32. |  2 |    1 | 2022-03-26 16:44:00 | NULL | ee   |
  33. |  3 |    4 | 2023-03-27 16:44:00 | NULL | ee   |
  34. +----+------+---------------------+------+------+
  35. greatsql> SELECT * FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;
  36.              "attaching_conditions_to_tables": {
  37.               "original_condition": "((`t1`.`c1` = `t3`.`ccc1`) or (`t1`.`c2` < 5))",
  38.               "attached_conditions_computation": [
  39.                 {
  40.                   "table": "`t1`",
  41.                   "rechecking_index_usage": { -- 这里对t1进行了recheck
  42.                     "recheck_reason": "not_first_table",
  43.                     "range_analysis": {
  44.                       "table_scan": {
  45.                         "rows": 7,
  46.                         "cost": 3.05
  47.                       },
  48.                       "potential_range_indexes": [
  49.                         {
  50.                           "index": "PRIMARY",
  51.                           "usable": true,
  52.                           "key_parts": [
  53.                             "c1"
  54.                           ]
  55.                         },
  56.                         {
  57.                           "index": "idx1",
  58.                           "usable": true,
  59.                           "key_parts": [
  60.                             "c2",
  61.                             "c1"
  62.                           ]
  63.                         },
  64.                         {
  65.                           "index": "idx2",
  66.                           "usable": true,
  67.                           "key_parts": [
  68.                             "c2",
  69.                             "date1",
  70.                             "c1"
  71.                           ]
  72.                         }
  73.                       ],
  74.                       "best_covering_index_scan": {
  75.                         "index": "idx2",
  76.                         "cost": 0.952742,
  77.                         "chosen": true
  78.                       },
  79.                       "setup_range_conditions": [
  80.                       ],
  81.                       "group_index_range": {
  82.                         "chosen": false,
  83.                         "cause": "not_single_table"
  84.                       },
  85.                       "skip_scan_range": {
  86.                         "chosen": false,
  87.                         "cause": "not_single_table"
  88.                       },
  89.                       "analyzing_range_alternatives": {
  90.                         "range_scan_alternatives": [
  91.                         ],
  92.                         "analyzing_roworder_intersect": {
  93.                           "usable": false,
  94.                           "cause": "too_few_roworder_scans"
  95.                         }
  96.                       },
  97.                       "analyzing_index_merge_union": [
  98.                         {
  99.                           "indexes_to_merge": [
  100.                             {
  101.                               "range_scan_alternatives": [
  102.                                 {
  103.                                   "index": "PRIMARY",
  104.                                   "chosen": false,
  105.                                   "cause": "depends_on_unread_values"
  106.                                 }
  107.                               ],
  108.                               "chosen": false,
  109.                               "cause": "cost"
  110.                             },
  111.                             {
  112.                               "range_scan_alternatives": [
  113.                                 {
  114.                                   "index": "idx1",
  115.                                   "ranges": [
  116.                                     "NULL < c2 < 5"
  117.                                   ],
  118.                                   "index_dives_for_eq_ranges": true,
  119.                                   "rowid_ordered": false,
  120.                                   "using_mrr": false,
  121.                                   "index_only": true,
  122.                                   "in_memory": 1,
  123.                                   "rows": 2,
  124.                                   "cost": 0.460274,
  125.                                   "chosen": true
  126.                                 },
  127.                                 {
  128.                                   "index": "idx2",
  129.                                   "ranges": [
  130.                                     "NULL < c2 < 5"
  131.                                   ],
  132.                                   "index_dives_for_eq_ranges": true,
  133.                                   "rowid_ordered": false,
  134.                                   "using_mrr": false,
  135.                                   "index_only": true,
  136.                                   "in_memory": 1,
  137.                                   "rows": 2,
  138.                                   "cost": 0.460457,
  139.                                   "chosen": false,
  140.                                   "cause": "cost"
  141.                                 }
  142.                               ],
  143.                               "chosen": false,
  144.                               "cause": "cost"
  145.                             }
  146.                           ],
  147.                           "cost_of_reading_ranges": 0,
  148.                           "chosen": false,
  149.                           "cause": "cost"
  150.                         }
  151.                       ]
  152.                     }
  153.                   }
  154.                 }
  155.               ],
  156.               "attached_conditions_summary": [
  157.                 {
  158.                   "table": "`t3`",
  159.                   "attached": null
  160.                 },
  161.                 {
  162.                   "table": "`t1`",
  163.                   "attached": "((`t1`.`c1` = `t3`.`ccc1`) or (`t1`.`c2` < 5))"
  164.                 }
  165.               ]
  166.             }
  167.      "join_execution": {
  168.         "select#": 1,
  169.         "steps": [
  170.           {
  171.             "rows_estimation_per_outer_row": { -- 这里只截取t3读第一行时候t1的索引选择
  172.               "table": "`t1`",
  173.               "range_analysis": {
  174.                 "table_scan": {
  175.                   "rows": 7,
  176.                   "cost": 3.05
  177.                 },
  178.                 "potential_range_indexes": [
  179.                   {
  180.                     "index": "PRIMARY",
  181.                     "usable": true,
  182.                     "key_parts": [
  183.                       "c1"
  184.                     ]
  185.                   },
  186.                   {
  187.                     "index": "idx1",
  188.                     "usable": true,
  189.                     "key_parts": [
  190.                       "c2",
  191.                       "c1"
  192.                     ]
  193.                   },
  194.                   {
  195.                     "index": "idx2",
  196.                     "usable": true,
  197.                     "key_parts": [
  198.                       "c2",
  199.                       "date1",
  200.                       "c1"
  201.                     ]
  202.                   }
  203.                 ],
  204.                 "best_covering_index_scan": {
  205.                   "index": "idx2",
  206.                   "cost": 0.952742,
  207.                   "chosen": true
  208.                 },
  209.                 "setup_range_conditions": [
  210.                 ],
  211.                 "group_index_range": {
  212.                   "chosen": false,
  213.                   "cause": "not_single_table"
  214.                 },
  215.                 "skip_scan_range": {
  216.                   "chosen": false,
  217.                   "cause": "not_single_table"
  218.                 },
  219.                 "analyzing_range_alternatives": {
  220.                   "range_scan_alternatives": [
  221.                   ],
  222.                   "analyzing_roworder_intersect": {
  223.                     "usable": false,
  224.                     "cause": "too_few_roworder_scans"
  225.                   }
  226.                 },
  227.                 "analyzing_index_merge_union": [
  228.                   {
  229.                     "indexes_to_merge": [
  230.                       {
  231.                         "range_scan_alternatives": [
  232.                           {
  233.                             "index": "PRIMARY",
  234.                             "ranges": [
  235.                               "c1 = 1"
  236.                             ],
  237.                             "index_dives_for_eq_ranges": true,
  238.                             "rowid_ordered": true,
  239.                             "using_mrr": false,
  240.                             "index_only": true,
  241.                             "in_memory": 1,
  242.                             "rows": 1,
  243.                             "cost": 0.36,
  244.                             "chosen": true
  245.                           }
  246.                         ],
  247.                         "index_to_merge": "PRIMARY",
  248.                         "cumulated_cost": 0.36
  249.                       },
  250.                       {
  251.                         "range_scan_alternatives": [
  252.                           {
  253.                             "index": "idx1",
  254.                             "ranges": [
  255.                               "NULL < c2 < 5"
  256.                             ],
  257.                             "index_dives_for_eq_ranges": true,
  258.                             "rowid_ordered": false,
  259.                             "using_mrr": false,
  260.                             "index_only": true,
  261.                             "in_memory": 1,
  262.                             "rows": 2,
  263.                             "cost": 0.460274,
  264.                             "chosen": true
  265.                           },
  266.                           {
  267.                             "index": "idx2",
  268.                             "ranges": [
  269.                               "NULL < c2 < 5"
  270.                             ],
  271.                             "index_dives_for_eq_ranges": true,
  272.                             "rowid_ordered": false,
  273.                             "using_mrr": false,
  274.                             "index_only": true,
  275.                             "in_memory": 1,
  276.                             "rows": 2,
  277.                             "cost": 0.460457,
  278.                             "chosen": false,
  279.                             "cause": "cost"
  280.                           }
  281.                         ],
  282.                         "index_to_merge": "idx1",
  283.                         "cumulated_cost": 0.820274
  284.                       }
  285.                     ],
  286.                     "cost_of_reading_ranges": 0.820274,
  287.                     "cost_of_mapping_rowid_in_non_clustered_pk_scan": 0.1,
  288.                     "cost_sort_rowid_and_read_disk": 0.4375,
  289.                     "use_roworder_index_merge": true, -- 根据上面结果选择了主键合并idx1的结果
  290.                     "cause": "cost"
  291.                   }
  292.                 ]
  293.               }
  294.             }
  295.           },
  296. greatsql> EXPLAIN SELECT * FROM t1 join t3 ON t1.c1=t3.ccc1 or t1.c2<5;
  297. +----+-------------+-------+------------+------+-------------------+------+---------+------+------+----------+------------------------------------------------+
  298. | id | select_type | table | partitions | type | possible_keys     | key  | key_len | ref  | rows | filtered | Extra                                          |
  299. +----+-------------+-------+------------+------+-------------------+------+---------+------+------+----------+------------------------------------------------+
  300. |  1 | SIMPLE      | t3    | NULL       | ALL  | idx3_1            | NULL | NULL    | NULL |    5 |   100.00 | NULL                                           |
  301. |  1 | SIMPLE      | t1    | NULL       | ALL  | PRIMARY,idx1,idx2 | NULL | NULL    | NULL |    7 |    42.85 | Range checked for each record (index map: 0x7) | -- 这里0x7指的是t1表的3条索引
  302. +----+-------------+-------+------------+------+-------------------+------+---------+------+------+----------+------------------------------------------------+
  303. -- 驱动表t3一共5条数据,每行记录都进行了一次test_quick_select()来查找t1是否有可用的更优索引
复制代码
表一:必要举行 recheck 的原因
场景条件非join表连接的第一张表存在针对这张表的条件连接的第一张表没有第一张表的列与常量值的比较,limit值小于预估的满足条件的行数表二:quick_type 范例
范例说明QS_NONE不使用快速扫描QS_RANGE使用范围扫描QS_DYNAMIC_RANGE使用动态范围表三:表用 QS_DYNAMIC_RANGE 的场合
条件(以下必须全部满足)说明条件列存在索引有机会天生mm treekeyuse_array数组没有值有值的话执行REF扫描,一般有OR条件时候导致该数组没有值表没有对应的mm tree无法天生mm tree的条件这张表不是join连接的第一张表对表执行recheckrecheck的原因见表一recheck以后天生AccessPath但是找到的范围老手数大于等于100行三、实际例子说明

接下来看几个例子来说明上面的代码:
  1. static bool make_join_query_block(JOIN *join, Item *cond) {
  2.         // 满足以下条件的需要进行RECHECK,要么是非第一张表要么是有limit语句
  3.         if ((tab->type() == JT_ALL || tab->type() == JT_RANGE ||
  4.              tab->type() == JT_INDEX_MERGE || tab->type() == JT_INDEX_SCAN) &&
  5.             tab->use_quick != QS_RANGE) {
  6.           if (cond &&                              // 1a
  7.               (tab->keys() != tab->const_keys) &&  // 1b
  8.               (i > 0 ||                            // 1c
  9.                (join->query_block->master_query_expression()->item &&
  10.                 cond->is_outer_reference())))
  11.             recheck_reason = NOT_FIRST_TABLE;
  12.           else if (!tab->const_keys.is_clear_all() &&  // 2a
  13.                    i == join->const_tables &&          // 2b
  14.                    (join->query_expression()->select_limit_cnt <
  15.                     (tab->position()->rows_fetched *
  16.                      tab->position()->filter_effect)) &&  // 2c
  17.                    !join->calc_found_rows)                // 2d
  18.             recheck_reason = LOW_LIMIT;
  19.         // 满足这个if条件的执行QS_DYNAMIC_RANGE,详情见下面几张表
  20.           if (!tab->table()->quick_keys.is_subset(tab->checked_keys) ||
  21.               !tab->needed_reg.is_subset(tab->checked_keys)) {
  22.             tab->keys().merge(tab->table()->quick_keys);
  23.             tab->keys().merge(tab->needed_reg);
  24.             if (!tab->needed_reg.is_clear_all() &&
  25.                 (tab->table()->quick_keys.is_clear_all() ||
  26.                  (tab->range_scan() &&
  27.                   (tab->range_scan()->num_output_rows() >= 100.0)))) {
  28.               tab->use_quick = QS_DYNAMIC_RANGE;
  29.               tab->set_type(JT_ALL);
  30.             } else
  31.               tab->use_quick = QS_RANGE;
  32.           }
  33.         }
  34. }
  35. // 实际使用代码
  36. bool DynamicRangeIterator::Init() {
  37.   // 每次迭代器开始的时候先获取最佳索引组合和AccessPath
  38.   AccessPath *range_scan;
  39.   int rc = test_quick_select(&range_scan);
  40. }
复制代码
四、总结

从上面关于 DYNAMIC RANGE 的解释我们知道这个状态必要经过一系列判断,并且只在特定条件才有大概出现,但是每行举行一次索引判断照旧很消耗资源的,最好照旧直接走索引,以是要只管避免这种情况,适当的时候可以举行 SQL 下令干预。

Enjoy GreatSQL
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

拉不拉稀肚拉稀

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表