浅谈SQLite——查询处理及优化
2010-06-25 00:00:00 来源:WEB开发网而(2)中的opcode在以下几行代码完成:
notReady = ~(Bitmask)0;
for(i=0; i<nTabList; i++){
//核心代码,从最外层向最内层,为每一层循环生成opcode
notReady = codeOneLoopStart(pWInfo, i, wctrlFlags, notReady);
pWInfo->iContinue = pWInfo->a[i].addrCont;
}
4、codeOneLoopStart
该函数根据优化分析得到的结果生成每层循环的opcode。
static Bitmask codeOneLoopStart(
WhereInfo *pWInfo, /* Complete information about the WHERE clause */
int iLevel, /* Which level of pWInfo->a[] should be coded */
u16 wctrlFlags, /* One of the WHERE_* flags defined in sqliteInt.h */
Bitmask notReady /* Which tables are currently available */
)
codeOneLoopStart针对5种不同的查询策略,生成各自不同的opcode:
if( pLevel->plan.wsFlags & WHERE_ROWID_EQ ){ //rowid的等值查询
...
}else if( pLevel->plan.wsFlags & WHERE_ROWID_RANGE ){//rowid的范围查询
...
//使用索引的等值/范围查询
}else if( pLevel->plan.wsFlags & (WHERE_COLUMN_RANGE|WHERE_COLUMN_EQ) ){
...
}if( pLevel->plan.wsFlags & WHERE_MULTI_OR ){//or
...
}else{ //全表扫描
...
}
先看全表扫描:
1 static const u8 aStep[] = { OP_Next, OP_Prev };
2 static const u8 aStart[] = { OP_Rewind, OP_Last };
3 pLevel->op = aStep[bRev];
4 pLevel->p1 = iCur;
5 pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk); //生成OP_Rewind/OP_Last指令
6 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
7
更多精彩
赞助商链接