diff --git a/manifest b/manifest index 26fa848aca..6a2746f92b 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Fix\sa\scompiler\swarning\swhen\sSTAT2\sis\soff.\s\sMore\stest\scases.\s\sFix\slegacy\stests\nto\sdeal\swith\sthe\snew\sSTAT2\slogic. -D 2011-08-06T01:22:50.386 +C Merge\stogether\sthe\sfork\sin\sthe\squery-planner-tweaks\sbranch. +D 2011-08-06T02:03:10.239 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f F Makefile.in 1e6988b3c11dee9bd5edc0c804bd4468d74a9cdc F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23 @@ -118,7 +118,7 @@ F sqlite.pc.in 42b7bf0d02e08b9e77734a47798d1a55a9e0716b F sqlite3.1 6be1ad09113570e1fc8dcaff84c9b0b337db5ffc F sqlite3.pc.in ae6f59a76e862f5c561eb32a380228a02afc3cad F src/alter.c ac80a0f31189f8b4a524ebf661e47e84536ee7f5 -F src/analyze.c b1df43e5452fc6bcb14a6503c67bfcbdffddc2fe +F src/analyze.c 19894783b6f833337ae7e56001c96dc3795d2a83 F src/attach.c 12c6957996908edc31c96d7c68d4942c2474405f F src/auth.c 523da7fb4979469955d822ff9298352d6b31de34 F src/backup.c 986c15232757f2873dff35ee3b35cbf935fc573c @@ -183,7 +183,7 @@ F src/select.c d219c4b68d603cc734b6f9b1e2780fee12a1fa0d F src/shell.c bbe7818ff5bc8614105ceb81ad67b8bdc0b671dd F src/sqlite.h.in 0b3cab7b2ea51f58396e8871fa5f349cfece5330 F src/sqlite3ext.h 1a1a4f784aa9c3b00edd287940197de52487cd93 -F src/sqliteInt.h ba4a6d6288efb25b84bc0d7d0aaf80f9b42523ba +F src/sqliteInt.h be1ab8bc2c295a1bf0ddc9241ea879f455b72df6 F src/sqliteLimit.h 164b0e6749d31e0daa1a4589a169d31c0dec7b3d F src/status.c 7ac64842c86cec2fc1a1d0e5c16d3beb8ad332bf F src/table.c 2cd62736f845d82200acfa1287e33feb3c15d62e @@ -250,7 +250,7 @@ F src/vtab.c 901791a47318c0562cd0c676a2c6ff1bc530e582 F src/wal.c 3154756177d6219e233d84291d5b05f4e06ff5e9 F src/wal.h 66b40bd91bc29a5be1c88ddd1f5ade8f3f48728a F src/walker.c 3112bb3afe1d85dc52317cb1d752055e9a781f8f -F src/where.c 7d09f4c1512affb60cc1190a4b33d121d4ce039a +F src/where.c b8ebb50b82578761e5e764c5c16d7693049a523f F test/8_3_names.test 631ea964a3edb091cf73c3b540f6bcfdb36ce823 F test/aggerror.test a867e273ef9e3d7919f03ef4f0e8c0d2767944f2 F test/alias.test 4529fbc152f190268a15f9384a5651bbbabc9d87 @@ -264,9 +264,9 @@ F test/analyze.test c8cb89e8736336f1f0646c8123e6028a14c7b55e F test/analyze2.test b2c53cf72aa65383da991d97b9d3ec7b45f72c03 F test/analyze3.test d61f55d8b472fc6e713160b1e577f7a68e63f38b F test/analyze4.test 757b37875cf9bb528d46f74497bc789c88365045 -F test/analyze5.test 1de8d66b11aae5a1453aa042d62e834a476bac9c +F test/analyze5.test f6c250012b173f9b10ae5d577e9592767b376b1c F test/analyze6.test c125622a813325bba1b4999040ddc213773c2290 -F test/analyze7.test 5508e7828164ea0b518ed219bed7320a481863d4 +F test/analyze7.test ed672a2c09c4b5a5b528ca38951318af463bb17e F test/analyze8.test ff7a3b4631c896a51e7404268796c61d74117e9f F test/async.test 1d0e056ba1bb9729283a0f22718d3a25e82c277b F test/async2.test c0a9bd20816d7d6a2ceca7b8c03d3d69c28ffb8b @@ -956,7 +956,7 @@ F tool/symbols.sh caaf6ccc7300fd43353318b44524853e222557d5 F tool/tostr.awk 11760e1b94a5d3dcd42378f3cc18544c06cfa576 F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f F tool/warnings.sh 2ebae31e1eb352696f3c2f7706a34c084b28c262 -P e93c248c8470622944cf7f9e5af4f10c4e4809e3 -R 4c0da7c33e60748bbef1bc20f3ff5e64 +P 7e914aa999d9f3f7be58a4494d33a7876af56603 b9d41c3490bf325915227a32b612e6772fb62fa9 +R c43dfa67e269269636fb7d00fce4d038 U drh -Z d7b9f3f4715d72ce86f406011b991106 +Z 2a4dca5bf1206d663b012fe85305a05e diff --git a/manifest.uuid b/manifest.uuid index 9cb64d118c..c8d9795b4a 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -7e914aa999d9f3f7be58a4494d33a7876af56603 \ No newline at end of file +2daab6bd42b34b32de46db513437cc4d6ca17975 \ No newline at end of file diff --git a/src/analyze.c b/src/analyze.c index bf8e59254c..4e106cdc57 100644 --- a/src/analyze.c +++ b/src/analyze.c @@ -590,7 +590,7 @@ void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){ #ifdef SQLITE_ENABLE_STAT2 if( pIdx->aSample ){ int j; - for(j=0; jnSample; j++){ IndexSample *p = &pIdx->aSample[j]; if( p->eType==SQLITE_TEXT || p->eType==SQLITE_BLOB ){ sqlite3DbFree(db, p->u.z); @@ -640,6 +640,7 @@ int sqlite3AnalysisLoad(sqlite3 *db, int iDb){ sqlite3DefaultRowEst(pIdx); sqlite3DeleteIndexSamples(db, pIdx); pIdx->aSample = 0; + pIdx->nSample = 0; } /* Check to make sure the sqlite_stat1 table exists */ @@ -669,7 +670,8 @@ int sqlite3AnalysisLoad(sqlite3 *db, int iDb){ sqlite3_stmt *pStmt = 0; zSql = sqlite3MPrintf(db, - "SELECT idx,sampleno,sample FROM %Q.sqlite_stat2", sInfo.zDatabase); + "SELECT idx, sampleno, sample FROM %Q.sqlite_stat2" + " ORDER BY rowid DESC", sInfo.zDatabase); if( !zSql ){ rc = SQLITE_NOMEM; }else{ @@ -681,51 +683,48 @@ int sqlite3AnalysisLoad(sqlite3 *db, int iDb){ while( sqlite3_step(pStmt)==SQLITE_ROW ){ char *zIndex; /* Index name */ Index *pIdx; /* Pointer to the index object */ + int iSample; + int eType; + IndexSample *pSample; zIndex = (char *)sqlite3_column_text(pStmt, 0); - pIdx = zIndex ? sqlite3FindIndex(db, zIndex, sInfo.zDatabase) : 0; - if( pIdx ){ - int iSample = sqlite3_column_int(pStmt, 1); - if( iSample=0 ){ - int eType = sqlite3_column_type(pStmt, 2); - - if( pIdx->aSample==0 ){ - static const int sz = sizeof(IndexSample)*SQLITE_INDEX_SAMPLES; - pIdx->aSample = (IndexSample *)sqlite3DbMallocRaw(0, sz); - if( pIdx->aSample==0 ){ - db->mallocFailed = 1; - break; - } - memset(pIdx->aSample, 0, sz); - } - - assert( pIdx->aSample ); - { - IndexSample *pSample = &pIdx->aSample[iSample]; - pSample->eType = (u8)eType; - if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){ - pSample->u.r = sqlite3_column_double(pStmt, 2); - }else if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){ - const char *z = (const char *)( - (eType==SQLITE_BLOB) ? - sqlite3_column_blob(pStmt, 2): - sqlite3_column_text(pStmt, 2) - ); - int n = sqlite3_column_bytes(pStmt, 2); - if( n>24 ){ - n = 24; - } - pSample->nByte = (u8)n; - if( n < 1){ - pSample->u.z = 0; - }else{ - pSample->u.z = sqlite3DbStrNDup(0, z, n); - if( pSample->u.z==0 ){ - db->mallocFailed = 1; - break; - } - } - } + if( zIndex==0 ) continue; + pIdx = sqlite3FindIndex(db, zIndex, sInfo.zDatabase); + if( pIdx==0 ) continue; + iSample = sqlite3_column_int(pStmt, 1); + if( iSample>=SQLITE_MAX_SAMPLES || iSample<0 ) continue; + if( pIdx->nSample<=iSample ){ + IndexSample *pNew; + int sz = sizeof(IndexSample)*(iSample+1); + pNew = (IndexSample*)sqlite3Realloc(pIdx->aSample, sz); + if( pNew==0 ){ + db->mallocFailed = 1; + break; + } + pIdx->aSample = pNew; + pIdx->nSample = iSample+1; + } + eType = sqlite3_column_type(pStmt, 2); + pSample = &pIdx->aSample[iSample]; + pSample->eType = (u8)eType; + if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){ + pSample->u.r = sqlite3_column_double(pStmt, 2); + }else if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){ + const char *z = (const char *)( + (eType==SQLITE_BLOB) ? + sqlite3_column_blob(pStmt, 2): + sqlite3_column_text(pStmt, 2) + ); + int n = sqlite3_column_bytes(pStmt, 2); + if( n>24 ) n = 24; + pSample->nByte = (u8)n; + if( n < 1){ + pSample->u.z = 0; + }else{ + pSample->u.z = sqlite3DbStrNDup(0, z, n); + if( pSample->u.z==0 ){ + db->mallocFailed = 1; + break; } } } diff --git a/src/sqliteInt.h b/src/sqliteInt.h index bcf6a591af..9c341292c5 100644 --- a/src/sqliteInt.h +++ b/src/sqliteInt.h @@ -81,7 +81,10 @@ ** construct a histogram of the table content when running ANALYZE ** and with SQLITE_ENABLE_STAT2 */ -#define SQLITE_INDEX_SAMPLES 10 +#ifndef SQLITE_INDEX_SAMPLES +# define SQLITE_INDEX_SAMPLES 10 +#endif +#define SQLITE_MAX_SAMPLES 100 /* ** The following macros are used to cast pointers to integers and @@ -1483,6 +1486,7 @@ struct Index { u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */ u8 autoIndex; /* True if is automatically created (ex: by UNIQUE) */ u8 bUnordered; /* Use this index for == or IN queries only */ + u8 nSample; /* Number of slots in aSample[] */ char *zColAff; /* String defining the affinity of each column */ Index *pNext; /* The next index associated with the same table */ Schema *pSchema; /* Schema containing this index */ diff --git a/src/where.c b/src/where.c index 21fb7f45f4..dbe6ed2ea0 100644 --- a/src/where.c +++ b/src/where.c @@ -2422,12 +2422,12 @@ static void bestVirtualIndex( /* ** Argument pIdx is a pointer to an index structure that has an array of -** SQLITE_INDEX_SAMPLES evenly spaced samples of the first indexed column +** pIdx->nSample evenly spaced samples of the first indexed column ** stored in Index.aSample. These samples divide the domain of values stored -** the index into (SQLITE_INDEX_SAMPLES+1) regions. +** the index into (pIdx->nSample+1) regions. ** Region 0 contains all values less than the first sample value. Region ** 1 contains values between the first and second samples. Region 2 contains -** values between samples 2 and 3. And so on. Region SQLITE_INDEX_SAMPLES +** values between samples 2 and 3. And so on. Region pIdx->nSample ** contains values larger than the last sample. ** ** If the index contains many duplicates of a single value, then it is @@ -2438,7 +2438,7 @@ static void bestVirtualIndex( ** ** If successful, this function determines which of the regions value ** pVal lies in, sets *piRegion to the region index (a value between 0 -** and SQLITE_INDEX_SAMPLES+1, inclusive) and returns SQLITE_OK. +** and S+1, inclusive) and returns SQLITE_OK. ** Or, if an OOM occurs while converting text values between encodings, ** SQLITE_NOMEM is returned and *piRegion is undefined. */ @@ -2453,12 +2453,14 @@ static int whereRangeRegion( assert( roundUp==0 || roundUp==1 ); if( ALWAYS(pVal) ){ IndexSample *aSample = pIdx->aSample; + int nSample = pIdx->nSample; int i = 0; int eType = sqlite3_value_type(pVal); + assert( nSample>0 ); if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){ double r = sqlite3_value_double(pVal); - for(i=0; i=SQLITE_TEXT ) break; if( roundUp ){ @@ -2470,7 +2472,7 @@ static int whereRangeRegion( }else if( eType==SQLITE_NULL ){ i = 0; if( roundUp ){ - while( idb; @@ -2500,7 +2502,7 @@ static int whereRangeRegion( } n = sqlite3ValueBytes(pVal, pColl->enc); - for(i=0; i=0 && i<=SQLITE_INDEX_SAMPLES ); + assert( i>=0 && i<=pIdx->nSample ); *piRegion = i; } return SQLITE_OK; @@ -2626,7 +2628,8 @@ static int whereRangeScanEst( sqlite3_value *pUpperVal = 0; int iEst; int iLower = 0; - int iUpper = SQLITE_INDEX_SAMPLES; + int nSample = p->nSample; + int iUpper = p->nSample; int roundUpUpper = 0; int roundUpLower = 0; u8 aff = p->pTable->aCol[p->aiColumn[0]].affinity; @@ -2653,7 +2656,7 @@ static int whereRangeScanEst( if( pLower ) iLower = iUpper/2; }else if( pUpperVal==0 ){ rc = whereRangeRegion(pParse, p, pLowerVal, roundUpLower, &iLower); - if( pUpper ) iUpper = (iLower + SQLITE_INDEX_SAMPLES + 1)/2; + if( pUpper ) iUpper = (iLower + p->nSample + 1)/2; }else{ rc = whereRangeRegion(pParse, p, pUpperVal, roundUpUpper, &iUpper); if( rc==SQLITE_OK ){ @@ -2663,12 +2666,13 @@ static int whereRangeScanEst( WHERETRACE(("range scan regions: %d..%d\n", iLower, iUpper)); iEst = iUpper - iLower; - testcase( iEst==SQLITE_INDEX_SAMPLES ); - assert( iEst<=SQLITE_INDEX_SAMPLES ); + testcase( iEst==nSample ); + assert( iEst<=nSample ); + assert( nSample>0 ); if( iEst<1 ){ - *piEst = 50/SQLITE_INDEX_SAMPLES; + *piEst = 50/nSample; }else{ - *piEst = (iEst*100)/SQLITE_INDEX_SAMPLES; + *piEst = (iEst*100)/nSample; } sqlite3ValueFree(pLowerVal); sqlite3ValueFree(pUpperVal); @@ -2718,6 +2722,7 @@ static int whereEqualScanEst( double nRowEst; /* New estimate of the number of rows */ assert( p->aSample!=0 ); + assert( p->nSample>0 ); aff = p->pTable->aCol[p->aiColumn[0]].affinity; if( pExpr ){ rc = valueFromExpr(pParse, pExpr, aff, &pRhs); @@ -2732,10 +2737,10 @@ static int whereEqualScanEst( if( rc ) goto whereEqualScanEst_cancel; WHERETRACE(("equality scan regions: %d..%d\n", iLower, iUpper)); if( iLower>=iUpper ){ - nRowEst = p->aiRowEst[0]/(SQLITE_INDEX_SAMPLES*2); + nRowEst = p->aiRowEst[0]/(p->nSample*3); if( nRowEst<*pnRow ) *pnRow = nRowEst; }else{ - nRowEst = (iUpper-iLower)*p->aiRowEst[0]/SQLITE_INDEX_SAMPLES; + nRowEst = (iUpper-iLower)*p->aiRowEst[0]/p->nSample; *pnRow = nRowEst; } @@ -2776,14 +2781,16 @@ static int whereInScanEst( int nSpan = 0; /* Number of histogram regions spanned */ int nSingle = 0; /* Histogram regions hit by a single value */ int nNotFound = 0; /* Count of values that are not constants */ - int i; /* Loop counter */ - u8 aSpan[SQLITE_INDEX_SAMPLES+1]; /* Histogram regions that are spanned */ - u8 aSingle[SQLITE_INDEX_SAMPLES+1]; /* Histogram regions hit once */ + int i; /* Loop counter */ + int nSample = p->nSample; /* Number of samples */ + u8 aSpan[SQLITE_MAX_SAMPLES+1]; /* Histogram regions that are spanned */ + u8 aSingle[SQLITE_MAX_SAMPLES+1]; /* Histogram regions hit once */ assert( p->aSample!=0 ); + assert( nSample>0 ); aff = p->pTable->aCol[p->aiColumn[0]].affinity; - memset(aSpan, 0, sizeof(aSpan)); - memset(aSingle, 0, sizeof(aSingle)); + memset(aSpan, 0, nSample+1); + memset(aSingle, 0, nSample+1); for(i=0; inExpr; i++){ sqlite3ValueFree(pVal); rc = valueFromExpr(pParse, pList->a[i].pExpr, aff, &pVal); @@ -2799,19 +2806,19 @@ static int whereInScanEst( if( iLower>=iUpper ){ aSingle[iLower] = 1; }else{ - assert( iLower>=0 && iUpper<=SQLITE_INDEX_SAMPLES ); + assert( iLower>=0 && iUpper<=nSample ); while( iLoweraiRowEst[0]/(2*SQLITE_INDEX_SAMPLES) + nRowEst = (nSpan*3+nSingle)*p->aiRowEst[0]/(3*nSample) + nNotFound*p->aiRowEst[1]; if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0]; *pnRow = nRowEst; diff --git a/test/analyze5.test b/test/analyze5.test index a0469da125..e1720341e7 100644 --- a/test/analyze5.test +++ b/test/analyze5.test @@ -128,28 +128,28 @@ foreach {testid where index rows} { 51 {z>=-100 AND z<=2.0} t1z 900 52 {z>=-100 AND z<3.0} t1z 900 - 101 {z=-1} t1z 50 + 101 {z=-1} t1z 33 102 {z=0} t1z 400 103 {z=1} t1z 300 104 {z=2} t1z 200 105 {z=3} t1z 100 - 106 {z=4} t1z 50 - 107 {z=-10.0} t1z 50 + 106 {z=4} t1z 33 + 107 {z=-10.0} t1z 33 108 {z=0.0} t1z 400 109 {z=1.0} t1z 300 110 {z=2.0} t1z 200 111 {z=3.0} t1z 100 - 112 {z=4.0} t1z 50 - 113 {z=1.5} t1z 50 - 114 {z=2.5} t1z 50 + 112 {z=4.0} t1z 33 + 113 {z=1.5} t1z 33 + 114 {z=2.5} t1z 33 - 201 {z IN (-1)} t1z 50 + 201 {z IN (-1)} t1z 33 202 {z IN (0)} t1z 400 203 {z IN (1)} t1z 300 204 {z IN (2)} t1z 200 205 {z IN (3)} t1z 100 - 206 {z IN (4)} t1z 50 - 207 {z IN (0.5)} t1z 50 + 206 {z IN (4)} t1z 33 + 207 {z IN (0.5)} t1z 33 208 {z IN (0,1)} t1z 700 209 {z IN (0,1,2)} t1z 900 210 {z IN (0,1,2,3)} {} 100 @@ -157,12 +157,12 @@ foreach {testid where index rows} { 212 {z IN (1,2)} t1z 500 213 {z IN (2,3)} t1z 300 214 {z=3 OR z=2} t1z 300 - 215 {z IN (-1,3)} t1z 150 - 216 {z=-1 OR z=3} t1z 150 + 215 {z IN (-1,3)} t1z 133 + 216 {z=-1 OR z=3} t1z 133 300 {y=0} {} 100 - 301 {y=1} t1y 50 - 302 {y=0.1} t1y 50 + 301 {y=1} t1y 33 + 302 {y=0.1} t1y 33 400 {x IS NULL} t1x 400 @@ -205,9 +205,9 @@ db eval { # foreach {testid where index rows} { 500 {x IS NULL AND u='charlie'} t1u 20 - 501 {x=1 AND u='charlie'} t1x 5 + 501 {x=1 AND u='charlie'} t1x 3 502 {x IS NULL} {} 100 - 503 {x=1} t1x 50 + 503 {x=1} t1x 33 504 {x IS NOT NULL} t1x 25 505 {+x IS NOT NULL} {} 500 506 {upper(x) IS NOT NULL} {} 500 diff --git a/test/analyze7.test b/test/analyze7.test index 4892a2233a..4fd76285bf 100644 --- a/test/analyze7.test +++ b/test/analyze7.test @@ -99,8 +99,10 @@ do_test analyze7-3.3 { execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123 AND b=123} } {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?) (~1 rows)}} do_test analyze7-3.4 { - execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=123 AND b=123} -} {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?) (~2 rows)}} + set x [execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=123 AND b=123}] + regsub -all {[bcd]+} $x {x} x + set x +} {0 0 0 {SEARCH TABLE t1 USING INDEX t1x (x=?) (~2 rows)}} do_test analyze7-3.5 { execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123 AND c=123} } {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?) (~1 rows)}}