【GreatSQL优化器-18】GROUP_INDEX_SKIP_SCAN

打印 上一主题 下一主题

主题 1843|帖子 1843|积分 5529

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
【GreatSQL优化器-18】GROUP_INDEX_SKIP_SCAN

一、GROUP_INDEX_SKIP_SCAN先容

GreatSQL 优化器的分组索引跳跃扫描(GROUP Index Skip Scan) 是一种优化查询的技术,尤其在联合索引中用于减少扫描的无效行数。group by操作在没有合适的索引可用的时候,通常先扫描整个表提取数据并创建一个临时表,然后按照 group by 指定的列进行排序。在这个临时表里面,对于每一个group的数据行来说是连续在一起的。完成排序之后,就可以发现所有的groups,并可以实行聚集函数(aggregate function)。可以看到,在没有利用索引的时候,必要创建临时表和排序。在实行计划中通常可以看到“Using temporary; Using filesort”
GROUP索引跳跃扫描利用的是联合索引中非首列(非最左前缀)的索引列,来提高查询效率。例如,如果有个查询需求,必要查某个列的 distinct 值,或者 group by 之后的值 MIN()/MAX() 值,最简单的方式是扫描整个数据页,然后分组排序后,取 DISTINCT/MIN/MAX 值,但由于索引自己就有序而且完成了 group by 工作,如果可以直接借助于这个索引的有序性,那么扫描整个索引就可以避免二次排序的开销。这个功能支持带有聚合函数+GROUP BY或者聚合函数+DISTINCT或者DISTINCT的SQL利用。
这个功能类似于 INDEX_SKIP_SCAN,做一次对相同的 key 值进行 skip 动作,即可以跳过了索引上相同的段, 如许相比较与索引扫描而言,减少了很多的索引扫描,索引稀疏性越好,性能就会相对更好。
下面用一个简单的例子来说明GROUP_INDEX_SKIP_SCAN怎么应用在优化器。
  1. CREATE TABLE t1(c1 INT, c2 INT, c3 INT, c4 INT);
  2. CREATE UNIQUE INDEX i1_t1 ON t1(c1,c2,c3);
  3. INSERT INTO t1 VALUES (1,1,1,1), (1,1,2,2), (1,3,3,3), (1,4,4,4), (1,5,5,5),
  4.                       (2,1,1,1), (2,2,2,2), (2,3,3,3), (2,4,4,4), (2,5,5,5);
  5. INSERT INTO t1 SELECT c1, c2, c3+5, c4+10  FROM t1;
  6. INSERT INTO t1 SELECT c1, c2, c3+10, c4+20 FROM t1;
  7. INSERT INTO t1 SELECT c1, c2, c3+20, c4+40 FROM t1;
  8. INSERT INTO t1 SELECT c1, c2, c3+40, c4+80 FROM t1;
  9. ANALYZE TABLE t1;
  10. -- 下面的结果用到了group index skip scan,扫描方式是RANGE SCAN。
  11. greatsql> explain SELECT c1, MIN(c2) FROM t1 GROUP BY c1;
  12. +----+-------------+-------+------------+-------+---------------+-------+---------+------+------+----------+--------------------------+
  13. | id | select_type | table | partitions | type  | possible_keys | key   | key_len | ref  | rows | filtered | Extra                    |
  14. +----+-------------+-------+------------+-------+---------------+-------+---------+------+------+----------+--------------------------+
  15. |  1 | SIMPLE      | t1    | NULL       | range | i1_t1         | i1_t1 | 10      | NULL |    3 |   100.00 | Using index for group-by |
  16. +----+-------------+-------+------------+-------+---------------+-------+---------+------+------+----------+--------------------------+
  17. /* 运行过程:
  18. 1、先找到第一列的唯一值,得到结果为1和2
  19. SELECT distinct c1 FROM t1;
  20. 2、根据第一列的分组结果在分组内对c2执行范围扫描
  21. SELECT c1, MIN(c2) FROM t1 WHERE c1 = 1;
  22. SELECT c1, MIN(c2) FROM t1 WHERE c1 = 2;*/
  23. -- 如果没有联合索引,那么这条sql内部要创建临时表用于数据的临时处理,扫描方式是全表扫描。可见用GROUP_INDEX_SKIP_SCAN方式执行groupby可以节省空间和开销,提升执行效率。
  24. greatsql> DROP INDEX i1_t1 ON t1;
  25. greatsql> explain SELECT c1, MIN(c2) FROM t1 GROUP BY c1;
  26. +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-----------------+
  27. | id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra           |
  28. +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-----------------+
  29. |  1 | SIMPLE      | t1    | NULL       | ALL  | NULL          | NULL | NULL    | NULL |  160 |   100.00 | Using temporary |
  30. +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-----------------+
复制代码
二、get_best_group_min_max代码解释

group index skip scan是根据条件和聚合函数列以及distinct列,以及查找对应的联合索引进行判定能不能用group index skip scan,此中联合索引中聚合函数列或者distinct列之前的列为prefix列,即用来作为分组的依据的列,如果有多个除了第一列之外的范围列,那么选取碰到的第一个聚合函数列之前的列为前缀进行分组。
group index skip scan功能没有相关可以开启关闭的关键词,默认满足条件就可以利用,因此如果不想利用就要改变条件或者group by分组,只要不满足条件就不会利用联合索引。
  1. int test_quick_select() {
  2.   // 先判断GROUP_INDEX_SKIP_SCAN是否满足条件,不满足的话执行索引合并。
  3.   AccessPath *group_path = get_best_group_min_max();
  4.   // 不满足GROUP_INDEX_SKIP_SCAN执行索引合并,相关知识看之前知识介绍
  5. }
  6. AccessPath *get_best_group_min_max() {
  7.   // 遍历表所有索引,找到联合索引
  8.   for (uint cur_param_idx = 0; cur_param_idx < param->keys; ++cur_param_idx) {
  9.     // 处理group by语句
  10.     if (!join->group_list.empty()) {
  11.       // 找到联合索引内的列字段相关mm tree
  12.       if (tree) cur_tree = get_index_range_tree(cur_index, tree, param);
  13.       // 该条件是否是等号条件或者IS NULL条件
  14.           is_eq_range_pred = !(range->min_flag & is_open_range) &&
  15.                              !(range->max_flag & is_open_range) &&
  16.                              ((range->maybe_null() && range->min_value[0] &&
  17.                                range->max_value[0]) ||
  18.                               memcmp(range->min_value, range->max_value,
  19.                                      cur_part->store_length) == 0);
  20.     }
  21.     // 处理distinct语句
  22.     if ((join->group_list.empty() && join->select_distinct) ||
  23.         is_agg_distinct) {
  24.       检查distinct后面的列字段,必须包含联合索引前缀列或者所有列
  25.     }
  26.     // 获取第一个不在GROUP BY的索引字段
  27.     first_non_group_part =
  28.         (cur_group_key_parts < actual_key_parts(cur_index_info))
  29.             ? cur_index_info->key_part + cur_group_key_parts
  30.             : nullptr;
  31.     // 获取聚合函数的索引字段
  32.     first_non_infix_part = cur_min_max_arg_part
  33.                                ? (cur_min_max_arg_part < last_part)
  34.                                      ? cur_min_max_arg_part
  35.                                      : nullptr
  36.                                : nullptr;
  37.     // 接下来是对sql语句的各种检查,确认能否用group index skip scan,代码略,具体见表一
  38.     // 获取聚合函数列与第一个非GROUP BY列之间的差值
  39.     key_infix_parts = cur_key_infix_len
  40.                           ? (uint)(first_non_infix_part - first_non_group_part)
  41.                           : 0;
  42.     // 获取GROUP BY列与聚合函数列之间的列个数
  43.     cur_used_key_parts = cur_group_key_parts + key_infix_parts;
  44.     // 计算range范围内行数
  45.     if (tree) {
  46.       cur_quick_prefix_records = check_quick_select();
  47.     }
  48.     // 计算rows和cost
  49.     cost_group_min_max();
  50.   }
  51.   // 最后生成AccessPath
  52.   AccessPath *path = new (param->return_mem_root) AccessPath;
  53.   path->type = AccessPath::GROUP_INDEX_SKIP_SCAN;
  54. }
  55. // 计算rows和cost,这里主要展示的是rows的计算公式
  56. static void cost_group_min_max() {
  57.   table_records = table->file->stats.records;
  58.   keys_per_block = (table->file->stats.block_size / 2 /
  59.                         (index_info->key_length + table->file->ref_length) +
  60.                     1);
  61.   num_blocks = (uint)(table_records / keys_per_block) + 1;
  62.   if (index_info->has_records_per_key(group_key_parts - 1))
  63.     // Use index statistics
  64.     keys_per_group = index_info->records_per_key(group_key_parts - 1);
  65.   else
  66.     /* If there is no statistics try to guess */
  67.     keys_per_group = guess_rec_per_key(table, index_info, group_key_parts);
  68.   if (single_group)
  69.     num_groups = 1;
  70.   else {
  71.     num_groups = (uint)(table_records / keys_per_group) + 1;
  72.     if (range_tree && (quick_prefix_records != HA_POS_ERROR)) {
  73.       quick_prefix_selectivity =
  74.           (double)quick_prefix_records / (double)table_records;
  75.       num_groups = (uint)rint(num_groups * quick_prefix_selectivity);
  76.       num_groups = std::max(num_groups, 1U);
  77.     }
  78.   }
  79. }
  80. 最后具体实现过程见代码GroupIndexSkipScanIterator::Read(),这里不再展开赘述。
复制代码
表一:不能利用GROUP_INDEX_SKIP_SCAN的场所
场所cause说明sql语句涉及多张表not_single_table只能支持单张表sql语句带有rolluprollup表没有可用联合索引not_coveringsql语句带有ORDER BY DESCcannot_do_reverse_orderingsql语句不包含group_by或者DISTINCTnot_group_by_or_distinctsql语句不包含COUNT,SUM,AVG,MIN,MAX聚合函数not_applicable_aggregate_function查询条件生成了tree->mergesdisjuntive_predicate_present好比where c1=1 or c2>1,c1和c2属于差别索引查询条件既包含MIN/MAX列又包含非MIN/MAX列minmax_keypart_in_disjunctive_query见下面例子sql语句既带有distinct又带有min或者maxhave_both_agg_distinct_and_min_maxsql语句既带有distinct且distinct后面参数包含列和非列group_by后面跟的不是列而是表达式group_field_is_expression好比GROUP BY c1+1group by字段是联合索引的第一列group_attribute_not_prefix_in_index见下面例子distinct后面字段是派生表的列derived_tabledistinct后面字段是联合索引非前缀列或者非所有列select_attribute_not_prefix_in_indexdistinct后面字段必须是联合索引前缀列或者所有列group by前面的聚合函数的列不是连续的联合索引列no_nongroup_keypart_predicate见下面例子聚合函数(distinct column)字段是主键索引primary_key_is_clustered聚合函数列小于等于group by字段aggregate_column_not_suffix_in_idx见下面例子带有聚合函数的group by语句的条件不是聚合函数列之前一个列的等号条件non_equality_gap_attribute好比where c1>2 and c2>1 或者 where c1=2 and c2>1 见下面例子。而where c1>2 and c2=1是允许的带有聚合函数的group by语句且没有条件,聚合函数列大于group by的列no_nongroup_keypart_predicate见下面例子没有聚合函数但是有group by语句的条件带有非select和group by的条件列keypart_reference_from_where_clause好比where c1=1 or c3=1,见下面例子带有聚合函数的group by语句的sql涉及聚合函数之后的列keypart_after_infix_in_query见下面例子三、现实例子说明

接下来看几个例子来说明上面的代码。
  1. greatsql> explain SELECT c1,c2,MIN(c3) FROM t1 where c1>1 and c2=1  GROUP BY c1;
  2. +----+-------------+-------+------------+-------+---------------+-------+---------+------+------+----------+---------------------------------------+
  3. | id | select_type | table | partitions | type  | possible_keys | key   | key_len | ref  | rows | filtered | Extra                                 |
  4. +----+-------------+-------+------------+-------+---------------+-------+---------+------+------+----------+---------------------------------------+
  5. |  1 | SIMPLE      | t1    | NULL       | range | i1_t1         | i1_t1 | 15      | NULL |    2 |   100.00 | Using where; Using index for group-by |
  6. +----+-------------+-------+------------+-------+---------------+-------+---------+------+------+----------+---------------------------------------+
  7.                  "group_index_range": {
  8.                     "potential_group_range_indexes": [
  9.                       {
  10.                         "index": "i1_t1",
  11.                         "covering": true,
  12.                         "index_dives_for_eq_ranges": true,
  13.                         "ranges": [
  14.                           "1 < c1"
  15.                         ],
  16.                         "rows": 2, 用group index skip scan扫描满足条件c1>1 and c2=1的一共2条数据,计算见下面
  17.                         "cost": 0.55
  18.                       }
  19.                     ]
  20.                   },
  21.                   "best_group_range_summary": {
  22.                     "type": "index_group",
  23.                     "index": "i1_t1",
  24.                     "group_attribute": "c3",
  25.                     "min_aggregate": true,
  26.                     "max_aggregate": false,
  27.                     "distinct_aggregate": false,
  28.                     "rows": 2,
  29.                     "cost": 0.55,
  30.                     "key_parts_used_for_access": [
  31.                       "c1",
  32.                       "c2",
  33.                       "c3"
  34.                     ],
  35.                     "ranges": [
  36.                       "1 < c1"
  37.                     ],
  38.                     "chosen": true
  39.                   },
  40.                   "skip_scan_range": {
  41.                     "chosen": false,
  42.                     "cause": "has_group_by"
  43.                   },
  44.                   "analyzing_range_alternatives": {
  45.                     "range_scan_alternatives": [
  46.                       {
  47.                         "index": "i1_t1",
  48.                         "ranges": [
  49.                           "1 < c1"
  50.                         ],
  51.                         "index_dives_for_eq_ranges": true,
  52.                         "rowid_ordered": false,
  53.                         "using_mrr": false,
  54.                         "index_only": true,
  55.                         "in_memory": 1,
  56.                         "rows": 80, 单独用索引i1_t1满足1 < c1条件的有80条
  57.                         "cost": 8.31051,
  58.                         "chosen": false,
  59.                         "cause": "cost"
  60.                       }
  61.                     ],
  62.                     "analyzing_roworder_intersect": {
  63.                       "usable": false,
  64.                       "cause": "too_few_roworder_scans"
  65.                     }
  66.                   },
  67.                   "chosen_range_access_summary": {
  68.                     "range_access_plan": {
  69.                       "type": "index_group",
  70.                       "index": "i1_t1",
  71.                       "group_attribute": "c3",
  72.                       "min_aggregate": true,
  73.                       "max_aggregate": false,
  74.                       "distinct_aggregate": false,
  75.                       "rows": 2,
  76.                       "cost": 0.55,
  77.                       "key_parts_used_for_access": [
  78.                         "c1",
  79.                         "c2",
  80.                         "c3"
  81.                       ],
  82.                       "ranges": [
  83.                         "1 < c1"
  84.                       ]
  85.                     },
  86.                     "rows_for_plan": 2,
  87.                     "cost_for_plan": 0.55,
  88.                     "chosen": true
  89.                   }
  90.                 }
  91. /* 计算方式:
  92. table_records = 160
  93. keys_per_block = 391
  94. num_blocks = (table_records / keys_per_block) + 1 = 160 / 391 + 1 = 1
  95. keys_per_group = 80 -- group by字段分组后每组的行数
  96. num_groups = (table_records / keys_per_group) + 1 = 160 / 80 + 1 = 3
  97. quick_prefix_selectivity = quick_prefix_records / table_records = 80 / 160 = 0.5 -- 计算前缀选择系数
  98. num_groups = num_groups * quick_prefix_selectivity = 3 * 0.5 = 2 */
复制代码
下面看一下 distinct 的例子:
  1. greatsql> explain SELECT distinct c1,c2 from t1;
  2. +----+-------------+-------+------------+-------+---------------+-------+---------+------+------+----------+--------------------------+
  3. | id | select_type | table | partitions | type  | possible_keys | key   | key_len | ref  | rows | filtered | Extra                    |
  4. +----+-------------+-------+------------+-------+---------------+-------+---------+------+------+----------+--------------------------+
  5. |  1 | SIMPLE      | t1    | NULL       | range | i1_t1         | i1_t1 | 10      | NULL |   10 |   100.00 | Using index for group-by |
  6. +----+-------------+-------+------------+-------+---------------+-------+---------+------+------+----------+--------------------------+
  7.                  "group_index_range": {
  8.                     "distinct_query": true, -- 这里说明用到了distinct关键字
  9.                     "potential_group_range_indexes": [
  10.                       {
  11.                         "index": "i1_t1",
  12.                         "covering": true,
  13.                         "rows": 10,
  14.                         "cost": 1.75
  15.                       }
  16.                     ]
  17.                   },
  18.                   "best_group_range_summary": {
  19.                     "type": "index_group",
  20.                     "index": "i1_t1",
  21.                     "group_attribute": null,
  22.                     "min_aggregate": false,
  23.                     "max_aggregate": false,
  24.                     "distinct_aggregate": false,
  25.                     "rows": 10,
  26.                     "cost": 1.75,
  27.                     "key_parts_used_for_access": [
  28.                       "c1",
  29.                       "c2"
  30.                     ],
  31.                     "ranges": [
  32.                     ],
  33.                     "chosen": true
  34.                   },
  35. -- 当然也支持聚合函数+distinct+group by的组合。
  36. greatsql> explain SELECT c1,sum(distinct c2) from t1 group by c1;
  37. +----+-------------+-------+------------+-------+---------------+-------+---------+------+------+----------+--------------------------+
  38. | id | select_type | table | partitions | type  | possible_keys | key   | key_len | ref  | rows | filtered | Extra                    |
  39. +----+-------------+-------+------------+-------+---------------+-------+---------+------+------+----------+--------------------------+
  40. |  1 | SIMPLE      | t1    | NULL       | range | i1_t1         | i1_t1 | 10      | NULL |   10 |   100.00 | Using index for group-by |
  41. +----+-------------+-------+------------+-------+---------------+-------+---------+------+------+----------+--------------------------+
复制代码
下面例子都是对上面表格的例子:
  1. -- 查询条件既包含MIN/MAX列又包含非MIN/MAX列
  2. greatsql> explain SELECT c1, MIN(c2) FROM t1 where c3>1 or c2<6 GROUP BY c1;
  3. +----+-------------+-------+------------+-------+---------------+-------+---------+------+------+----------+--------------------------+
  4. | id | select_type | table | partitions | type  | possible_keys | key   | key_len | ref  | rows | filtered | Extra                    |
  5. +----+-------------+-------+------------+-------+---------------+-------+---------+------+------+----------+--------------------------+
  6. |  1 | SIMPLE      | t1    | NULL       | index | i1_t1         | i1_t1 | 15      | NULL |  160 |    55.55 | Using where; Using index |
  7. +----+-------------+-------+------------+-------+---------------+-------+---------+------+------+----------+--------------------------+
  8.                   "best_covering_index_scan": {
  9.                     "index": "i1_t1",
  10.                     "cost": 17.4066,
  11.                     "chosen": true
  12.                   },
  13.                   "setup_range_conditions": [
  14.                   ],
  15.                   "range_scan_possible": false,
  16.                   "cause": "condition_always_true",
  17.                   "group_index_range": {
  18.                     "chosen": false,
  19.                     "cause": "minmax_keypart_in_disjunctive_query"
  20.                   },
  21.                   "skip_scan_range": {
  22.                     "chosen": false,
  23.                     "cause": "has_group_by"
  24.                   }
  25. -- group by字段是联合索引的第一列
  26. greatsql> explain SELECT c2, MIN(c1) FROM t1 GROUP BY c2 ;
  27. +----+-------------+-------+------------+-------+---------------+-------+---------+------+------+----------+------------------------------+
  28. | id | select_type | table | partitions | type  | possible_keys | key   | key_len | ref  | rows | filtered | Extra                        |
  29. +----+-------------+-------+------------+-------+---------------+-------+---------+------+------+----------+------------------------------+
  30. |  1 | SIMPLE      | t1    | NULL       | index | i1_t1         | i1_t1 | 15      | NULL |  160 |   100.00 | Using index; Using temporary |
  31. +----+-------------+-------+------------+-------+---------------+-------+---------+------+------+----------+------------------------------+
  32.                   "group_index_range": {
  33.                     "potential_group_range_indexes": [
  34.                       {
  35.                         "index": "i1_t1",
  36.                         "covering": true,
  37.                         "usable": false,
  38.                         "cause": "group_attribute_not_prefix_in_index"
  39.                       }
  40.                     ]
  41.                   }
  42. -- 带有聚合函数的group by语句且没有条件,聚合函数列大于group by的列
  43. greatsql> explain SELECT c1, MIN(c3) FROM t1 GROUP BY c1;
  44. +----+-------------+-------+------------+-------+---------------+-------+---------+------+------+----------+-------------+
  45. | id | select_type | table | partitions | type  | possible_keys | key   | key_len | ref  | rows | filtered | Extra       |
  46. +----+-------------+-------+------------+-------+---------------+-------+---------+------+------+----------+-------------+
  47. |  1 | SIMPLE      | t1    | NULL       | index | i1_t1         | i1_t1 | 15      | NULL |  160 |   100.00 | Using index |
  48. +----+-------------+-------+------------+-------+---------------+-------+---------+------+------+----------+-------------+
  49.                   "group_index_range": {
  50.                     "potential_group_range_indexes": [
  51.                       {
  52.                         "index": "i1_t1",
  53.                         "covering": true,
  54.                         "usable": false,
  55.                         "cause": "no_nongroup_keypart_predicate"
  56.                       }
  57.                     ]
  58.                   },
  59. -- 带有聚合函数的group by语句的sql涉及聚合函数之后的列
  60. greatsql> explain SELECT c1,MIN(c2) FROM t1 where c3=2 GROUP BY c1;
  61. +----+-------------+-------+------------+-------+---------------+-------+---------+------+------+----------+--------------------------+
  62. | id | select_type | table | partitions | type  | possible_keys | key   | key_len | ref  | rows | filtered | Extra                    |
  63. +----+-------------+-------+------------+-------+---------------+-------+---------+------+------+----------+--------------------------+
  64. |  1 | SIMPLE      | t1    | NULL       | index | i1_t1         | i1_t1 | 15      | NULL |  160 |    10.00 | Using where; Using index |
  65. +----+-------------+-------+------------+-------+---------------+-------+---------+------+------+----------+--------------------------+
  66.                   "group_index_range": {
  67.                     "potential_group_range_indexes": [
  68.                       {
  69.                         "index": "i1_t1",
  70.                         "covering": true,
  71.                         "usable": false,
  72.                         "cause": "keypart_after_infix_in_query"
  73.                       }
  74.                     ]
  75.                   },
  76. -- 带有聚合函数的group by语句的条件不是聚合函数列之前一个列的等号条件
  77. greatsql> explain SELECT c1,c2,MIN(c3) FROM t1 where c3=2 GROUP BY c1;
  78. +----+-------------+-------+------------+-------+---------------+-------+---------+------+------+----------+--------------------------+
  79. | id | select_type | table | partitions | type  | possible_keys | key   | key_len | ref  | rows | filtered | Extra                    |
  80. +----+-------------+-------+------------+-------+---------------+-------+---------+------+------+----------+--------------------------+
  81. |  1 | SIMPLE      | t1    | NULL       | index | i1_t1         | i1_t1 | 15      | NULL |  160 |    10.00 | Using where; Using index |
  82. +----+-------------+-------+------------+-------+---------------+-------+---------+------+------+----------+--------------------------+
  83.                   "group_index_range": {
  84.                     "potential_group_range_indexes": [
  85.                       {
  86.                         "index": "i1_t1",
  87.                         "covering": true,
  88.                         "usable": false,
  89.                         "cause": "non_equality_gap_attribute"
  90.                       }
  91.                     ]
  92.                   },
  93.                   
  94. -- 聚合函数列小于等于group by字段
  95. greatsql> explain SELECT c1,c2,MIN(c2) FROM t1 where c1=2 and c2=2  GROUP BY c1,c2;
  96. +----+-------------+-------+------------+------+---------------+-------+---------+-------------+------+----------+-------------+
  97. | id | select_type | table | partitions | type | possible_keys | key   | key_len | ref         | rows | filtered | Extra       |
  98. +----+-------------+-------+------------+------+---------------+-------+---------+-------------+------+----------+-------------+
  99. |  1 | SIMPLE      | t1    | NULL       | ref  | i1_t1         | i1_t1 | 10      | const,const |   16 |   100.00 | Using index |
  100. +----+-------------+-------+------------+------+---------------+-------+---------+-------------+------+----------+-------------+
  101.                   "group_index_range": {
  102.                     "potential_group_range_indexes": [
  103.                       {
  104.                         "index": "i1_t1",
  105.                         "covering": true,
  106.                         "usable": false,
  107.                         "cause": "aggregate_column_not_suffix_in_idx"
  108.                       }
  109.                     ]
  110.                   },
  111. -- 带有聚合函数的group by语句且没有条件,聚合函数列大于group by的列
  112. greatsql> explain SELECT c1,c2,MIN(c3) FROM t1  GROUP BY c1;
  113. +----+-------------+-------+------------+-------+---------------+-------+---------+------+------+----------+-------------+
  114. | id | select_type | table | partitions | type  | possible_keys | key   | key_len | ref  | rows | filtered | Extra       |
  115. +----+-------------+-------+------------+-------+---------------+-------+---------+------+------+----------+-------------+
  116. |  1 | SIMPLE      | t1    | NULL       | index | i1_t1         | i1_t1 | 15      | NULL |  160 |   100.00 | Using index |
  117. +----+-------------+-------+------------+-------+---------------+-------+---------+------+------+----------+-------------+
  118.                   "group_index_range": {
  119.                     "potential_group_range_indexes": [
  120.                       {
  121.                         "index": "i1_t1",
  122.                         "covering": true,
  123.                         "usable": false,
  124.                         "cause": "no_nongroup_keypart_predicate"
  125.                       }
  126.                     ]
  127.                   },
  128. -- 没有聚合函数但是有group by语句的条件带有非select和group by的条件列
  129. greatsql> explain SELECT c1,c2 FROM t1 where c1=1 or c3=1 GROUP BY c1,c2;
  130. +----+-------------+-------+------------+-------+---------------+-------+---------+------+------+----------+--------------------------+
  131. | id | select_type | table | partitions | type  | possible_keys | key   | key_len | ref  | rows | filtered | Extra                    |
  132. +----+-------------+-------+------------+-------+---------------+-------+---------+------+------+----------+--------------------------+
  133. |  1 | SIMPLE      | t1    | NULL       | index | i1_t1         | i1_t1 | 15      | NULL |  160 |    55.00 | Using where; Using index |
  134. +----+-------------+-------+------------+-------+---------------+-------+---------+------+------+----------+--------------------------+
  135.                   "group_index_range": {
  136.                     "potential_group_range_indexes": [
  137.                       {
  138.                         "index": "i1_t1",
  139.                         "covering": true,
  140.                         "usable": false,
  141.                         "cause": "keypart_reference_from_where_clause"
  142.                       }
  143.                     ]
  144.                   },
复制代码
四、总结

从上面group index skip iscan的代码我们相识了组别跳跃扫描的利用条件和利用场景,以及利用规则,这个功能让一些涉及分组和聚合函数实行的时候直接读联合索引就可以很快得到分组数据,避免了无效列的读取,提高了效率,但sql利用限定比较多,因此创建联合索引和利用的时候要注意sql语句与联合索引的匹配才能用到这个功能。

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

使用道具 举报

0 个回复

倒序浏览

快速回复

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

本版积分规则

王海鱼

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表