]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Adjust the query planner to keep track of the number of ORDER BY terms
authordrh <drh@noemail.net>
Tue, 18 Mar 2014 18:59:07 +0000 (18:59 +0000)
committerdrh <drh@noemail.net>
Tue, 18 Mar 2014 18:59:07 +0000 (18:59 +0000)
satisfied.  Still doesn't do anything with this information.  Some tests
fail after this check-in, but all failures are believed to be benign.  The
failures will be addressed at a later stage.

FossilOrigin-Name: 59d49b7fc49fa290e04a02653e7268c85836b27e

manifest
manifest.uuid
src/where.c

index 6f347f2fefa43b0b02298d22bf93d94cc20dde37..cf41f09f0dfd07d23effdea2e5502e6502a3548d 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Experiments\swith\sthe\soptimization\sof\sORDER\sBY\sand\sGROUP\sBY\sclauses.
-D 2014-03-18T15:30:27.660
+C Adjust\sthe\squery\splanner\sto\skeep\strack\sof\sthe\snumber\sof\sORDER\sBY\sterms\nsatisfied.\s\sStill\sdoesn't\sdo\sanything\swith\sthis\sinformation.\s\sSome\stests\nfail\safter\sthis\scheck-in,\sbut\sall\sfailures\sare\sbelieved\sto\sbe\sbenign.\s\sThe\nfailures\swill\sbe\saddressed\sat\sa\slater\sstage.
+D 2014-03-18T18:59:07.798
 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
 F Makefile.in 2ef13430cd359f7b361bb863504e227b25cc7f81
 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@ -291,7 +291,7 @@ F src/vtab.c 21b932841e51ebd7d075e2d0ad1415dce8d2d5fd
 F src/wal.c 76e7fc6de229bea8b30bb2539110f03a494dc3a8
 F src/wal.h df01efe09c5cb8c8e391ff1715cca294f89668a4
 F src/walker.c 11edb74d587bc87b33ca96a5173e3ec1b8389e45
-F src/where.c 222e6ed521c854887850e65a131d41fd59c28cf2
+F src/where.c 81eea5ced2a2586ccd7ebd1d5bed7d34f4a2e99c
 F src/whereInt.h daa3cdf9f56c1dd12b63e4eda3182e64e3bba14f
 F test/8_3_names.test ebbb5cd36741350040fd28b432ceadf495be25b2
 F test/aggerror.test a867e273ef9e3d7919f03ef4f0e8c0d2767944f2
@@ -1156,10 +1156,7 @@ F tool/vdbe_profile.tcl 67746953071a9f8f2f668b73fe899074e2c6d8c1
 F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
 F tool/warnings.sh d1a6de74685f360ab718efda6265994b99bbea01
 F tool/win/sqlite.vsix 030f3eeaf2cb811a3692ab9c14d021a75ce41fff
-P ecd9d3f9453be0bb8e312d8027fd1a9e55882f36
-R 8abedb680a805ab9b5e400a1ac18119b
-T *branch * orderby-planning
-T *sym-orderby-planning *
-T -sym-trunk *
+P b150902579d708b454efd5f8750e26a816f7f1a6
+R 179b4f0ee34acb2c0f356485d1e56bc3
 U drh
-Z 6c19349f1c38f37cb3b2bffb6095d2a0
+Z adae3618c374e493bb2483c0db46dcf7
index 26699b8468ebaeee91f9299680592b94e145a5e2..6d8c042ba1435f036875e5c38d0a7d61a84063db 100644 (file)
@@ -1 +1 @@
-b150902579d708b454efd5f8750e26a816f7f1a6
\ No newline at end of file
+59d49b7fc49fa290e04a02653e7268c85836b27e
\ No newline at end of file
index bed58ac75238bcc447724618ec6719a1ca356bff..a766def0356d2e1d5165fa0d6c96daaf3fde0250 100644 (file)
@@ -4916,7 +4916,13 @@ static i8 wherePathSatisfiesOrderBy(
     }
   } /* End the loop over all WhereLoops from outer-most down to inner-most */
   if( obSat==obDone ) return nOrderBy;
-  if( !isOrderDistinct ) return 0;
+  if( !isOrderDistinct ){
+    for(i=nOrderBy-1; i>0; i--){
+      Bitmask m = MASKBIT(i) - 1;
+      if( (obSat&m)==m ) return i;
+    }
+    return 0;
+  }
   return -1;
 }
 
@@ -4953,11 +4959,11 @@ static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){
   int iLoop;                /* Loop counter over the terms of the join */
   int ii, jj;               /* Loop counters */
   int mxI = 0;              /* Index of next entry to replace */
+  int nOrderBy;             /* Number of ORDER BY clause terms */
   LogEst rCost;             /* Cost of a path */
   LogEst nOut;              /* Number of outputs */
   LogEst mxCost = 0;        /* Maximum cost of a set of paths */
   LogEst mxOut = 0;         /* Maximum nOut value on the set of paths */
-  LogEst rSortCost;         /* Cost to do a sort */
   int nTo, nFrom;           /* Number of valid entries in aTo[] and aFrom[] */
   WherePath *aFrom;         /* All nFrom paths at the previous level */
   WherePath *aTo;           /* The nTo best paths at the current level */
@@ -4999,17 +5005,12 @@ static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){
 
   /* Precompute the cost of sorting the final result set, if the caller
   ** to sqlite3WhereBegin() was concerned about sorting */
-  rSortCost = 0;
   if( pWInfo->pOrderBy==0 || nRowEst==0 ){
     aFrom[0].isOrdered = 0;
+    nOrderBy = 0;
   }else{
-    /* TUNING: Estimated cost of sorting is 48*N*log2(N) where N is the
-    ** number of output rows. The 48 is the expected size of a row to sort. 
-    ** FIXME:  compute a better estimate of the 48 multiplier based on the
-    ** result set expressions. */
     aFrom[0].isOrdered = -1;
-    rSortCost = nRowEst + estLog(nRowEst);
-    WHERETRACE(0x002,("---- sort cost=%-3d\n", rSortCost));
+    nOrderBy = pWInfo->pOrderBy->nExpr;
   }
 
   /* Compute successively longer WherePaths using the previous generation
@@ -5034,7 +5035,16 @@ static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){
           isOrdered = wherePathSatisfiesOrderBy(pWInfo,
                        pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags,
                        iLoop, pWLoop, &revMask);
-          if( isOrdered==0 ){
+          if( isOrdered>=0 && isOrdered<nOrderBy ){
+            /* TUNING: Estimated cost of sorting cost as roughly 4*N*log(N).
+            ** If some but not all of the columns are in sorted order, then
+            ** scale down the log(N) term. */
+            LogEst rSortCost = 20 + nRowEst +
+                                estLog(nRowEst)*(nOrderBy-isOrdered)/nOrderBy;
+            WHERETRACE(0x002,
+               ("---- sort cost=%-3d (%d/%d) increases cost %3d to %-3d\n",
+                rSortCost, (nOrderBy-isOrdered), nOrderBy, rCost,
+                sqlite3LogEstAdd(rCost,rSortCost)));
             rCost = sqlite3LogEstAdd(rCost, rSortCost);
           }
         }else{