From b50596d64dad5fe8c2677ec9587d9c8568f0955e Mon Sep 17 00:00:00 2001
From: drh <drh@noemail.net>
Date: Tue, 8 Oct 2013 20:42:41 +0000
Subject: [PATCH] Rollback some of the previous changes in the branch such that
 the estimated row sizes are now only used as a tie-breaker for index scans.

FossilOrigin-Name: 65553ff34b41e54d129ff2fee96be714105503c4
---
 manifest          | 16 ++++++++--------
 manifest.uuid     |  2 +-
 src/where.c       | 28 +++++++++++-----------------
 test/select1.test |  2 +-
 test/where9.test  | 22 +++++++++++++++++++++-
 5 files changed, 42 insertions(+), 28 deletions(-)

diff --git a/manifest b/manifest
index 70da98f212..e1fb3ff867 100644
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Use\s#ifdefs\sto\somit\sunused\scode\sin\sthe\scolumnType()\sroutine\sdepending\son\ncompile-time\soptions.
-D 2013-10-08T20:01:35.742
+C Rollback\ssome\sof\sthe\sprevious\schanges\sin\sthe\sbranch\ssuch\sthat\sthe\sestimated\nrow\ssizes\sare\snow\sonly\sused\sas\sa\stie-breaker\sfor\sindex\sscans.
+D 2013-10-08T20:42:41.270
 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 2a04ab5856f8c964369d181f3580a8d14e4a325c
+F src/where.c 8dd4cb208b9b70beeb9da7dbcd9b8b8b08261ed7
 F test/8_3_names.test ebbb5cd36741350040fd28b432ceadf495be25b2
 F test/aggerror.test a867e273ef9e3d7919f03ef4f0e8c0d2767944f2
 F test/aggnested.test 45c0201e28045ad38a530b5a144b73cd4aa2cfd6
@@ -761,7 +761,7 @@ F test/schema4.test e6a66e20cc69f0e306667c08be7fda3d11707dc5
 F test/schema5.test 0103e4c0313b3725b5ae5600bdca53006ab53db3
 F test/securedel.test 87a2561151af1f1e349071a89fdd77059f50113c
 F test/securedel2.test 2d54c28e46eb1fd6902089958b20b1b056c6f1c5
-F test/select1.test deba017eed9daa5af33de868676c997e7eebb931
+F test/select1.test fc2a61f226a649393664ad54bc5376631801517c
 F test/select2.test 352480e0e9c66eda9c3044e412abdf5be0215b56
 F test/select3.test 2ce595f8fb8e2ac10071d3b4e424cadd4634a054
 F test/select4.test 00179be44e531fe04c1c3f15df216439dff2519d
@@ -1059,7 +1059,7 @@ F test/where6.test 5da5a98cec820d488e82708301b96cb8c18a258b
 F test/where7.test 5a4b0abc207d71da4deecd734ad8579e8dd40aa8
 F test/where8.test 6f95896633cf2d307b5263145b942b7d33e837c6
 F test/where8m.test da346596e19d54f0aba35ebade032a7c47d79739
-F test/where9.test 06c5f1e13168239951d2426078334d97fdf2d26f
+F test/where9.test 4f3eab951353a3ae164befc521c777dfa903e46c
 F test/whereA.test 4d253178d135ec46d1671e440cd8f2b916aa6e6b
 F test/whereB.test 0def95db3bdec220a731c7e4bec5930327c1d8c5
 F test/whereC.test d6f4ecd4fa2d9429681a5b22a25d2bda8e86ab8a
@@ -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 18bd6ba96d19de6047baebfa15b1f739577c9ec4
-R 967a79399191741c9ec3babe410d2a81
+P 3fd5e33217a91402b3499fa0977469b91618a928
+R 2e607623289a6e77a66e8d2d68d4002e
 U drh
-Z f2e723eade28ed7288d4d048e8169b82
+Z 42356aefb8b584494e71f168c94cd126
diff --git a/manifest.uuid b/manifest.uuid
index 1c171677f7..ee437d26db 100644
--- a/manifest.uuid
+++ b/manifest.uuid
@@ -1 +1 @@
-3fd5e33217a91402b3499fa0977469b91618a928
\ No newline at end of file
+65553ff34b41e54d129ff2fee96be714105503c4
\ No newline at end of file
diff --git a/src/where.c b/src/where.c
index b576d4f2b7..545dee2d29 100644
--- a/src/where.c
+++ b/src/where.c
@@ -4338,12 +4338,6 @@ static int whereLoopAddBtreeIndex(
       /* Adjust nOut and rRun for STAT3 range values */
       assert( pNew->nOut==saved_nOut );
       whereRangeScanEst(pParse, pBuilder, pBtm, pTop, pNew);
-
-      /* If the range constraint is the only constraint on the index and
-      ** if the range constraint does not reduce the search space,
-      ** then this is really just an index scan which has already
-      ** been analyzed. */
-      if( pNew->nOut>=saved_nOut && pNew->u.btree.nEq==0 ) continue;
     }
 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
     if( nInMul==0 
@@ -4374,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) + pProbe->szIdxRow;
+    pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut);
     whereLoopOutputAdjust(pBuilder->pWC, pNew, pSrc->iCursor);
     rc = whereLoopInsert(pBuilder, pNew);
     if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0
@@ -4538,13 +4532,12 @@ 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) + pTab->szTabRow;
+        pNew->rRun = sqlite3LogEstAdd(rLogSize,pNew->nOut);
         pNew->wsFlags = WHERE_AUTO_INDEX;
         pNew->prereq = mExtra | pTerm->prereqRight;
         rc = whereLoopInsert(pBuilder, pNew);
@@ -4579,7 +4572,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 + pTab->szTabRow;
+      pNew->rRun = sqlite3LogEstAdd(rSize,rLogSize) + 16;
       whereLoopOutputAdjust(pWC, pNew, pSrc->iCursor);
       rc = whereLoopInsert(pBuilder, pNew);
       pNew->nOut = rSize;
@@ -4592,6 +4585,7 @@ static int whereLoopAddBtree(
       if( b
        || ( m==0
          && pProbe->bUnordered==0
+         && pProbe->szIdxRow<pTab->szTabRow
          && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0
          && sqlite3GlobalConfig.bUseCis
          && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan)
@@ -4600,18 +4594,18 @@ static int whereLoopAddBtree(
         pNew->iSortIdx = b ? iSortIdx : 0;
         if( m==0 ){
           /* TUNING: Cost of a covering index scan is K*(N + log2(N)).
-          **  +  The extra factor K of between 1.1 (iScanRatio between 0
-          **     and 9) and 2.8 (iScanRatio between 126 and 127) is added
-          **     to encourage the use of indexed lookups.  FIXME
+          **  +  The extra factor K of between 1.1 and 3.0 that depends
+          **     on the relative sizes of the table and the index.  K
+          **     is smaller for smaller indices, thus favoring them.
           */
-          pNew->rRun = sqlite3LogEstAdd(rSize,rLogSize) + 10;
+          pNew->rRun = sqlite3LogEstAdd(rSize,rLogSize) + 1 +
+                        (15*pProbe->szIdxRow)/pTab->szTabRow;
         }else{
           assert( b!=0 ); 
           /* TUNING: Cost of scanning a non-covering index is (N+1)*log2(N)
           ** 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;
@@ -4783,7 +4777,7 @@ static int whereLoopAddVirtual(
       pNew->u.vtab.isOrdered = (u8)((pIdxInfo->nOrderBy!=0)
                                      && pIdxInfo->orderByConsumed);
       pNew->rSetup = 0;
-      pNew->rRun = sqlite3LogEstFromDouble(pIdxInfo->estimatedCost) + 55;
+      pNew->rRun = sqlite3LogEstFromDouble(pIdxInfo->estimatedCost);
       /* TUNING: Every virtual table query returns 25 rows */
       pNew->nOut = 46;  assert( 46==sqlite3LogEst(25) );
       whereLoopInsert(pBuilder, pNew);
@@ -5276,7 +5270,7 @@ static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){
     ** 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. */
-    rSortCost = nRowEst + estLog(nRowEst) + 55;
+    rSortCost = nRowEst + estLog(nRowEst);
     WHERETRACE(0x002,("---- sort cost=%-3d\n", rSortCost));
   }
 
diff --git a/test/select1.test b/test/select1.test
index 852b52e284..875c87c84a 100644
--- a/test/select1.test
+++ b/test/select1.test
@@ -542,7 +542,7 @@ do_test select1-6.9.7 {
   set x [execsql2 {
      SELECT * FROM test1 a, (select 5, 6) LIMIT 1
   }]
-  regsub -all {subquery_[0-9a-fA-F]+_} $x {subquery} x
+  regsub -all {sq_[0-9a-fA-F_]+} $x {subquery} x
   set x
 } {a.f1 11 a.f2 22 sqlite_subquery.5 5 sqlite_subquery.6 6}
 do_test select1-6.9.8 {
diff --git a/test/where9.test b/test/where9.test
index 963d3bc1b7..1c32ff82f7 100644
--- a/test/where9.test
+++ b/test/where9.test
@@ -781,7 +781,27 @@ do_test where9-6.8.2 {
         OR (b NOT NULL AND c NOT NULL AND d IS NULL)
   }
 } {1 {no query solution}}
-if {1} {
+ifcapable stat4||stat3 {
+  # When STAT3 is enabled, the "b NOT NULL" terms get translated
+  # into b>NULL, which can be satified by the index t1b.  It is a very
+  # expensive way to do the query, but it works, and so a solution is possible.
+  do_test where9-6.8.3-stat4 {
+    catchsql {
+      UPDATE t1 INDEXED BY t1b SET a=a+100
+       WHERE (b IS NULL AND c NOT NULL AND d NOT NULL)
+          OR (b NOT NULL AND c IS NULL AND d NOT NULL)
+          OR (b NOT NULL AND c NOT NULL AND d IS NULL)
+    }
+  } {0 {}}
+  do_test where9-6.8.4-stat4 {
+    catchsql {
+      DELETE FROM t1 INDEXED BY t1b
+       WHERE (b IS NULL AND c NOT NULL AND d NOT NULL)
+          OR (b NOT NULL AND c IS NULL AND d NOT NULL)
+          OR (b NOT NULL AND c NOT NULL AND d IS NULL)
+    }
+  } {0 {}}
+} else {
   do_test where9-6.8.3 {
     catchsql {
       UPDATE t1 INDEXED BY t1b SET a=a+100