SQLite Forum

index misuse?
Login
I insert two lines of code into the function `whereLoopAddBtreeIndex` before it invokes the function `ApplyCostMultiplier`, which reduces the cost when the index t1_c0 is used to optimize the constraint on t1.c1. Therefore, SQLite will use index t1_c0 to optimize the constraints on t1.c1.

```c 
function whereLoopAddBtreeIndex()
{
    ...
    //the following two lines are add by myself
    if(pProbe->aiColumn[saved_nEq] != pTerm->u.x.leftColumn)
      pNew->rRun = sqlite3LogEstAdd(pNew->rRun -16, pNew->nOut - 16);

    ApplyCostMultiplier(pNew->rRun, pProbe->pTable->costMult);

    nOutUnadjusted = pNew->nOut;
    pNew->rRun += nInMul + nIn;
    pNew->nOut += nInMul + nIn;
    whereLoopOutputAdjust(pBuilder->pWC, pNew, rSize);
    rc = whereLoopInsert(pBuilder, pNew);
    ...
}
```

For WHERE clause "x=y and x=1", it is reasonable to use the index on "y" for constraints on x, since "x=y and x=1" is equivalent with "x=y and y=1".
SQLite seems to  transform "x=1" into "y=1"and keep "x=y" unchanged.
However, as for WHERE clause "x=y, x=z", changing "x=z" to "x=y" and keeping "x=y" unchanged result in incorrect optimization("x=y and x=y").
Looking forward to your early reply!