1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-11-12 13:01:09 +03:00

Improvements to likelihood processing so that commuting an unindexed term in

the WHERE clause does not change the query plan.

FossilOrigin-Name: 6e6bded055cdbc902731687c86d92c39a3ba5904
This commit is contained in:
drh
2013-09-11 17:39:09 +00:00
parent 09328c00d6
commit 7d9e7d8287
4 changed files with 39 additions and 23 deletions

View File

@@ -1,5 +1,5 @@
C Additional\sunlikely()\stest\scases.\s\sLogic\stweaks\sto\ssupport\stest\scoverage. C Improvements\sto\slikelihood\sprocessing\sso\sthat\scommuting\san\sunindexed\sterm\sin\nthe\sWHERE\sclause\sdoes\snot\schange\sthe\squery\splan.
D 2013-09-11T14:34:58.423 D 2013-09-11T17:39:09.434
F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
F Makefile.in 5e41da95d92656a5004b03d3576e8b226858a28e F Makefile.in 5e41da95d92656a5004b03d3576e8b226858a28e
F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@ -290,7 +290,7 @@ F src/vtab.c 5a423b042eb1402ef77697d03d6a67378d97bc8d
F src/wal.c 7dc3966ef98b74422267e7e6e46e07ff6c6eb1b4 F src/wal.c 7dc3966ef98b74422267e7e6e46e07ff6c6eb1b4
F src/wal.h df01efe09c5cb8c8e391ff1715cca294f89668a4 F src/wal.h df01efe09c5cb8c8e391ff1715cca294f89668a4
F src/walker.c 4fa43583d0a84b48f93b1e88f11adf2065be4e73 F src/walker.c 4fa43583d0a84b48f93b1e88f11adf2065be4e73
F src/where.c 74beca71522c95dde151a379cef6badb168a49de F src/where.c 47a9d554aa12e37d00c87ed6557edff3f96d1980
F test/8_3_names.test ebbb5cd36741350040fd28b432ceadf495be25b2 F test/8_3_names.test ebbb5cd36741350040fd28b432ceadf495be25b2
F test/aggerror.test a867e273ef9e3d7919f03ef4f0e8c0d2767944f2 F test/aggerror.test a867e273ef9e3d7919f03ef4f0e8c0d2767944f2
F test/aggnested.test 45c0201e28045ad38a530b5a144b73cd4aa2cfd6 F test/aggnested.test 45c0201e28045ad38a530b5a144b73cd4aa2cfd6
@@ -1059,7 +1059,7 @@ F test/whereC.test d6f4ecd4fa2d9429681a5b22a25d2bda8e86ab8a
F test/whereD.test 6c2feb79ef1f68381b07f39017fe5f9b96da8d62 F test/whereD.test 6c2feb79ef1f68381b07f39017fe5f9b96da8d62
F test/whereE.test b3a055eef928c992b0a33198a7b8dc10eea5ad2f F test/whereE.test b3a055eef928c992b0a33198a7b8dc10eea5ad2f
F test/whereF.test 5b2ba0dbe8074aa13e416b37c753991f0a2492d7 F test/whereF.test 5b2ba0dbe8074aa13e416b37c753991f0a2492d7
F test/whereG.test 5e6954236069469b939d3e0ccaf09dc38dc8320b F test/whereG.test 2a3d5181decc801b36600fa1c40b0dad2ccc267f
F test/wherelimit.test 5e9fd41e79bb2b2d588ed999d641d9c965619b31 F test/wherelimit.test 5e9fd41e79bb2b2d588ed999d641d9c965619b31
F test/wild001.test bca33f499866f04c24510d74baf1e578d4e44b1c F test/wild001.test bca33f499866f04c24510d74baf1e578d4e44b1c
F test/win32lock.test 7a6bd73a5dcdee39b5bb93e92395e1773a194361 F test/win32lock.test 7a6bd73a5dcdee39b5bb93e92395e1773a194361
@@ -1112,7 +1112,7 @@ F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
F tool/warnings.sh fbc018d67fd7395f440c28f33ef0f94420226381 F tool/warnings.sh fbc018d67fd7395f440c28f33ef0f94420226381
F tool/wherecosttest.c f407dc4c79786982a475261866a161cd007947ae F tool/wherecosttest.c f407dc4c79786982a475261866a161cd007947ae
F tool/win/sqlite.vsix 97894c2790eda7b5bce3cc79cb2a8ec2fde9b3ac F tool/win/sqlite.vsix 97894c2790eda7b5bce3cc79cb2a8ec2fde9b3ac
P 29a359b8d7f90e6fa2b28ce2a112284fd3870494 P 5d00cce74a7aefaf30022ae971ab1e0451e0ad6e
R 93dee01cdf95f7285a0f10ba394514d9 R aedc9ce3e88fcb8ddfac4258d6222c86
U drh U drh
Z 1aa243de8c46c2869ae531db3feb1a86 Z 4fd6b6c258c46144822c202b8357c6fb

View File

@@ -1 +1 @@
5d00cce74a7aefaf30022ae971ab1e0451e0ad6e 6e6bded055cdbc902731687c86d92c39a3ba5904

View File

@@ -689,9 +689,7 @@ static int whereClauseInsert(WhereClause *pWC, Expr *p, u8 wtFlags){
pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]); pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]);
} }
pTerm = &pWC->a[idx = pWC->nTerm++]; pTerm = &pWC->a[idx = pWC->nTerm++];
if( wtFlags & TERM_VIRTUAL ){ if( p && ExprHasAnyProperty(p, EP_Hint) ){
pTerm->truthProb = 0;
}else if( ALWAYS(p) && ExprHasAnyProperty(p, EP_Hint) ){
pTerm->truthProb = whereCost(p->iTable) - 99; pTerm->truthProb = whereCost(p->iTable) - 99;
}else{ }else{
pTerm->truthProb = -1; pTerm->truthProb = -1;
@@ -4286,24 +4284,24 @@ whereLoopInsert_noop:
** reduces the number of output rows by sqrt(2). ** reduces the number of output rows by sqrt(2).
*/ */
static void whereLoopOutputAdjust(WhereClause *pWC, WhereLoop *pLoop, int iCur){ static void whereLoopOutputAdjust(WhereClause *pWC, WhereLoop *pLoop, int iCur){
WhereTerm *pTerm; WhereTerm *pTerm, *pX;
Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf); Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf);
int x = 0; int i, j;
int i;
if( !OptimizationEnabled(pWC->pWInfo->pParse->db, SQLITE_AdjustOutEst) ){ if( !OptimizationEnabled(pWC->pWInfo->pParse->db, SQLITE_AdjustOutEst) ){
return; return;
} }
for(i=pWC->nTerm, pTerm=pWC->a; i>0; i--, pTerm++){ for(i=pWC->nTerm, pTerm=pWC->a; i>0; i--, pTerm++){
if( (pTerm->wtFlags & TERM_VIRTUAL)!=0 ) continue; if( (pTerm->wtFlags & TERM_VIRTUAL)!=0 ) break;
if( (pTerm->prereqAll & pLoop->maskSelf)==0 ) continue; if( (pTerm->prereqAll & pLoop->maskSelf)==0 ) continue;
if( (pTerm->prereqAll & notAllowed)!=0 ) continue; if( (pTerm->prereqAll & notAllowed)!=0 ) continue;
x += pTerm->truthProb; for(j=pLoop->nLTerm-1; j>=0; j--){
pX = pLoop->aLTerm[j];
if( pX==pTerm ) break;
if( pX->iParent>=0 && (&pWC->a[pX->iParent])==pTerm ) break;
}
if( j<0 ) pLoop->nOut += pTerm->truthProb;
} }
for(i=pLoop->nLTerm-1; i>=0; i--){
x -= pLoop->aLTerm[i]->truthProb;
}
if( x<0 ) pLoop->nOut += x;
} }
/* /*
@@ -5426,9 +5424,7 @@ static int wherePathSolver(WhereInfo *pWInfo, WhereCost nRowEst){
} }
} }
if( jj>=nTo ){ if( jj>=nTo ){
if( nTo>=mxChoice if( nTo>=mxChoice && rCost>=mxCost ){
&& (rCost>mxCost || (rCost==mxCost && nOut>=mxOut))
){
#ifdef WHERETRACE_ENABLED #ifdef WHERETRACE_ENABLED
if( sqlite3WhereTrace&0x4 ){ if( sqlite3WhereTrace&0x4 ){
sqlite3DebugPrintf("Skip %s cost=%-3d,%3d order=%c\n", sqlite3DebugPrintf("Skip %s cost=%-3d,%3d order=%c\n",

View File

@@ -147,4 +147,24 @@ do_test whereG-2.3 {
} }
} {1 {second argument to likelihood() must be a constant between 0.0 and 1.0}} } {1 {second argument to likelihood() must be a constant between 0.0 and 1.0}}
# Commuting a term of the WHERE clause should not change the query plan
#
do_execsql_test whereG-3.0 {
CREATE TABLE a(a1 PRIMARY KEY, a2);
CREATE TABLE b(b1 PRIMARY KEY, b2);
} {}
do_eqp_test whereG-3.1 {
SELECT * FROM a, b WHERE b1=a1 AND a2=5;
} {/.*SCAN TABLE a.*SEARCH TABLE b USING INDEX .*b_1 .b1=..*/}
do_eqp_test whereG-3.2 {
SELECT * FROM a, b WHERE a1=b1 AND a2=5;
} {/.*SCAN TABLE a.*SEARCH TABLE b USING INDEX .*b_1 .b1=..*/}
do_eqp_test whereG-3.3 {
SELECT * FROM a, b WHERE a2=5 AND b1=a1;
} {/.*SCAN TABLE a.*SEARCH TABLE b USING INDEX .*b_1 .b1=..*/}
do_eqp_test whereG-3.4 {
SELECT * FROM a, b WHERE a2=5 AND a1=b1;
} {/.*SCAN TABLE a.*SEARCH TABLE b USING INDEX .*b_1 .b1=..*/}
finish_test finish_test