]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Multiply all cursor step cost estimates by the estimated size of the row in
authordrh <drh@noemail.net>
Mon, 7 Oct 2013 17:32:15 +0000 (17:32 +0000)
committerdrh <drh@noemail.net>
Mon, 7 Oct 2013 17:32:15 +0000 (17:32 +0000)
bytes, in order to get the query planner ot make use of estimated row sizes.
This check-in uses magic numbers in a few places (for example, estimates of
the size of output rows) and needs lots of refinement.  Consider this a
proof-of-concept only.

FossilOrigin-Name: cb34cfe57c2a664fbfae8106e95114400ea222d5

manifest
manifest.uuid
src/where.c

index 824d937e6444b9976c5851d9ca4c4475cf62a07f..623db16001a173f6573a5039c9043a37e70b9b5d 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Merge\sbug\sfixes\sfrom\strunk.
-D 2013-10-07T10:48:06.076
+C Multiply\sall\scursor\sstep\scost\sestimates\sby\sthe\sestimated\ssize\sof\sthe\srow\sin\nbytes,\sin\sorder\sto\sget\sthe\squery\splanner\sot\smake\suse\sof\sestimated\srow\ssizes.\nThis\scheck-in\suses\smagic\snumbers\sin\sa\sfew\splaces\s(for\sexample,\sestimates\sof\nthe\ssize\sof\soutput\srows)\sand\sneeds\slots\sof\srefinement.\s\sConsider\sthis\sa\nproof-of-concept\sonly.
+D 2013-10-07T17:32:15.338
 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 e9e593d5bb798c3e67fc3893dfe7055c9e7d8d74
-F src/where.c 72c6c205e53ae72d2d235705bb2b040d70fff2e2
+F src/where.c ad5e680c0b95014bb9e227990d6b0efe634e7749
 F test/8_3_names.test ebbb5cd36741350040fd28b432ceadf495be25b2
 F test/aggerror.test a867e273ef9e3d7919f03ef4f0e8c0d2767944f2
 F test/aggnested.test 45c0201e28045ad38a530b5a144b73cd4aa2cfd6
@@ -1121,7 +1121,7 @@ F tool/vdbe-compress.tcl f12c884766bd14277f4fcedcae07078011717381
 F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
 F tool/warnings.sh fbc018d67fd7395f440c28f33ef0f94420226381
 F tool/win/sqlite.vsix 030f3eeaf2cb811a3692ab9c14d021a75ce41fff
-P e9e932aa400f217e383cda9922fbde8a4356f57a 0aca31e1514b3df254c049b4251bcb199831681a
-R 58571132dec54639078eadde66ba0889
+P 1d7b2dc0eae70c0c0e523b715acf758bb4cfa9ac
+R b3d2afdb618d61f33659270b34382478
 U drh
-Z 412c711013eb3a5449cb7f9aec9f3c69
+Z 2fed1ec905830e828c488397bcac95d0
index eb89607625913526da3d275a9773bd196448f742..b45f4b6ece886bd3433f1ca6b0d50fcf2b115912 100644 (file)
@@ -1 +1 @@
-1d7b2dc0eae70c0c0e523b715acf758bb4cfa9ac
\ No newline at end of file
+cb34cfe57c2a664fbfae8106e95114400ea222d5
\ No newline at end of file
index 98af69b10c152fea9388726f6965328c0100981b..3cf09a81797920c5448f691ac5bb56f3f5d6ce49 100644 (file)
@@ -4368,7 +4368,7 @@ static int whereLoopAddBtreeIndex(
       pNew->rRun =  sqlite3LogEstAdd(pNew->rRun,rLogSize>27 ? rLogSize-17 : 10);
     }
     /* Step cost for each output row */
-    pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut);
+    pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut) + pProbe->szIdxRow;
     whereLoopOutputAdjust(pBuilder->pWC, pNew, pSrc->iCursor);
     rc = whereLoopInsert(pBuilder, pNew);
     if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0
@@ -4471,11 +4471,13 @@ static int whereLoopAddBtree(
   LogEst rSize;               /* number of rows in the table */
   LogEst rLogSize;            /* Logarithm of the number of rows in the table */
   WhereClause *pWC;           /* The parsed WHERE clause */
+  Table *pTab;                /* Table being queried */
   
   pNew = pBuilder->pNew;
   pWInfo = pBuilder->pWInfo;
   pTabList = pWInfo->pTabList;
   pSrc = pTabList->a + pNew->iTab;
+  pTab = pSrc->pTab;
   pWC = pBuilder->pWC;
   assert( !IsVirtual(pSrc->pTab) );
 
@@ -4493,8 +4495,8 @@ static int whereLoopAddBtree(
     sPk.aiColumn = &aiColumnPk;
     sPk.aiRowEst = aiRowEstPk;
     sPk.onError = OE_Replace;
-    sPk.pTable = pSrc->pTab;
-    aiRowEstPk[0] = pSrc->pTab->nRowEst;
+    sPk.pTable = pTab;
+    aiRowEstPk[0] = pTab->nRowEst;
     aiRowEstPk[1] = 1;
     pFirst = pSrc->pTab->pIndex;
     if( pSrc->notIndexed==0 ){
@@ -4504,7 +4506,7 @@ static int whereLoopAddBtree(
     }
     pProbe = &sPk;
   }
-  rSize = sqlite3LogEst(pSrc->pTab->nRowEst);
+  rSize = sqlite3LogEst(pTab->nRowEst);
   rLogSize = estLog(rSize);
 
 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
@@ -4530,12 +4532,13 @@ static int whereLoopAddBtree(
         ** approximately 7*N*log2(N) where N is the number of rows in
         ** the table being indexed. */
         pNew->rSetup = rLogSize + rSize + 28;  assert( 28==sqlite3LogEst(7) );
+        pNew->rSetup += pTab->szTabRow;
         /* TUNING: Each index lookup yields 20 rows in the table.  This
         ** is more than the usual guess of 10 rows, since we have no way
         ** of knowning how selective the index will ultimately be.  It would
         ** not be unreasonable to make this value much larger. */
         pNew->nOut = 43;  assert( 43==sqlite3LogEst(20) );
-        pNew->rRun = sqlite3LogEstAdd(rLogSize,pNew->nOut);
+        pNew->rRun = sqlite3LogEstAdd(rLogSize,pNew->nOut) + pTab->szTabRow;
         pNew->wsFlags = WHERE_AUTO_INDEX;
         pNew->prereq = mExtra | pTerm->prereqRight;
         rc = whereLoopInsert(pBuilder, pNew);
@@ -4570,7 +4573,7 @@ static int whereLoopAddBtree(
       /* TUNING: Cost of full table scan is 3*(N + log2(N)).
       **  +  The extra 3 factor is to encourage the use of indexed lookups
       **     over full scans.  FIXME */
-      pNew->rRun = sqlite3LogEstAdd(rSize,rLogSize) + 16;
+      pNew->rRun = sqlite3LogEstAdd(rSize,rLogSize) + 16 + pTab->szTabRow;
       whereLoopOutputAdjust(pWC, pNew, pSrc->iCursor);
       rc = whereLoopInsert(pBuilder, pNew);
       pNew->nOut = rSize;
@@ -4602,6 +4605,7 @@ static int whereLoopAddBtree(
           ** which we will simplify to just N*log2(N) */
           pNew->rRun = rSize + rLogSize;
         }
+        pNew->rRun += pProbe->szIdxRow;
         whereLoopOutputAdjust(pWC, pNew, pSrc->iCursor);
         rc = whereLoopInsert(pBuilder, pNew);
         pNew->nOut = rSize;
@@ -4773,7 +4777,7 @@ static int whereLoopAddVirtual(
       pNew->u.vtab.isOrdered = (u8)((pIdxInfo->nOrderBy!=0)
                                      && pIdxInfo->orderByConsumed);
       pNew->rSetup = 0;
-      pNew->rRun = sqlite3LogEstFromDouble(pIdxInfo->estimatedCost);
+      pNew->rRun = sqlite3LogEstFromDouble(pIdxInfo->estimatedCost) + 55;
       /* TUNING: Every virtual table query returns 25 rows */
       pNew->nOut = 46;  assert( 46==sqlite3LogEst(25) );
       whereLoopInsert(pBuilder, pNew);
@@ -5264,7 +5268,7 @@ static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){
   }else{
     /* TUNING: Estimated cost of sorting is N*log2(N) where N is the
     ** number of output rows. */
-    rSortCost = nRowEst + estLog(nRowEst);
+    rSortCost = nRowEst + estLog(nRowEst) + 55;
     WHERETRACE(0x002,("---- sort cost=%-3d\n", rSortCost));
   }