diff options
author | drh <> | 2021-05-04 23:21:35 +0000 |
---|---|---|
committer | drh <> | 2021-05-04 23:21:35 +0000 |
commit | 67656ac78a819446d6ce47fb9c7c96f188415a3c (patch) | |
tree | 25181b135677ef33fd786375d0bac1195ef8d365 /src | |
parent | 23634898c5835e5523331652702c75a25fa55de5 (diff) | |
download | sqlite-67656ac78a819446d6ce47fb9c7c96f188415a3c.tar.gz sqlite-67656ac78a819446d6ce47fb9c7c96f188415a3c.zip |
When applying the optimization that disables WHERE clause terms that drive
indexes, make sure not to do so if the term being disabled is a transitive
constraint. Fix for the problem identified by
[forum:forumpost/eb8613976a|forum post eb8613976a].
FossilOrigin-Name: f1f9b5de3c59489b94963685660b3ddc45eece5535b02fec399b6ece0e38563d
Diffstat (limited to 'src')
-rw-r--r-- | src/where.c | 1 | ||||
-rw-r--r-- | src/whereInt.h | 1 | ||||
-rw-r--r-- | src/wherecode.c | 17 |
3 files changed, 18 insertions, 1 deletions
diff --git a/src/where.c b/src/where.c index d051464ab..62bf5fbbc 100644 --- a/src/where.c +++ b/src/where.c @@ -2646,6 +2646,7 @@ static int whereLoopAddBtreeIndex( pNew->wsFlags |= WHERE_UNQ_WANTED; } } + if( scan.iEquiv>1 ) pNew->wsFlags |= WHERE_TRANSCONS; }else if( eOp & WO_ISNULL ){ pNew->wsFlags |= WHERE_COLUMN_NULL; }else if( eOp & (WO_GT|WO_GE) ){ diff --git a/src/whereInt.h b/src/whereInt.h index 0bc43bf88..c5dd83cc8 100644 --- a/src/whereInt.h +++ b/src/whereInt.h @@ -603,5 +603,6 @@ void sqlite3WhereTabFuncArgs(Parse*, SrcItem*, WhereClause*); #define WHERE_IN_EARLYOUT 0x00040000 /* Perhaps quit IN loops early */ #define WHERE_BIGNULL_SORT 0x00080000 /* Column nEq of index is BIGNULL */ #define WHERE_IN_SEEKSCAN 0x00100000 /* Seek-scan optimization for IN */ +#define WHERE_TRANSCONS 0x00200000 /* Uses a transitive constraint */ #endif /* !defined(SQLITE_WHEREINT_H) */ diff --git a/src/wherecode.c b/src/wherecode.c index 157596db4..05c0a61bc 100644 --- a/src/wherecode.c +++ b/src/wherecode.c @@ -620,7 +620,22 @@ static int codeEqualityTerm( sqlite3DbFree(pParse->db, aiMap); #endif } - disableTerm(pLevel, pTerm); + + /* As an optimization, try to disable the WHERE clause term that is + ** driving the index as it will always be true. The correct answer is + ** obtained regardless, but we might get the answer with fewer CPU cycles + ** by omitting the term. + ** + ** But do not disable the term unless we are certain that the term is + ** not a transitive constraint. For an example of where that does not + ** work, see https://sqlite.org/forum/forumpost/eb8613976a (2021-05-04) + */ + if( (pLevel->pWLoop->wsFlags & WHERE_TRANSCONS)==0 + || (pTerm->eOperator & WO_EQUIV)==0 + ){ + disableTerm(pLevel, pTerm); + } + return iReg; } |