]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Simplify the computation of Index.aAvgEq. analyze-worst-case
authordrh <drh@noemail.net>
Fri, 4 Mar 2016 19:55:02 +0000 (19:55 +0000)
committerdrh <drh@noemail.net>
Fri, 4 Mar 2016 19:55:02 +0000 (19:55 +0000)
FossilOrigin-Name: c448873006cda294cdaf6b61e75a07f121ba34ea

manifest
manifest.uuid
src/analyze.c
test/skipscan2.test

index 0f7618522692f28caaa1689c1465cf192706b03d..e730d6e1862259ad87c27a5060a5be92ff32f641 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Merge\schanges\sfrom\strunk.
-D 2016-03-04T18:45:12.274
+C Simplify\sthe\scomputation\sof\sIndex.aAvgEq.
+D 2016-03-04T19:55:02.239
 F Makefile.in ead489fe5d3313b8a2a8d5f3710173a4b50dd2e0
 F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434
 F Makefile.msc d68db8ddac21ea2576ea5b446f12cf9ebe897a03
@@ -286,7 +286,7 @@ F sqlite.pc.in 42b7bf0d02e08b9e77734a47798d1a55a9e0716b
 F sqlite3.1 fc7ad8990fc8409983309bb80de8c811a7506786
 F sqlite3.pc.in 48fed132e7cb71ab676105d2a4dc77127d8c1f3a
 F src/alter.c 1bb0709b3048e24217b80ec6bd78a3e99a47c01b
-F src/analyze.c dac2f49d4a622091df1964a9ec9de1d2cd0584bb
+F src/analyze.c a89554ee91a8b17b8a745770ddcb4e85a1e27a69
 F src/attach.c a3724c64de1099d85e30751213d285752aed9505
 F src/auth.c b56c78ebe40a2110fd361379f7e8162d23f92240
 F src/backup.c f60f0aa55d25d853ffde53d0b0370a7bb7ee41ce
@@ -1032,7 +1032,7 @@ F test/show_speedtest1_rtree.tcl 32e6c5f073d7426148a6936a0408f4b5b169aba5
 F test/shrink.test 1b4330b1fd9e818c04726d45cb28db73087535ce
 F test/sidedelete.test f0ad71abe6233e3b153100f3b8d679b19a488329
 F test/skipscan1.test d37a75b4be4eb9dedeb69b4f38b1d0a74b5021d7
-F test/skipscan2.test 9fb533b9b66d33d5036fa86bbad1c1ae20861a3f
+F test/skipscan2.test 4cf572db43ca6b2f3b8162295e984c3555e4abeb
 F test/skipscan3.test ec5bab3f81c7038b43450e7b3062e04a198bdbb5
 F test/skipscan5.test 67817a4b6857c47e0e33ba3e506da6f23ef68de2
 F test/skipscan6.test 5866039d03a56f5bd0b3d172a012074a1d90a15b
@@ -1453,7 +1453,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
 F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P 21bfd47c4245846b12aeeb7cf0212529e300b878 cb9302cca423de41305719a49208daa392ec09da
-R 636b0403188c11b543ab3d2b973ee377
+P 5294c977d9b15a4f29028782ef253329bd739fc0
+R 0d9736ab8ee345d5b31279fe6ba1cf6e
 U drh
-Z 61c960fff1efcb39fcf10879f7f27e05
+Z 7c24b575cc7f48aed8f15dbed80ebac8
index c6b79347aab32931a8c255c24e133d37c20d8113..df77854bb9a36d2311d756337bea509a3339fb65 100644 (file)
@@ -1 +1 @@
-5294c977d9b15a4f29028782ef253329bd739fc0
\ No newline at end of file
+c448873006cda294cdaf6b61e75a07f121ba34ea
\ No newline at end of file
index b2b85d3d71b9814c833c35ae6658350c9578451c..687e0fa2464699a5c157487a72a158bf96451c5b 100644 (file)
@@ -1604,52 +1604,45 @@ void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
 */
 static void initAvgEq(Index *pIdx){
   if( pIdx ){
-    IndexSample *aSample = pIdx->aSample;
-    IndexSample *pFinal = &aSample[pIdx->nSample-1];
-    int iCol;
-    int nCol = 1;
-    if( pIdx->nSampleCol>1 ){
+    int nSample = pIdx->nSample;                /* Number of samples */
+    int nCol = pIdx->nSampleCol;                /* Number of columns sampled */
+    IndexSample *aSample = pIdx->aSample;       /* The samples */
+    IndexSample *pFinal = &aSample[nSample-1];  /* The last sample */
+    int iCol;                                   /* Loop counter of columns */
+    if( nCol>1 ){
       /* If this is stat4 data, then calculate aAvgEq[] values for all
       ** sample columns except the last. The last is always set to 1, as
       ** once the trailing PK fields are considered all index keys are
       ** unique.  */
-      nCol = pIdx->nSampleCol-1;
+      nCol--;
       pIdx->aAvgEq[nCol] = 1;
     }
     for(iCol=0; iCol<nCol; iCol++){
-      int nSample = pIdx->nSample;
       int i;                    /* Used to iterate through samples */
+      u32 nDSample = 0;         /* Number of distinct samples less than pFinal*/
       tRowcnt sumEq = 0;        /* Sum of the nEq values */
-      tRowcnt avgEq = 0;
+      tRowcnt avgEq;            /* Average repeats for unsampled entries */
       tRowcnt nRow;             /* Number of rows in index */
-      i64 nSum100 = 0;          /* Number of terms contributing to sumEq */
-      i64 nDist100;             /* Number of distinct values in index */
+      tRowcnt prevLt = 0;       /* Number of values less than previous sample */
+      tRowcnt thisLt;           /* Number of values less than current sample */
 
       if( !pIdx->aiRowEst || iCol>=pIdx->nKeyCol || pIdx->aiRowEst[iCol+1]==0 ){
-        nRow = pFinal->anLt[iCol];
-        nDist100 = (i64)100 * pFinal->anDLt[iCol];
-        nSample--;
+        nRow = pFinal->anLt[iCol] + pFinal->anEq[iCol];
       }else{
         nRow = pIdx->aiRowEst[0];
-        nDist100 = ((i64)100 * pIdx->aiRowEst[0]) / pIdx->aiRowEst[iCol+1];
       }
       pIdx->nRowEst0 = nRow;
-
-      /* Set nSum to the number of distinct (iCol+1) field prefixes that
-      ** occur in the stat4 table for this index. Set sumEq to the sum of 
-      ** the nEq values for column iCol for the same set (adding the value 
-      ** only once where there exist duplicate prefixes).  */
-      for(i=0; i<nSample; i++){
-        if( i==(pIdx->nSample-1)
-         || aSample[i].anDLt[iCol]!=aSample[i+1].anDLt[iCol] 
-        ){
+      for(i=0; (thisLt = aSample[i].anLt[iCol])<pFinal->anLt[iCol]; i++){
+        if( i==0 || thisLt!=prevLt ){
           sumEq += aSample[i].anEq[iCol];
-          nSum100 += 100;
+          nDSample++;
         }
+        prevLt = thisLt;
       }
-
-      if( nDist100>nSum100 ){
-        avgEq = ((i64)100 * (nRow - sumEq))/(nDist100 - nSum100);
+      if( pFinal->anDLt[iCol] > nDSample ){
+        avgEq = (pFinal->anLt[iCol] - sumEq)/(pFinal->anDLt[iCol] - nDSample);
+      }else{
+        avgEq = 1;
       }
       if( avgEq==0 ) avgEq = 1;
       pIdx->aAvgEq[iCol] = avgEq;
index 35ca8269ab3214fd30f773477c20010a1a5025ee..da20a69bacbe9148dcc1d2ac58234567e0f2073f 100644 (file)
@@ -48,14 +48,6 @@ do_execsql_test skipscan2-1.2 {
   INSERT INTO people VALUES('Robert','student',159);
   INSERT INTO people VALUES('Sally','student',166);
   INSERT INTO people VALUES('Tom','student',171);
-/*
-  INSERT INTO people VALUES('Ursula','student',170);
-  INSERT INTO people VALUES('Vance','student',179);
-  INSERT INTO people VALUES('Willma','student',175);
-  INSERT INTO people VALUES('Xavier','teacher',185);
-  INSERT INTO people VALUES('Yvonne','student',149);
-  INSERT INTO people VALUES('Zach','student',170);
-*/
 }
 
 # Without ANALYZE, a skip-scan is not used
@@ -110,7 +102,7 @@ do_execsql_test skipscan2-1.8 {
   INSERT INTO people VALUES('Ursula','student',170);
   ANALYZE;
   SELECT stat FROM sqlite_stat1 WHERE idx='people_idx1';
-} {{21 18 3}}
+} {{21 13 2}}
 db cache flush
 do_execsql_test skipscan2-1.9 {
   SELECT name FROM people WHERE height<=161 ORDER BY +name;
@@ -160,7 +152,7 @@ do_execsql_test skipscan2-2.5 {
 do_execsql_test skipscan2-2.5eqp {
   EXPLAIN QUERY PLAN
   SELECT name FROM peoplew WHERE height<=161 ORDER BY +name;
-} {/*INDEX peoplew_idx1 */}
+} {/*INDEX peoplew_idx1*/}
 
 # A skip-scan on a PK index of a WITHOUT ROWID table.
 #