BosCollege-SimpleDB-在查詢中使用索引

關(guān)鍵詞:索引惶我,SimpleDB,查詢計劃
Keyword: Index, SimpleDB, Query Plan

Author: Sixing Yan

在哪兒使用索引冠骄?

在SimpleDB-2中伪煤,執(zhí)行一個SQL查詢,將會為其創(chuàng)建一個查詢計劃(QueryPlan)凛辣,其中囊括了對一個基本SQL語句中的Project抱既,ProductSelection三個部分(見BasicQueryPlan.java)扁誓》辣茫可見阳堕,SimpleDB-2并不會主動使用索引完成查詢?nèi)蝿?wù)。所以择克,如果希望SimpleDB在查詢中使用索引恬总,則需要修改BasicQueryPlan中的creatQueryPlan方法,在其中加入IndexPlan相關(guān)內(nèi)容肚邢。

什么時候使用索引壹堰?

SimpleDB-2中實現(xiàn)(但未部署)了一個可以使用Index的SelectionPlan,顯然其中一處可以使用索引的地方就是Selection部分(Product部分也可以使用)骡湖。

如何使用索引贱纠?

簡單來說,就是替換SelectPlan成IndexSelectPlan响蕴。

For example, we use SimpleDB to execute SQL with indexing. Supposed there is a table course with an id attribute, the SQL is,

select cid from course where cid=10;

SimpleDB-2

在SimpleDB-2 中谆焊,我們需要修改地方是這里的"Step 3":

public class BasicQueryPlanner implements QueryPlanner {
   /**
    * Creates a query plan as follows.  It first takes
    * the product of all tables and views; it then selects on the predicate;
    * and finally it projects on the field list. 
    */
   public Plan createPlan(QueryData data, Transaction tx) {
      //Step 1: Create a plan for each mentioned table or view
      List<Plan> plans = new ArrayList<Plan>();
      for (String tblname : data.tables()) {
         String viewdef = SimpleDB.mdMgr().getViewDef(tblname, tx);
         if (viewdef != null) plans.add(SimpleDB.planner().createQueryPlan(viewdef, tx));
         else plans.add(new TablePlan(tblname, tx));
      }
      //Step 2: Create the product of all table plans
      Plan p = plans.remove(0);
      for (Plan nextplan : plans) p = new ProductPlan(p, nextplan);
      //Step 3: Add a selection plan for the predicate
      p = new SelectPlan(p, data.pred());
      //Step 4: Project on the field names
      p = new ProjectPlan(p, data.fields());
      return p;
   }
}

SimpleDB-3

使用索引時,一個需要考慮的問題是浦夷,where 中涉及的field是否有index辖试,以及查詢條件是否適合調(diào)用該index。例如劈狐,如果進(jìn)行一個等值查詢罐孝,那么使用基于Hash的索引可能比基于Tree的索引表現(xiàn)更改。

這里肥缔,首先檢查查詢?nèi)蝿?wù)中莲兢,涉及的field在其對應(yīng)的表上是否存在索引;其次檢查該查詢是否是“等值查詢”续膳,如果是改艇,則使用該索引。(這里貪婪地使用第一個滿足條件的索引)

public class IndexQueryPlanner implements QueryPlanner {
   public Plan createPlan(QueryData data, Transaction tx) {
      //Step 1: Create a plan for each mentioned table
      //Step 2: Create the product of all table plans
      //----above is the same to 2.0.1----

      //Step 3: Add a selection plan for the predicate
      Map<String, IndexInfo> indexInfoMap = SimpleDB.mdMgr().getIndexInfo(tblname, tx);
      Constant cst;
      IndexInfo ii;
      for (String fldname: indexInfoMap.keySet()){
        //check whether there exist condition like 'F=c', 
        //where 'F' is a fieldname, and 'c' is constant.
        cst = data.pred().equatesWithConstant(fldname);
        if (cst != null){
            //If yes, then check whether there exist indexing on 'F' 
            ii = indexInfoMap.get(fldname);
            //If yes, stop loop
            if (ii != null) break;
        } 
      }
      //use indexing or non-indexing to operate Selection
      if (ii != null) p = new IndexSelectPlan(p, ii, cst, tx);
      else p = new SelectPlan(p, data.pred());

      //----below is the same to v2.0.1----
      //Step 4: Project on the field names
   }
}

簡單來說坟岔,SimpleDB v3.0.0 改變了 exectueQuery的執(zhí)行方式谒兄,把其中的SelectPlan[v2.1.0] 替換成 IndexSelectPlan[v2.1.0]
SelectPlan的next()遍歷每一條tuple,用Predicate.isSatisfied(tuple)判斷其是否滿足where 里的限制
IndexSelectPlan使用上文中找到的 cst:Constant 變量炮车,使用 beforesearch(cst)方法找到 遍歷 的起始位置舵变,然后讀取這個索引上的所有tuple(直到next()為false)。這個索引的搜索避免了全表掃描的操作瘦穆。

SimpleDB-3.1

上一節(jié)中簡單地展示了如何在createQueryPlan中加入使用索引的操作纪隙。在面對使用場景中,我們可能不會簡單地只執(zhí)行針對等值查詢的索引(還有等域查詢)扛或;同時绵咱,我們還要考慮不同的操作符對應(yīng)不同的索引,其將對應(yīng)“等”查詢或者范圍查詢熙兔。

在IndexQueryPlan中悲伶,將"step3"更改成:

                //Step 3: Add a selection plan for the predicate
        IndexFinder ifder = new IndexFinder(data, tx);
        p = ifder.hasIndexInfo() ? new IndexSelectPlan(p, ifder.getIndexInfo(), ifder.getSearchKey(), tx) : new SelectPlan(p, data.pred());

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末艾恼,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子麸锉,更是在濱河造成了極大的恐慌钠绍,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,640評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件花沉,死亡現(xiàn)場離奇詭異柳爽,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)碱屁,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,254評論 3 395
  • 文/潘曉璐 我一進(jìn)店門磷脯,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人娩脾,你說我怎么就攤上這事赵誓。” “怎么了柿赊?”我有些...
    開封第一講書人閱讀 165,011評論 0 355
  • 文/不壞的土叔 我叫張陵俩功,是天一觀的道長。 經(jīng)常有香客問我闹瞧,道長绑雄,這世上最難降的妖魔是什么展辞? 我笑而不...
    開封第一講書人閱讀 58,755評論 1 294
  • 正文 為了忘掉前任奥邮,我火速辦了婚禮,結(jié)果婚禮上罗珍,老公的妹妹穿的比我還像新娘洽腺。我一直安慰自己,他們只是感情好覆旱,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,774評論 6 392
  • 文/花漫 我一把揭開白布蘸朋。 她就那樣靜靜地躺著,像睡著了一般扣唱。 火紅的嫁衣襯著肌膚如雪藕坯。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,610評論 1 305
  • 那天噪沙,我揣著相機(jī)與錄音炼彪,去河邊找鬼。 笑死正歼,一個胖子當(dāng)著我的面吹牛辐马,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播局义,決...
    沈念sama閱讀 40,352評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼喜爷,長吁一口氣:“原來是場噩夢啊……” “哼冗疮!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起檩帐,我...
    開封第一講書人閱讀 39,257評論 0 276
  • 序言:老撾萬榮一對情侶失蹤术幔,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后湃密,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體特愿,經(jīng)...
    沈念sama閱讀 45,717評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,894評論 3 336
  • 正文 我和宋清朗相戀三年勾缭,在試婚紗的時候發(fā)現(xiàn)自己被綠了揍障。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,021評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡俩由,死狀恐怖毒嫡,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情幻梯,我是刑警寧澤兜畸,帶...
    沈念sama閱讀 35,735評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站碘梢,受9級特大地震影響咬摇,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜煞躬,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,354評論 3 330
  • 文/蒙蒙 一肛鹏、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧恩沛,春花似錦在扰、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,936評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至搅裙,卻和暖如春皱卓,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背部逮。 一陣腳步聲響...
    開封第一講書人閱讀 33,054評論 1 270
  • 我被黑心中介騙來泰國打工娜汁, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人甥啄。 一個月前我還...
    沈念sama閱讀 48,224評論 3 371
  • 正文 我出身青樓存炮,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子穆桂,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,974評論 2 355

推薦閱讀更多精彩內(nèi)容