1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-11-18 10:21:03 +03:00

When stat4 information is available, try to use it to improve the truth

probability of WHERE clause terms that do not participate in the index.

FossilOrigin-Name: 1babd6ec5d60e2c34aa1c0285ead768a88004218468e97262411973fe3487022
This commit is contained in:
drh
2020-02-22 16:58:49 +00:00
parent e8c4f03266
commit 89efac94fd
4 changed files with 60 additions and 16 deletions

View File

@@ -2307,7 +2307,10 @@ static void whereLoopOutputAdjust(
}else{
k = 20;
}
if( iReduce<k ) iReduce = k;
if( iReduce<k ){
pTerm->wtFlags |= TERM_HEURTRUTH;
iReduce = k;
}
}
}
}
@@ -2489,9 +2492,9 @@ static int whereLoopAddBtreeIndex(
}
if( IsUniqueIndex(pProbe) && saved_nEq==pProbe->nKeyCol-1 ){
pBuilder->bldFlags |= SQLITE_BLDF_UNIQUE;
pBuilder->bldFlags1 |= SQLITE_BLDF1_UNIQUE;
}else{
pBuilder->bldFlags |= SQLITE_BLDF_INDEXED;
pBuilder->bldFlags1 |= SQLITE_BLDF1_INDEXED;
}
pNew->wsFlags = saved_wsFlags;
pNew->u.btree.nEq = saved_nEq;
@@ -2656,6 +2659,21 @@ static int whereLoopAddBtreeIndex(
if( rc!=SQLITE_OK ) break; /* Jump out of the pTerm loop */
if( nOut ){
pNew->nOut = sqlite3LogEst(nOut);
if( nEq==1 && pTerm->truthProb>0 ){
#if WHERETRACE_ENABLED /* 0x01 */
if( sqlite3WhereTrace & 0x01 ){
sqlite3DebugPrintf("Update truthProb from %d to %d:\n",
pTerm->truthProb, pNew->nOut - pProbe->aiRowLogEst[0]);
sqlite3WhereTermPrint(pTerm, 999);
}
#endif
pTerm->truthProb = pNew->nOut - pProbe->aiRowLogEst[0];
if( pTerm->wtFlags & TERM_HEURTRUTH ){
/* If the old heuristic truthProb was previously used, signal
** that all loops will need to be recomputed */
pBuilder->bldFlags2 |= SQLITE_BLDF2_2NDPASS;
}
}
if( pNew->nOut>saved_nOut ) pNew->nOut = saved_nOut;
pNew->nOut -= nIn;
}
@@ -3080,9 +3098,9 @@ static int whereLoopAddBtree(
}
}
pBuilder->bldFlags = 0;
pBuilder->bldFlags1 = 0;
rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 0);
if( pBuilder->bldFlags==SQLITE_BLDF_INDEXED ){
if( pBuilder->bldFlags1==SQLITE_BLDF1_INDEXED ){
/* If a non-unique index is used, or if a prefix of the key for
** unique index is used (making the index functionally non-unique)
** then the sqlite_stat1 data becomes important for scoring the
@@ -4838,6 +4856,25 @@ WhereInfo *sqlite3WhereBegin(
if( nTabList!=1 || whereShortCut(&sWLB)==0 ){
rc = whereLoopAddAll(&sWLB);
if( rc ) goto whereBeginError;
#ifdef SQLITE_ENABLE_STAT4
/* If one or more WhereTerm.truthProb values were used in estimating
** loop parameters, but then those truthProb values were subsequently
** changed based on STAT4 information while computing subsequent loops,
** then we need to rerun the whole loop building process so that all
** loops will be built using the revised truthProb values. */
if( sWLB.bldFlags2 & SQLITE_BLDF2_2NDPASS ){
WHERETRACE(0xffff,
("**** Redo all loop computations due to truthProb changes ****\n"));
while( pWInfo->pLoops ){
WhereLoop *p = pWInfo->pLoops;
pWInfo->pLoops = p->pNextLoop;
whereLoopDelete(db, p);
}
rc = whereLoopAddAll(&sWLB);
if( rc ) goto whereBeginError;
}
#endif
#ifdef WHERETRACE_ENABLED
if( sqlite3WhereTrace ){ /* Display all of the WhereLoop objects */