]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Improvements to likelihood processing so that commuting an unindexed term in
authordrh <drh@noemail.net>
Wed, 11 Sep 2013 17:39:09 +0000 (17:39 +0000)
committerdrh <drh@noemail.net>
Wed, 11 Sep 2013 17:39:09 +0000 (17:39 +0000)
the WHERE clause does not change the query plan.

FossilOrigin-Name: 6e6bded055cdbc902731687c86d92c39a3ba5904

manifest
manifest.uuid
src/where.c
test/whereG.test

index 9e53d5e7f40c25bca36641c12eb119f84ce1e1ed..8e7446d805f65422475ba64e6f9420fb7dc2ae39 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Additional\sunlikely()\stest\scases.\s\sLogic\stweaks\sto\ssupport\stest\scoverage.
-D 2013-09-11T14:34:58.423
+C Improvements\sto\slikelihood\sprocessing\sso\sthat\scommuting\san\sunindexed\sterm\sin\nthe\sWHERE\sclause\sdoes\snot\schange\sthe\squery\splan.
+D 2013-09-11T17:39:09.434
 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
 F Makefile.in 5e41da95d92656a5004b03d3576e8b226858a28e
 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@ -290,7 +290,7 @@ F src/vtab.c 5a423b042eb1402ef77697d03d6a67378d97bc8d
 F src/wal.c 7dc3966ef98b74422267e7e6e46e07ff6c6eb1b4
 F src/wal.h df01efe09c5cb8c8e391ff1715cca294f89668a4
 F src/walker.c 4fa43583d0a84b48f93b1e88f11adf2065be4e73
-F src/where.c 74beca71522c95dde151a379cef6badb168a49de
+F src/where.c 47a9d554aa12e37d00c87ed6557edff3f96d1980
 F test/8_3_names.test ebbb5cd36741350040fd28b432ceadf495be25b2
 F test/aggerror.test a867e273ef9e3d7919f03ef4f0e8c0d2767944f2
 F test/aggnested.test 45c0201e28045ad38a530b5a144b73cd4aa2cfd6
@@ -1059,7 +1059,7 @@ F test/whereC.test d6f4ecd4fa2d9429681a5b22a25d2bda8e86ab8a
 F test/whereD.test 6c2feb79ef1f68381b07f39017fe5f9b96da8d62
 F test/whereE.test b3a055eef928c992b0a33198a7b8dc10eea5ad2f
 F test/whereF.test 5b2ba0dbe8074aa13e416b37c753991f0a2492d7
-F test/whereG.test 5e6954236069469b939d3e0ccaf09dc38dc8320b
+F test/whereG.test 2a3d5181decc801b36600fa1c40b0dad2ccc267f
 F test/wherelimit.test 5e9fd41e79bb2b2d588ed999d641d9c965619b31
 F test/wild001.test bca33f499866f04c24510d74baf1e578d4e44b1c
 F test/win32lock.test 7a6bd73a5dcdee39b5bb93e92395e1773a194361
@@ -1112,7 +1112,7 @@ F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
 F tool/warnings.sh fbc018d67fd7395f440c28f33ef0f94420226381
 F tool/wherecosttest.c f407dc4c79786982a475261866a161cd007947ae
 F tool/win/sqlite.vsix 97894c2790eda7b5bce3cc79cb2a8ec2fde9b3ac
-P 29a359b8d7f90e6fa2b28ce2a112284fd3870494
-R 93dee01cdf95f7285a0f10ba394514d9
+P 5d00cce74a7aefaf30022ae971ab1e0451e0ad6e
+R aedc9ce3e88fcb8ddfac4258d6222c86
 U drh
-Z 1aa243de8c46c2869ae531db3feb1a86
+Z 4fd6b6c258c46144822c202b8357c6fb
index a7dc88fb9b429ad94a8e61cb5211ab48e97a25da..b142052f40c1a424fae4719fe570642a87961003 100644 (file)
@@ -1 +1 @@
-5d00cce74a7aefaf30022ae971ab1e0451e0ad6e
\ No newline at end of file
+6e6bded055cdbc902731687c86d92c39a3ba5904
\ No newline at end of file
index 540ab075e6fdcd7c6c9952527a6489a85231c7e7..86863451fb938b114613e5960e7a22124f54697a 100644 (file)
@@ -689,9 +689,7 @@ static int whereClauseInsert(WhereClause *pWC, Expr *p, u8 wtFlags){
     pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]);
   }
   pTerm = &pWC->a[idx = pWC->nTerm++];
-  if( wtFlags & TERM_VIRTUAL ){
-    pTerm->truthProb = 0;
-  }else if( ALWAYS(p) && ExprHasAnyProperty(p, EP_Hint) ){
+  if( p && ExprHasAnyProperty(p, EP_Hint) ){
     pTerm->truthProb = whereCost(p->iTable) - 99;
   }else{
     pTerm->truthProb = -1;
@@ -4286,24 +4284,24 @@ whereLoopInsert_noop:
 ** reduces the number of output rows by sqrt(2).
 */
 static void whereLoopOutputAdjust(WhereClause *pWC, WhereLoop *pLoop, int iCur){
-  WhereTerm *pTerm;
+  WhereTerm *pTerm, *pX;
   Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf);
-  int x = 0;
-  int i;
+  int i, j;
 
   if( !OptimizationEnabled(pWC->pWInfo->pParse->db, SQLITE_AdjustOutEst) ){
     return;
   }
   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 & notAllowed)!=0 ) continue;
-    x += pTerm->truthProb;
-  }
-  for(i=pLoop->nLTerm-1; i>=0; i--){
-    x -= pLoop->aLTerm[i]->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;
   }
-  if( x<0 ) pLoop->nOut += x;
 }
 
 /*
@@ -5426,9 +5424,7 @@ static int wherePathSolver(WhereInfo *pWInfo, WhereCost nRowEst){
           }
         }
         if( jj>=nTo ){
-          if( nTo>=mxChoice 
-           && (rCost>mxCost || (rCost==mxCost && nOut>=mxOut))
-          ){
+          if( nTo>=mxChoice && rCost>=mxCost ){
 #ifdef WHERETRACE_ENABLED
             if( sqlite3WhereTrace&0x4 ){
               sqlite3DebugPrintf("Skip   %s cost=%-3d,%3d order=%c\n",
index 38dfeb07bc557671a7f76cf5f5fc5bfc722c8c6d..1b6a861855bbafc0d37d5e4f4d46bc45e70ebe57 100644 (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}}
 
+# 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