]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Fix a couple of problems in estimating the number of rows visited by a range query...
authordan <dan@noemail.net>
Fri, 27 Jun 2014 20:14:25 +0000 (20:14 +0000)
committerdan <dan@noemail.net>
Fri, 27 Jun 2014 20:14:25 +0000 (20:14 +0000)
FossilOrigin-Name: 219736f54dcd1448af3400e699f1c20755ac6876

manifest
manifest.uuid
src/vdbemem.c
src/where.c
test/skipscan5.test [new file with mode: 0644]

index 3a8265365f17af34bd3c647d2f36edde0303096a..14dfe69604b79afb35758dc95182a5479baedc19 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Fix\scompilation\sissue\swhen\sSTAT4\sis\snot\senabled.
-D 2014-06-26T21:32:09.075
+C Fix\sa\scouple\sof\sproblems\sin\sestimating\sthe\snumber\sof\srows\svisited\sby\sa\srange\squery\sthat\suses\sa\sskip-scan.
+D 2014-06-27T20:14:25.215
 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
 F Makefile.in b03432313a3aad96c706f8164fb9f5307eaf19f5
 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@ -289,14 +289,14 @@ F src/vdbeInt.h e6d83e5bfd62fc6685ba1ed6153f7099f82de9f7
 F src/vdbeapi.c 0ed6053f947edd0b30f64ce5aeb811872a3450a4
 F src/vdbeaux.c e493f38758c4b8f4ca2007cf6a700bd405d192f3
 F src/vdbeblob.c 9205ce9d3b064d9600f8418a897fc88b5687d9ac
-F src/vdbemem.c 8f28cb5bdd5b8748dba67aab5a07a47386fe40dc
+F src/vdbemem.c 3f191d4113805e3c5e875b732c44c9d98bac0595
 F src/vdbesort.c 44441d73b08b3a638dcdb725afffb87c6574ad27
 F src/vdbetrace.c 6f52bc0c51e144b7efdcfb2a8f771167a8816767
 F src/vtab.c 21b932841e51ebd7d075e2d0ad1415dce8d2d5fd
 F src/wal.c 264df50a1b33124130b23180ded2e2c5663c652a
 F src/wal.h df01efe09c5cb8c8e391ff1715cca294f89668a4
 F src/walker.c 11edb74d587bc87b33ca96a5173e3ec1b8389e45
-F src/where.c 0396f040a1ffe31b1f63673c6cd6c469c5e4ce57
+F src/where.c fea270aeef23f978edac94b5591c74b0bf3ce884
 F src/whereInt.h 929c1349b5355fd44f22cee5c14d72b3329c58a6
 F test/8_3_names.test ebbb5cd36741350040fd28b432ceadf495be25b2
 F test/aggerror.test a867e273ef9e3d7919f03ef4f0e8c0d2767944f2
@@ -829,6 +829,7 @@ F test/shrink.test 8c70f62b6e8eb4d54533de6d65bd06b1b9a17868
 F test/sidedelete.test f0ad71abe6233e3b153100f3b8d679b19a488329
 F test/skipscan1.test 28c7faa41a0d7265040ecb0a0abd90c0904270b2
 F test/skipscan2.test d1d1450952b7275f0b0a3a981f0230532743951a
+F test/skipscan5.test 7eb78ace4e7145fb5005d47a8f837701d7027b85
 F test/soak.test 0b5b6375c9f4110c828070b826b3b4b0bb65cd5f
 F test/softheap1.test 40562fe6cac6d9827b7b42b86d45aedf12c15e24
 F test/sort.test 0e4456e729e5a92a625907c63dcdedfbe72c5dc5
@@ -1180,7 +1181,7 @@ F tool/vdbe_profile.tcl 67746953071a9f8f2f668b73fe899074e2c6d8c1
 F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
 F tool/warnings.sh 0abfd78ceb09b7f7c27c688c8e3fe93268a13b32
 F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f
-P 01dc8102592427b71a18c2cb82301d2266dd59c2
-R 1ded9652c09c1b748cd5e7181ce5016c
-U mistachkin
-Z b89953846d49961fb37fc288882b8874
+P 74a5454a710e1b7d8575cec2f872e6110aefce17
+R 02edb14d4ef805eea40e69e5acdf39b7
+U dan
+Z bd7cb892a5b1d67344c0bf9079bc8afa
index 7e63e6d9db605c64c6f85e0dc5849906f14ddad0..d621593f3a049978d50f27eac119c7db36155696 100644 (file)
@@ -1 +1 @@
-74a5454a710e1b7d8575cec2f872e6110aefce17
\ No newline at end of file
+219736f54dcd1448af3400e699f1c20755ac6876
\ No newline at end of file
index 67dea9c282bfa846a650e07ce4eeee01dd9bceb6..634c881f2f4989f4a317a699e1ecc37f8424889d 100644 (file)
@@ -1296,7 +1296,7 @@ int sqlite3Stat4Column(
     iHdr = getVarint32(a, nHdr);
     iField = nHdr;
     for(i=0; i<iCol; i++){
-      iHdr = getVarint32(&a[iHdr], t);
+      iHdr += getVarint32(&a[iHdr], t);
       iField += sqlite3VdbeSerialTypeLen(t);
     }
 
index a8c8bccde4f9329fbe2c93a8275adb06a1b36e61..3cd566c46b98235fc8b540bfc1efe7ab2f5ef2a9 100644 (file)
@@ -2045,8 +2045,8 @@ static int whereRangeSkipScanEst(
   Index *p = pLoop->u.btree.pIndex;
   int nEq = pLoop->u.btree.nEq;
   sqlite3 *db = pParse->db;
-  int nLower = 0;
-  int nUpper = 0;
+  int nLower = -1;
+  int nUpper = p->nSample+1;
   int rc = SQLITE_OK;
   u8 aff = p->pTable->aCol[ p->aiColumn[nEq] ].affinity;
   CollSeq *pColl;
@@ -2058,9 +2058,11 @@ static int whereRangeSkipScanEst(
   pColl = sqlite3LocateCollSeq(pParse, p->azColl[nEq]);
   if( pLower ){
     rc = sqlite3Stat4ValueFromExpr(pParse, pLower->pExpr->pRight, aff, &p1);
+    nLower = 0;
   }
   if( pUpper && rc==SQLITE_OK ){
     rc = sqlite3Stat4ValueFromExpr(pParse, pUpper->pExpr->pRight, aff, &p2);
+    nUpper = p2 ? 0 : p->nSample;
   }
 
   if( p1 || p2 ){
@@ -2070,18 +2072,29 @@ static int whereRangeSkipScanEst(
       rc = sqlite3Stat4Column(db, p->aSample[i].p, p->aSample[i].n, nEq, &pVal);
       if( rc==SQLITE_OK && p1 ){
         int res = sqlite3MemCompare(p1, pVal, pColl);
-        if( res<=0 ) nLower++;
+        if( res>=0 ) nLower++;
       }
       if( rc==SQLITE_OK && p2 ){
         int res = sqlite3MemCompare(p2, pVal, pColl);
-        if( res<=0 ) nUpper++;
+        if( res>=0 ) nUpper++;
       }
     }
-    if( p2==0 ) nUpper = p->nSample;
     nDiff = (nUpper - nLower);
     if( nDiff<=0 ) nDiff = 1;
-    pLoop->nOut -= (sqlite3LogEst(p->nSample) - sqlite3LogEst(nDiff));
-    *pbDone = 1;
+
+    /* If there is both an upper and lower bound specified, and the 
+    ** comparisons indicate that they are close together, use the fallback
+    ** method (assume that the scan visits 1/64 of the rows) for estimating
+    ** the number of rows visited. Otherwise, estimate the number of rows
+    ** using the method described in the header comment for this function. */
+    if( nDiff!=1 || pUpper==0 || pLower==0 ){
+      int nAdjust = (sqlite3LogEst(p->nSample) - sqlite3LogEst(nDiff));
+      pLoop->nOut -= nAdjust;
+      *pbDone = 1;
+      WHERETRACE(0x10, ("range skip-scan regions: %u..%u  adjust=%d est=%d\n",
+                           (u32)nLower, (u32)nUpper, nAdjust*-1, pLoop->nOut));
+    }
+
   }else{
     assert( *pbDone==0 );
   }
diff --git a/test/skipscan5.test b/test/skipscan5.test
new file mode 100644 (file)
index 0000000..b374302
--- /dev/null
@@ -0,0 +1,95 @@
+# 2013-11-13
+#
+# The author disclaims copyright to this source code.  In place of
+# a legal notice, here is a blessing:
+#
+#    May you do good and not evil.
+#    May you find forgiveness for yourself and forgive others.
+#    May you share freely, never taking more than you give.
+#
+#***********************************************************************
+#
+# This file implements tests of the "skip-scan" query strategy. In 
+# particular it tests that stat4 data can be used by a range query
+# that uses the skip-scan approach.
+#
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+set testprefix skipscan5
+
+ifcapable !stat4 {
+  finish_test
+  return
+}
+
+do_execsql_test 1.1 {
+  CREATE TABLE t1(a INT, b INT, c INT);
+  CREATE INDEX i1 ON t1(a, b);
+} {}
+
+expr srand(4)
+do_test 1.2 {
+  for {set i 0} {$i < 100} {incr i} {
+    set a [expr int(rand()*4.0) + 1]
+    set b [expr int(rand()*20.0) + 1]
+    execsql { INSERT INTO t1 VALUES($a, $b, NULL) }
+  }
+  execsql ANALYZE
+} {}
+
+do_eqp_test 1.3 {
+  SELECT * FROM t1 WHERE b = 5;
+} {
+  0 0 0 {SEARCH TABLE t1 USING INDEX i1 (ANY(a) AND b=?)}
+}
+
+do_eqp_test 1.4 {
+  SELECT * FROM t1 WHERE b > 12 AND b < 16;
+} {
+  0 0 0 {SEARCH TABLE t1 USING INDEX i1 (ANY(a) AND b>? AND b<?)}
+}
+
+do_eqp_test 1.5 {
+  SELECT * FROM t1 WHERE b > 2 AND b < 16;
+} {
+  0 0 0 {SCAN TABLE t1}
+}
+
+do_eqp_test 1.6 {
+  SELECT * FROM t1 WHERE b > 18 AND b < 25;
+} {
+  0 0 0 {SEARCH TABLE t1 USING INDEX i1 (ANY(a) AND b>? AND b<?)}
+}
+
+do_eqp_test 1.7 {
+  SELECT * FROM t1 WHERE b > 18 AND b < 25;
+} {
+  0 0 0 {SEARCH TABLE t1 USING INDEX i1 (ANY(a) AND b>? AND b<?)}
+}
+
+do_eqp_test 1.8 {
+  SELECT * FROM t1 WHERE b > 15;
+} {
+  0 0 0 {SEARCH TABLE t1 USING INDEX i1 (ANY(a) AND b>?)}
+}
+
+do_eqp_test 1.9 {
+  SELECT * FROM t1 WHERE b > 5;
+} {
+  0 0 0 {SCAN TABLE t1}
+}
+
+do_eqp_test 1.10 {
+  SELECT * FROM t1 WHERE b < 5;
+} {
+  0 0 0 {SEARCH TABLE t1 USING INDEX i1 (ANY(a) AND b<?)}
+}
+
+do_eqp_test 1.11 {
+  SELECT * FROM t1 WHERE b < 15;
+} {
+  0 0 0 {SCAN TABLE t1}
+}
+
+finish_test