OpenGauss 之索引查找和匹配

一. 前言

       本文主要通过走读OpenGuass的代码,来了解查询的时候OpenGuass是如何查找表的索引信息以及根据谓词条件过滤掉无用的索引信息的。

二. 索引路径匹配流程

     1.  首先OpenGuass在build_simple_rel的时候,首先将一个表以及与他相关的索引都加到rel->indexlist中,后续再筛选。主要代码如下所示:

build_simple_rel
​   switch (rte->rtekind) {
​       case RTE_RELATION:
​           get_relation_info
​               List* indexoidlist = RelationGetIndexList(relation)   // 到pg_index系统表中查找与relation相关的索引
​                    while (HeapTupleIsValid(htup = systable_getnext(indscan))) {
​                        result = insert_ordered_oid(result, index->indexrelid)   // result保存着索引的oid
​                    }
​              indexinfos = lcons(info, indexinfos)  // 无条件保留与一个表有关的所有索引
​              rel->indexlist = indexinfos
   }

        2. 再在create_index_paths中,根据列的过滤条件对用不上的索引进行剔除,主要代码如下:

create_index_paths
​    foreach (lc, rel->indexlist) {
​        match_restriction_clauses_to_index(&rclauseset);// 如果此索引跟谓词条件无关,则剔除此索引
​        bitindexpaths = list_concat(bitindexpaths, indexpaths);
​        add_path(root, rel, (Path*)bpath);   // 为索引建立起路径,建立的索引路径也会和其他路径进行代价对比取舍
​    }

         3. 保留下来的索引将计算代码,和顺序扫描等其他路径进行代码比较,再决定是否使用。

三. BitmapOrPath

      当查询条件带有or条件的时候,OpenGuass会生成一个BitmapOrPath将各个索引及其过滤条件连接起来,如对于select * from t1 where id = 1 or id = 2语句,会通过BitmapOr将多个条件应用到索引再通过BitmapOrPath连接起来,如下所示:

     

    其实现过程主要如下所示:

generate_bitmap_or_paths
​    foreach (j, ((BoolExpr*)rinfo->orclause)->args) {      // 逐个遍历or的所有条件
​        orargs = list_make1(orarg);
​        indlist = build_paths_for_OR
​            indexpaths = build_index_paths    // 用or其中一个条件构建一个index path
​        pathlist = lappend(pathlist, bitmapqual)  // 将当前or的其中一个条件保存到pathlist中
​    }
​    bitmapqual = create_bitmap_or_path(pathlist)    // 将pathlist组装成BitmapOrPath路径
  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值