]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Use a binary search instead of a linear scan when comparing a sample key against...
authordan <dan@noemail.net>
Thu, 8 Aug 2013 16:17:12 +0000 (16:17 +0000)
committerdan <dan@noemail.net>
Thu, 8 Aug 2013 16:17:12 +0000 (16:17 +0000)
FossilOrigin-Name: e50dc30523210ba12324d5d8379503610f13aa34

manifest
manifest.uuid
src/where.c

index 2ecaf9c19f9af5cd102a0a2d89774b765918a0c1..f1338e178697d90f9cce3f2cc04d2a61f4f4ad81 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Fix\sa\ssegfault\sin\s"ALTER\sTABLE\st1\sADD\sCOLUMN\sb\sDEFAULT\s(-+1)".\sAlso\san\sassert()\sfailure\sthat\scould\soccur\sif\sSQLITE_ENABLE_STAT4\swere\snot\sdefined.
-D 2013-08-08T12:21:32.556
+C Use\sa\sbinary\ssearch\sinstead\sof\sa\slinear\sscan\swhen\scomparing\sa\ssample\skey\sagainst\sdata\sfrom\sthe\ssqlite_stat4\stable.
+D 2013-08-08T16:17:12.293
 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
 F Makefile.in 5e41da95d92656a5004b03d3576e8b226858a28e
 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@ -290,7 +290,7 @@ F src/vtab.c 2e8b489db47e20ae36cd247932dc671c9ded0624
 F src/wal.c 7dc3966ef98b74422267e7e6e46e07ff6c6eb1b4
 F src/wal.h df01efe09c5cb8c8e391ff1715cca294f89668a4
 F src/walker.c 4fa43583d0a84b48f93b1e88f11adf2065be4e73
-F src/where.c a14294548b55404e9f6c082c0e63bc6d24926c8d
+F src/where.c 2323663d074a94fa1609d65d7175948d490e560b
 F test/8_3_names.test ebbb5cd36741350040fd28b432ceadf495be25b2
 F test/aggerror.test a867e273ef9e3d7919f03ef4f0e8c0d2767944f2
 F test/aggnested.test 45c0201e28045ad38a530b5a144b73cd4aa2cfd6
@@ -1106,7 +1106,7 @@ F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
 F tool/warnings.sh fbc018d67fd7395f440c28f33ef0f94420226381
 F tool/wherecosttest.c f407dc4c79786982a475261866a161cd007947ae
 F tool/win/sqlite.vsix 97894c2790eda7b5bce3cc79cb2a8ec2fde9b3ac
-P f783938ea999731ea073cd2c78e278095f7bea6d
-R 5118870e9b33149ca23f1597a77ab495
+P 9fec3e38287067d60874530300fbeb602958c951
+R 05ac6975552968743dc313f6e2204120
 U dan
-Z e9558b330772b69c8c1b515706fc9347
+Z 09be040e1ce68baa088fb590dab65772
index 347dc903778d6369fd394003e6f3741f2081d362..d8543e7110cae4b33825ee728f3966afd89b1809 100644 (file)
@@ -1 +1 @@
-9fec3e38287067d60874530300fbeb602958c951
\ No newline at end of file
+e50dc30523210ba12324d5d8379503610f13aa34
\ No newline at end of file
index c2a5e81b3c7770b485638cde1b4d73a15c35ab80..a8559bce8674a92103e8d617594a50da94387ceb 100644 (file)
@@ -2415,25 +2415,47 @@ static int whereKeyStats(
   tRowcnt *aStat              /* OUT: stats written here */
 ){
   IndexSample *aSample = pIdx->aSample;
-  int i;
-  int isEq = 0;
-  int iCol = pRec->nField-1;
+  int iCol = pRec->nField-1;  /* Index of required stats in anEq[] etc. */
+  int iMin = 0;               /* Smallest sample not yet tested */
+  int i = pIdx->nSample;      /* Smallest sample larger than or equal to pRec */
+  int iTest;                  /* Next sample to test */
+  int res;                    /* Result of comparison operation */
 
+  assert( pIdx->nSample>0 );
   assert( pRec->nField>0 && iCol<pIdx->nColumn );
-  for(i=0; i<pIdx->nSample; i++){
-    int res = sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec);
-    if( res>=0 ){
-      isEq = (res==0);
-      break;
+  do{
+    iTest = (iMin+i)/2;
+    res = sqlite3VdbeRecordCompare(aSample[iTest].n, aSample[iTest].p, pRec);
+    if( res<0 ){
+      iMin = iTest+1;
+    }else{
+      i = iTest;
     }
+  }while( res && iMin<i );
+
+#ifdef SQLITE_DEBUG
+  /* The following assert statements check that the binary search code
+  ** above found the right answer. This block serves no purpose other
+  ** than to invoke the asserts.  */
+  if( res==0 ){
+    /* If (res==0) is true, then sample $i must be equal to pRec */
+    assert( i<pIdx->nSample );
+    assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec) );
+  }else{
+    /* Otherwise, pRec must be smaller than sample $i and larger than
+    ** sample ($i-1).  */
+    assert( i==pIdx->nSample 
+         || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)>0 );
+    assert( i==0
+         || sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0 );
   }
+#endif /* ifdef SQLITE_DEBUG */
 
   /* At this point, aSample[i] is the first sample that is greater than
   ** or equal to pVal.  Or if i==pIdx->nSample, then all samples are less
-  ** than pVal.  If aSample[i]==pVal, then isEq==1.
+  ** than pVal.  If aSample[i]==pVal, then res==0.
   */
-  if( isEq ){
-    assert( i<pIdx->nSample );
+  if( res==0 ){
     aStat[0] = aSample[i].anLt[iCol];
     aStat[1] = aSample[i].anEq[iCol];
   }else{