/ Check-in [67a9a392]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Adjust ANALYZE for improved test coverage. Use the SQLITE_ENABLE_STAT3_OR_STAT4 macro (created in sqliteInt.h) to conditionally include code, instead of a boolean specifying both macros separately.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 67a9a392edd62ef5a5a1ad3010b20a39b285793d
User & Date: drh 2013-08-27 23:15:44
Context
2013-08-28
11:43
Label a certain branch as NEVER() only for non-STAT3/4 builds. check-in: b5ccf6e9 user: drh tags: trunk
2013-08-27
23:15
Adjust ANALYZE for improved test coverage. Use the SQLITE_ENABLE_STAT3_OR_STAT4 macro (created in sqliteInt.h) to conditionally include code, instead of a boolean specifying both macros separately. check-in: 67a9a392 user: drh tags: trunk
20:16
Reduce the amount of code run and memory used for ANALYZE in the common case where neither STAT3 and STAT4 are enabled. check-in: 9d1424c9 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/analyze.c.

   141    141   */
   142    142   #ifndef SQLITE_OMIT_ANALYZE
   143    143   #include "sqliteInt.h"
   144    144   
   145    145   #if defined(SQLITE_ENABLE_STAT4)
   146    146   # define IsStat4     1
   147    147   # define IsStat3     0
   148         -# define SQLITE_ENABLE_STAT34 1
   149    148   #elif defined(SQLITE_ENABLE_STAT3)
   150    149   # define IsStat4     0
   151    150   # define IsStat3     1
   152         -# define SQLITE_ENABLE_STAT34 1
   153    151   #else
   154    152   # define IsStat4     0
   155    153   # define IsStat3     0
   156         -# undef SQLITE_ENABLE_STAT34
   157    154   # undef SQLITE_STAT4_SAMPLES
   158    155   # define SQLITE_STAT4_SAMPLES 1
   159    156   #endif
   160    157   #define IsStat34    (IsStat3+IsStat4)  /* 1 for STAT3 or STAT4. 0 otherwise */
   161    158   
   162    159   /*
   163    160   ** This routine generates code that opens the sqlite_statN tables.
................................................................................
   230    227         ** associated with the table zWhere. If zWhere is NULL, delete the
   231    228         ** entire contents of the table. */
   232    229         aRoot[i] = pStat->tnum;
   233    230         aCreateTbl[i] = 0;
   234    231         sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab);
   235    232         if( zWhere ){
   236    233           sqlite3NestedParse(pParse,
   237         -           "DELETE FROM %Q.%s WHERE %s=%Q", pDb->zName, zTab, zWhereType, zWhere
          234  +           "DELETE FROM %Q.%s WHERE %s=%Q",
          235  +           pDb->zName, zTab, zWhereType, zWhere
   238    236           );
   239    237         }else{
   240    238           /* The sqlite_stat[134] table already exists.  Delete all rows. */
   241    239           sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
   242    240         }
   243    241       }
   244    242     }
   245    243   
   246    244     /* Open the sqlite_stat[134] tables for writing. */
   247         -  for(i=0; i<ArraySize(aTable); i++){
   248         -    if( aTable[i].zCols ){
   249         -      sqlite3VdbeAddOp3(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb);
   250         -      sqlite3VdbeChangeP4(v, -1, (char *)3, P4_INT32);
   251         -      sqlite3VdbeChangeP5(v, aCreateTbl[i]);
   252         -    }else{
   253         -      break;
   254         -    }
          245  +  for(i=0; aTable[i].zCols; i++){
          246  +    assert( i<ArraySize(aTable) );
          247  +    sqlite3VdbeAddOp3(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb);
          248  +    sqlite3VdbeChangeP4(v, -1, (char *)3, P4_INT32);
          249  +    sqlite3VdbeChangeP5(v, aCreateTbl[i]);
   255    250     }
   256    251   }
   257    252   
   258    253   /*
   259    254   ** Recommended number of samples for sqlite_stat4
   260    255   */
   261    256   #ifndef SQLITE_STAT4_SAMPLES
................................................................................
   268    263   ** information.
   269    264   */
   270    265   typedef struct Stat4Accum Stat4Accum;
   271    266   typedef struct Stat4Sample Stat4Sample;
   272    267   struct Stat4Sample {
   273    268     tRowcnt *anEq;                  /* sqlite_stat4.nEq */
   274    269     tRowcnt *anDLt;                 /* sqlite_stat4.nDLt */
   275         -#ifdef SQLITE_ENABLE_STAT34
          270  +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
   276    271     tRowcnt *anLt;                  /* sqlite_stat4.nLt */
   277    272     i64 iRowid;                     /* Rowid in main table of the key */
   278    273     u8 isPSample;                   /* True if a periodic sample */
   279    274     int iCol;                       /* If !isPSample, the reason for inclusion */
   280    275     u32 iHash;                      /* Tiebreaker hash */
   281    276   #endif
   282    277   };                                                    
................................................................................
   308    303     int argc,
   309    304     sqlite3_value **argv
   310    305   ){
   311    306     Stat4Accum *p;
   312    307     int nCol;                       /* Number of columns in index being sampled */
   313    308     int nColUp;                     /* nCol rounded up for alignment */
   314    309     int n;                          /* Bytes of space to allocate */
   315         -#ifdef SQLITE_ENABLE_STAT34
          310  +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
   316    311     int mxSample = SQLITE_STAT4_SAMPLES;
   317    312   #endif
   318    313   
   319    314     /* Decode the three function arguments */
   320    315     UNUSED_PARAMETER(argc);
   321    316     nCol = sqlite3_value_int(argv[0]);
   322    317     assert( nCol>1 );               /* >1 because it includes the rowid column */
   323    318     nColUp = sizeof(tRowcnt)<8 ? (nCol+1)&~1 : nCol;
   324    319   
   325    320     /* Allocate the space required for the Stat4Accum object */
   326    321     n = sizeof(*p) 
   327    322       + sizeof(tRowcnt)*nColUp                  /* Stat4Accum.anEq */
   328    323       + sizeof(tRowcnt)*nColUp                  /* Stat4Accum.anDLt */
   329         -#ifdef SQLITE_ENABLE_STAT34
          324  +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
   330    325       + sizeof(tRowcnt)*nColUp                  /* Stat4Accum.anLt */
   331    326       + sizeof(Stat4Sample)*(nCol+mxSample)   /* Stat4Accum.aBest[], a[] */
   332    327       + sizeof(tRowcnt)*3*nColUp*(nCol+mxSample)
   333    328   #endif
   334    329     ;
   335    330     p = sqlite3MallocZero(n);
   336    331     if( p==0 ){
................................................................................
   339    334     }
   340    335   
   341    336     p->nRow = 0;
   342    337     p->nCol = nCol;
   343    338     p->current.anDLt = (tRowcnt*)&p[1];
   344    339     p->current.anEq = &p->current.anDLt[nColUp];
   345    340   
   346         -#ifdef SQLITE_ENABLE_STAT34
          341  +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
   347    342     {
   348    343       u8 *pSpace;                     /* Allocated space not yet assigned */
   349    344       int i;                          /* Used to iterate through p->aSample[] */
   350    345   
   351    346       p->iGet = -1;
   352    347       p->mxSample = mxSample;
   353    348       p->nPSample = sqlite3_value_int64(argv[1])/(mxSample/3+1) + 1;
................................................................................
   384    379     0,               /* xStep */
   385    380     0,               /* xFinalize */
   386    381     "stat_init",     /* zName */
   387    382     0,               /* pHash */
   388    383     0                /* pDestructor */
   389    384   };
   390    385   
   391         -#ifdef SQLITE_ENABLE_STAT34
          386  +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
   392    387   /*
   393    388   ** Return true if pNew is to be preferred over pOld.
   394    389   */
   395    390   static int sampleIsBetter(Stat4Sample *pNew, Stat4Sample *pOld){
   396    391     tRowcnt nEqNew = pNew->anEq[pNew->iCol];
   397    392     tRowcnt nEqOld = pOld->anEq[pOld->iCol];
   398    393   
................................................................................
   506    501           iMin = i;
   507    502         }
   508    503       }
   509    504       assert( iMin>=0 );
   510    505       p->iMin = iMin;
   511    506     }
   512    507   }
   513         -#endif /* SQLITE_ENABLE_STAT34 */
          508  +#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
   514    509   
   515    510   /*
   516    511   ** Field iChng of the index being scanned has changed. So at this point
   517    512   ** p->current contains a sample that reflects the previous row of the
   518    513   ** index. The value of anEq[iChng] and subsequent anEq[] elements are
   519    514   ** correct at this point.
   520    515   */
................................................................................
   599    594       /* Update anDLt[], anLt[] and anEq[] to reflect the values that apply
   600    595       ** to the current row of the index. */
   601    596       for(i=0; i<iChng; i++){
   602    597         p->current.anEq[i]++;
   603    598       }
   604    599       for(i=iChng; i<p->nCol; i++){
   605    600         p->current.anDLt[i]++;
   606         -#ifdef SQLITE_ENABLE_STAT34
          601  +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
   607    602         p->current.anLt[i] += p->current.anEq[i];
   608    603   #endif
   609    604         p->current.anEq[i] = 1;
   610    605       }
   611    606     }
   612    607     p->nRow++;
   613         -#ifdef SQLITE_ENABLE_STAT34
          608  +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
   614    609     p->current.iRowid = sqlite3_value_int64(argv[2]);
   615    610     p->current.iHash = p->iPrn = p->iPrn*1103515245 + 12345;
   616    611   #endif
   617    612   
   618    613   #ifdef SQLITE_ENABLE_STAT4
   619    614     {
   620    615       tRowcnt nLt = p->current.anLt[p->nCol-1];
................................................................................
   669    664   */
   670    665   static void statGet(
   671    666     sqlite3_context *context,
   672    667     int argc,
   673    668     sqlite3_value **argv
   674    669   ){
   675    670     Stat4Accum *p = (Stat4Accum*)sqlite3_value_blob(argv[0]);
   676         -#ifdef SQLITE_ENABLE_STAT34
          671  +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
   677    672     /* STAT3 and STAT4 have a parameter on this routine. */
   678    673     int eCall = sqlite3_value_int(argv[1]);
   679    674     assert( argc==2 );
   680    675     assert( eCall==STAT_GET_STAT1 || eCall==STAT_GET_NEQ 
   681    676          || eCall==STAT_GET_ROWID || eCall==STAT_GET_NLT
   682    677          || eCall==STAT_GET_NDLT 
   683    678     );
................................................................................
   725    720         z += sqlite3Strlen30(z);
   726    721         assert( p->current.anEq[i] );
   727    722       }
   728    723       assert( z[0]=='\0' && z>zRet );
   729    724   
   730    725       sqlite3_result_text(context, zRet, -1, sqlite3_free);
   731    726     }
   732         -#ifdef SQLITE_ENABLE_STAT34
          727  +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
   733    728     else if( eCall==STAT_GET_ROWID ){
   734    729       if( p->iGet<0 ){
   735    730         samplePushPrevious(p, 0);
   736    731         p->iGet = 0;
   737    732       }
   738    733       if( p->iGet<p->nSample ){
   739    734         sqlite3_result_int64(context, p->a[p->iGet].iRowid);
................................................................................
   767    762           }
   768    763           assert( z[0]=='\0' && z>zRet );
   769    764           z[-1] = '\0';
   770    765           sqlite3_result_text(context, zRet, -1, sqlite3_free);
   771    766         }
   772    767       }
   773    768     }
   774         -#endif /* SQLITE_ENABLE_STAT34 */
          769  +#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
   775    770   }
   776    771   static const FuncDef statGetFuncdef = {
   777    772     1+IsStat34,      /* nArg */
   778    773     SQLITE_UTF8,     /* iPrefEnc */
   779    774     0,               /* flags */
   780    775     0,               /* pUserData */
   781    776     0,               /* pNext */
................................................................................
   785    780     "stat_get",      /* zName */
   786    781     0,               /* pHash */
   787    782     0                /* pDestructor */
   788    783   };
   789    784   
   790    785   static void callStatGet(Vdbe *v, int regStat4, int iParam, int regOut){
   791    786     assert( regOut!=regStat4 && regOut!=regStat4+1 );
   792         -#ifdef SQLITE_ENABLE_STAT34
          787  +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
   793    788     sqlite3VdbeAddOp2(v, OP_Integer, iParam, regStat4+1);
   794    789   #else
   795    790     assert( iParam==STAT_GET_STAT1 );
   796    791   #endif
   797    792     sqlite3VdbeAddOp3(v, OP_Function, 0, regStat4, regOut);
   798    793     sqlite3VdbeChangeP4(v, -1, (char*)&statGetFuncdef, P4_FUNCDEF);
   799    794     sqlite3VdbeChangeP5(v, 1 + IsStat34);
................................................................................
   819    814     int i;                       /* Loop counter */
   820    815     int jZeroRows = -1;          /* Jump from here if number of rows is zero */
   821    816     int iDb;                     /* Index of database containing pTab */
   822    817     u8 needTableCnt = 1;         /* True to count the table */
   823    818     int regNewRowid = iMem++;    /* Rowid for the inserted record */
   824    819     int regStat4 = iMem++;       /* Register to hold Stat4Accum object */
   825    820     int regChng = iMem++;        /* Index of changed index field */
   826         -#ifdef SQLITE_ENABLE_STAT34
          821  +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
   827    822     int regRowid = iMem++;       /* Rowid argument passed to stat_push() */
   828    823   #endif
   829    824     int regTemp = iMem++;        /* Temporary use register */
   830    825     int regTabname = iMem++;     /* Register containing table name */
   831    826     int regIdxname = iMem++;     /* Register containing index name */
   832    827     int regStat1 = iMem++;       /* Value for the stat column of sqlite_stat1 */
   833    828     int regPrev = iMem;          /* MUST BE LAST (see below) */
................................................................................
   933    928       /* Invoke the stat_init() function. The arguments are:
   934    929       ** 
   935    930       **    (1) the number of columns in the index including the rowid,
   936    931       **    (2) the number of rows in the index,
   937    932       **
   938    933       ** The second argument is only used for STAT3 and STAT4
   939    934       */
   940         -#ifdef SQLITE_ENABLE_STAT34
          935  +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
   941    936       sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat4+2);
   942    937   #endif
   943    938       sqlite3VdbeAddOp2(v, OP_Integer, nCol+1, regStat4+1);
   944    939       sqlite3VdbeAddOp3(v, OP_Function, 0, regStat4+1, regStat4);
   945    940       sqlite3VdbeChangeP4(v, -1, (char*)&statInitFuncdef, P4_FUNCDEF);
   946    941       sqlite3VdbeChangeP5(v, 1+IsStat34);
   947    942   
................................................................................
   996    991       **  chng_addr_N:
   997    992       **   regRowid = idx(rowid)            // STAT34 only
   998    993       **   stat_push(P, regChng, regRowid)  // 3rd parameter STAT34 only
   999    994       **   Next csr
  1000    995       **   if !eof(csr) goto next_row;
  1001    996       */
  1002    997       sqlite3VdbeJumpHere(v, aGotoChng[nCol]);
  1003         -#ifdef SQLITE_ENABLE_STAT34
          998  +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  1004    999       sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, regRowid);
  1005   1000       assert( regRowid==(regStat4+2) );
  1006   1001   #endif
  1007   1002       assert( regChng==(regStat4+1) );
  1008   1003       sqlite3VdbeAddOp3(v, OP_Function, 1, regStat4, regTemp);
  1009   1004       sqlite3VdbeChangeP4(v, -1, (char*)&statPushFuncdef, P4_FUNCDEF);
  1010   1005       sqlite3VdbeChangeP5(v, 2+IsStat34);
................................................................................
  1014   1009       callStatGet(v, regStat4, STAT_GET_STAT1, regStat1);
  1015   1010       sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "aaa", 0);
  1016   1011       sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
  1017   1012       sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid);
  1018   1013       sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
  1019   1014   
  1020   1015       /* Add the entries to the stat3 or stat4 table. */
  1021         -#ifdef SQLITE_ENABLE_STAT34
         1016  +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  1022   1017       {
  1023   1018         int regEq = regStat1;
  1024   1019         int regLt = regStat1+1;
  1025   1020         int regDLt = regStat1+2;
  1026   1021         int regSample = regStat1+3;
  1027   1022         int regCol = regStat1+4;
  1028   1023         int regSampleRowid = regCol + nCol;
................................................................................
  1050   1045   #endif
  1051   1046         sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 6, regTemp, "bbbbbb", 0);
  1052   1047         sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regNewRowid);
  1053   1048         sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regTemp, regNewRowid);
  1054   1049         sqlite3VdbeAddOp2(v, OP_Goto, 0, addrNext);
  1055   1050         sqlite3VdbeJumpHere(v, addrIsNull);
  1056   1051       }
  1057         -#endif /* SQLITE_ENABLE_STAT34 */
         1052  +#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
  1058   1053   
  1059   1054       /* End of analysis */
  1060   1055       sqlite3VdbeJumpHere(v, addrRewind);
  1061   1056       sqlite3DbFree(db, aGotoChng);
  1062   1057     }
  1063   1058   
  1064   1059   
................................................................................
  1231   1226   ){
  1232   1227     char *z = zIntArray;
  1233   1228     int c;
  1234   1229     int i;
  1235   1230     tRowcnt v;
  1236   1231   
  1237   1232     assert( pbUnordered==0 || *pbUnordered==0 );
  1238         -  
  1239         -  if( z==0 ) z = "";
         1233  +
         1234  +  if( NEVER(z==0) ) z = "";
  1240   1235     for(i=0; *z && i<nOut; i++){
  1241   1236       v = 0;
  1242   1237       while( (c=z[0])>='0' && c<='9' ){
  1243   1238         v = v*10 + c - '0';
  1244   1239         z++;
  1245   1240       }
  1246   1241       aOut[i] = v;
................................................................................
  1283   1278     }else{
  1284   1279       pIndex = 0;
  1285   1280     }
  1286   1281     z = argv[2];
  1287   1282   
  1288   1283     if( pIndex ){
  1289   1284       int bUnordered = 0;
  1290         -    decodeIntArray((char*)z, pIndex->nColumn+1, pIndex->aiRowEst, &bUnordered);
         1285  +    decodeIntArray((char*)z, pIndex->nColumn+1, pIndex->aiRowEst,&bUnordered);
  1291   1286       if( pIndex->pPartIdxWhere==0 ) pTable->nRowEst = pIndex->aiRowEst[0];
  1292   1287       pIndex->bUnordered = bUnordered;
  1293   1288     }else{
  1294   1289       decodeIntArray((char*)z, 1, &pTable->nRowEst, 0);
  1295   1290     }
  1296   1291   
  1297   1292     return 0;
................................................................................
  1298   1293   }
  1299   1294   
  1300   1295   /*
  1301   1296   ** If the Index.aSample variable is not NULL, delete the aSample[] array
  1302   1297   ** and its contents.
  1303   1298   */
  1304   1299   void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
  1305         -#ifdef SQLITE_ENABLE_STAT34
         1300  +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  1306   1301     if( pIdx->aSample ){
  1307   1302       int j;
  1308   1303       for(j=0; j<pIdx->nSample; j++){
  1309   1304         IndexSample *p = &pIdx->aSample[j];
  1310   1305         sqlite3DbFree(db, p->p);
  1311   1306       }
  1312   1307       sqlite3DbFree(db, pIdx->aSample);
................................................................................
  1314   1309     if( db && db->pnBytesFreed==0 ){
  1315   1310       pIdx->nSample = 0;
  1316   1311       pIdx->aSample = 0;
  1317   1312     }
  1318   1313   #else
  1319   1314     UNUSED_PARAMETER(db);
  1320   1315     UNUSED_PARAMETER(pIdx);
  1321         -#endif /* SQLITE_ENABLE_STAT34 */
         1316  +#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
  1322   1317   }
  1323   1318   
  1324         -#ifdef SQLITE_ENABLE_STAT34
         1319  +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  1325   1320   /*
  1326   1321   ** Populate the pIdx->aAvgEq[] array based on the samples currently
  1327   1322   ** stored in pIdx->aSample[]. 
  1328   1323   */
  1329   1324   static void initAvgEq(Index *pIdx){
  1330   1325     if( pIdx ){
  1331   1326       IndexSample *aSample = pIdx->aSample;
................................................................................
  1511   1506         "SELECT idx,neq,nlt,ndlt,sqlite_record(sample) FROM %Q.sqlite_stat3",
  1512   1507         zDb
  1513   1508       );
  1514   1509     }
  1515   1510   
  1516   1511     return rc;
  1517   1512   }
  1518         -#endif /* SQLITE_ENABLE_STAT34 */
         1513  +#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
  1519   1514   
  1520   1515   /*
  1521   1516   ** Load the content of the sqlite_stat1 and sqlite_stat3/4 tables. The
  1522   1517   ** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
  1523   1518   ** arrays. The contents of sqlite_stat3/4 are used to populate the
  1524   1519   ** Index.aSample[] arrays.
  1525   1520   **
................................................................................
  1547   1542     assert( db->aDb[iDb].pBt!=0 );
  1548   1543   
  1549   1544     /* Clear any prior statistics */
  1550   1545     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
  1551   1546     for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
  1552   1547       Index *pIdx = sqliteHashData(i);
  1553   1548       sqlite3DefaultRowEst(pIdx);
  1554         -#ifdef SQLITE_ENABLE_STAT34
         1549  +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  1555   1550       sqlite3DeleteIndexSamples(db, pIdx);
  1556   1551       pIdx->aSample = 0;
  1557   1552   #endif
  1558   1553     }
  1559   1554   
  1560   1555     /* Check to make sure the sqlite_stat1 table exists */
  1561   1556     sInfo.db = db;
................................................................................
  1572   1567     }else{
  1573   1568       rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
  1574   1569       sqlite3DbFree(db, zSql);
  1575   1570     }
  1576   1571   
  1577   1572   
  1578   1573     /* Load the statistics from the sqlite_stat4 table. */
  1579         -#ifdef SQLITE_ENABLE_STAT34
         1574  +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  1580   1575     if( rc==SQLITE_OK ){
  1581   1576       int lookasideEnabled = db->lookaside.bEnabled;
  1582   1577       db->lookaside.bEnabled = 0;
  1583   1578       rc = loadStat4(db, sInfo.zDatabase);
  1584   1579       db->lookaside.bEnabled = lookasideEnabled;
  1585   1580     }
  1586   1581   #endif

Changes to src/sqliteInt.h.

   564    564   # define SQLITE_DEFAULT_MMAP_SIZE_xc 1  /* Exclude from ctime.c */
   565    565   #endif
   566    566   #if SQLITE_DEFAULT_MMAP_SIZE>SQLITE_MAX_MMAP_SIZE
   567    567   # undef SQLITE_DEFAULT_MMAP_SIZE
   568    568   # define SQLITE_DEFAULT_MMAP_SIZE SQLITE_MAX_MMAP_SIZE
   569    569   #endif
   570    570   
          571  +/*
          572  +** Only one of SQLITE_ENABLE_STAT3 or SQLITE_ENABLE_STAT4 can be defined.
          573  +** Priority is given to SQLITE_ENABLE_STAT4.  If either are defined, also
          574  +** define SQLITE_ENABLE_STAT3_OR_STAT4
          575  +*/
          576  +#ifdef SQLITE_ENABLE_STAT4
          577  +# undef SQLITE_ENABLE_STAT3
          578  +# define SQLITE_ENABLE_STAT3_OR_STAT4 1
          579  +#elif SQLITE_ENABLE_STAT3
          580  +# define SQLITE_ENABLE_STAT3_OR_STAT4 1
          581  +#elif SQLITE_ENABLE_STAT3_OR_STAT4
          582  +# undef SQLITE_ENABLE_STAT3_OR_STAT4
          583  +#endif
          584  +
   571    585   /*
   572    586   ** An instance of the following structure is used to store the busy-handler
   573    587   ** callback for a given sqlite handle. 
   574    588   **
   575    589   ** The sqlite.busyHandler member of the sqlite struct contains the busy
   576    590   ** callback for the database handle. Each pager opened via the sqlite
   577    591   ** handle is passed a pointer to sqlite.busyHandler. The busy-handler
................................................................................
  1546   1560     Expr *pPartIdxWhere;     /* WHERE clause for partial indices */
  1547   1561     int tnum;                /* DB Page containing root of this index */
  1548   1562     u16 nColumn;             /* Number of columns in table used by this index */
  1549   1563     u8 onError;              /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
  1550   1564     unsigned autoIndex:2;    /* 1==UNIQUE, 2==PRIMARY KEY, 0==CREATE INDEX */
  1551   1565     unsigned bUnordered:1;   /* Use this index for == or IN queries only */
  1552   1566     unsigned uniqNotNull:1;  /* True if UNIQUE and NOT NULL for all columns */
  1553         -#if defined(SQLITE_ENABLE_STAT4) || defined(SQLITE_ENABLE_STAT3)
         1567  +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  1554   1568     int nSample;             /* Number of elements in aSample[] */
  1555   1569     int nSampleCol;          /* Size of IndexSample.anEq[] and so on */
  1556   1570     tRowcnt *aAvgEq;         /* Average nEq values for keys not in aSample */
  1557   1571     IndexSample *aSample;    /* Samples of the left-most key */
  1558   1572   #endif
  1559   1573   };
  1560   1574   
................................................................................
  3102   3116   void sqlite3StrAccumReset(StrAccum*);
  3103   3117   void sqlite3SelectDestInit(SelectDest*,int,int);
  3104   3118   Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
  3105   3119   
  3106   3120   void sqlite3BackupRestart(sqlite3_backup *);
  3107   3121   void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *);
  3108   3122   
  3109         -#if defined(SQLITE_ENABLE_STAT3) || defined(SQLITE_ENABLE_STAT4)
         3123  +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  3110   3124   void sqlite3AnalyzeFunctions(void);
  3111   3125   int sqlite3Stat4ProbeSetValue(Parse*,Index*,UnpackedRecord**,Expr*,u8,int,int*);
  3112   3126   void sqlite3Stat4ProbeFree(UnpackedRecord*);
  3113   3127   #endif
  3114   3128   
  3115   3129   /*
  3116   3130   ** The interface to the LEMON-generated parser

Changes to src/vdbemem.c.

  1019   1019   ** Otherwise, if the second argument is non-zero, then this function is 
  1020   1020   ** being called indirectly by sqlite3Stat4ProbeSetValue(). If it has not
  1021   1021   ** already been allocated, allocate the UnpackedRecord structure that 
  1022   1022   ** that function will return to its caller here. Then return a pointer 
  1023   1023   ** an sqlite3_value within the UnpackedRecord.a[] array.
  1024   1024   */
  1025   1025   static sqlite3_value *valueNew(sqlite3 *db, struct ValueNewStat4Ctx *p){
  1026         -#if defined(SQLITE_ENABLE_STAT4) || defined(SQLITE_ENABLE_STAT3)
         1026  +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  1027   1027     if( p ){
  1028   1028       UnpackedRecord *pRec = p->ppRec[0];
  1029   1029   
  1030   1030       if( pRec==0 ){
  1031   1031         Index *pIdx = p->pIdx;      /* Index being probed */
  1032   1032         int nByte;                  /* Bytes of space to allocate */
  1033   1033         int i;                      /* Counter variable */
................................................................................
  1094   1094     }
  1095   1095     op = pExpr->op;
  1096   1096   
  1097   1097     /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT4.
  1098   1098     ** The ifdef here is to enable us to achieve 100% branch test coverage even
  1099   1099     ** when SQLITE_ENABLE_STAT4 is omitted.
  1100   1100     */
  1101         -#if defined(SQLITE_ENABLE_STAT4) || defined(SQLITE_ENABLE_STAT3)
         1101  +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  1102   1102     if( op==TK_REGISTER ) op = pExpr->op2;
  1103   1103   #else
  1104   1104     if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
  1105   1105   #endif
  1106   1106   
  1107   1107     /* Handle negative integers in a single step.  This is needed in the
  1108   1108     ** case when the value is -9223372036854775808.
................................................................................
  1176   1176     *ppVal = pVal;
  1177   1177     return rc;
  1178   1178   
  1179   1179   no_mem:
  1180   1180     db->mallocFailed = 1;
  1181   1181     sqlite3DbFree(db, zVal);
  1182   1182     assert( *ppVal==0 );
         1183  +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  1183   1184     if( pCtx==0 ) sqlite3ValueFree(pVal);
         1185  +#else
         1186  +  assert( pCtx==0 ); sqlite3ValueFree(pVal);
         1187  +#endif
  1184   1188     return SQLITE_NOMEM;
  1185   1189   }
  1186   1190   
  1187   1191   /*
  1188   1192   ** Create a new sqlite3_value object, containing the value of pExpr.
  1189   1193   **
  1190   1194   ** This only works for very simple expressions that consist of one constant
................................................................................
  1200   1204     u8 enc,                   /* Encoding to use */
  1201   1205     u8 affinity,              /* Affinity to use */
  1202   1206     sqlite3_value **ppVal     /* Write the new value here */
  1203   1207   ){
  1204   1208     return valueFromExpr(db, pExpr, enc, affinity, ppVal, 0);
  1205   1209   }
  1206   1210   
  1207         -#if defined(SQLITE_ENABLE_STAT4) || defined(SQLITE_ENABLE_STAT3)
         1211  +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  1208   1212   /*
  1209   1213   ** The implementation of the sqlite_record() function. This function accepts
  1210   1214   ** a single argument of any type. The return value is a formatted database 
  1211   1215   ** record (a blob) containing the argument value.
  1212   1216   **
  1213   1217   ** This is used to convert the value stored in the 'sample' column of the
  1214   1218   ** sqlite_stat3 table to the record format SQLite uses internally.

Changes to src/where.c.

   282    282   #define TERM_DYNAMIC    0x01   /* Need to call sqlite3ExprDelete(db, pExpr) */
   283    283   #define TERM_VIRTUAL    0x02   /* Added by the optimizer.  Do not code */
   284    284   #define TERM_CODED      0x04   /* This term is already coded */
   285    285   #define TERM_COPIED     0x08   /* Has a child */
   286    286   #define TERM_ORINFO     0x10   /* Need to free the WhereTerm.u.pOrInfo object */
   287    287   #define TERM_ANDINFO    0x20   /* Need to free the WhereTerm.u.pAndInfo obj */
   288    288   #define TERM_OR_OK      0x40   /* Used during OR-clause processing */
   289         -#if defined(SQLITE_ENABLE_STAT4) || defined(SQLITE_ENABLE_STAT3)
          289  +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
   290    290   #  define TERM_VNULL    0x80   /* Manufactured x>NULL or x<=NULL term */
   291    291   #else
   292    292   #  define TERM_VNULL    0x00   /* Disabled if not using stat3 */
   293    293   #endif
   294    294   
   295    295   /*
   296    296   ** An instance of the WhereScan object is used as an iterator for locating
................................................................................
   388    388   */
   389    389   struct WhereLoopBuilder {
   390    390     WhereInfo *pWInfo;        /* Information about this WHERE */
   391    391     WhereClause *pWC;         /* WHERE clause terms */
   392    392     ExprList *pOrderBy;       /* ORDER BY clause */
   393    393     WhereLoop *pNew;          /* Template WhereLoop */
   394    394     WhereOrSet *pOrSet;       /* Record best loops here, if not NULL */
   395         -#if defined(SQLITE_ENABLE_STAT4) || defined(SQLITE_ENABLE_STAT3)
          395  +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
   396    396     UnpackedRecord *pRec;     /* Probe for stat4 (if required) */
   397    397     int nRecValid;            /* Number of valid fields currently in pRec */
   398    398   #endif
   399    399   };
   400    400   
   401    401   /*
   402    402   ** The WHERE clause processing routine has two halves.  The
................................................................................
  1786   1786         pTerm->nChild = 1;
  1787   1787         pTerm->wtFlags |= TERM_COPIED;
  1788   1788         pNewTerm->prereqAll = pTerm->prereqAll;
  1789   1789       }
  1790   1790     }
  1791   1791   #endif /* SQLITE_OMIT_VIRTUALTABLE */
  1792   1792   
  1793         -#if defined(SQLITE_ENABLE_STAT4) || defined(SQLITE_ENABLE_STAT3)
         1793  +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  1794   1794     /* When sqlite_stat3 histogram data is available an operator of the
  1795   1795     ** form "x IS NOT NULL" can sometimes be evaluated more efficiently
  1796   1796     ** as "x>NULL" if x is not an INTEGER PRIMARY KEY.  So construct a
  1797   1797     ** virtual term of that form.
  1798   1798     **
  1799   1799     ** Note that the virtual term must be tagged with TERM_VNULL.  This
  1800   1800     ** TERM_VNULL tag will suppress the not-null check at the beginning
................................................................................
  1826   1826         pNewTerm->iParent = idxTerm;
  1827   1827         pTerm = &pWC->a[idxTerm];
  1828   1828         pTerm->nChild = 1;
  1829   1829         pTerm->wtFlags |= TERM_COPIED;
  1830   1830         pNewTerm->prereqAll = pTerm->prereqAll;
  1831   1831       }
  1832   1832     }
  1833         -#endif /* SQLITE_ENABLE_STAT */
         1833  +#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
  1834   1834   
  1835   1835     /* Prevent ON clause terms of a LEFT JOIN from being used to drive
  1836   1836     ** an index for tables to the left of the join.
  1837   1837     */
  1838   1838     pTerm->prereqRight |= extraRight;
  1839   1839   }
  1840   1840   
................................................................................
  2394   2394     }
  2395   2395   
  2396   2396     return pParse->nErr;
  2397   2397   }
  2398   2398   #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */
  2399   2399   
  2400   2400   
  2401         -#if defined(SQLITE_ENABLE_STAT4) || defined(SQLITE_ENABLE_STAT3)
         2401  +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  2402   2402   /*
  2403   2403   ** Estimate the location of a particular key among all keys in an
  2404   2404   ** index.  Store the results in aStat as follows:
  2405   2405   **
  2406   2406   **    aStat[0]      Est. number of rows less than pVal
  2407   2407   **    aStat[1]      Est. number of rows equal to pVal
  2408   2408   **
................................................................................
  2481   2481         iGap = (iGap*2)/3;
  2482   2482       }else{
  2483   2483         iGap = iGap/3;
  2484   2484       }
  2485   2485       aStat[0] = iLower + iGap;
  2486   2486     }
  2487   2487   }
  2488         -#endif /* SQLITE_ENABLE_STAT4 */
         2488  +#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
  2489   2489   
  2490   2490   /*
  2491   2491   ** This function is used to estimate the number of rows that will be visited
  2492   2492   ** by scanning an index for a range of values. The range may have an upper
  2493   2493   ** bound, a lower bound, or both. The WHERE clause terms that set the upper
  2494   2494   ** and lower bounds are represented by pLower and pUpper respectively. For
  2495   2495   ** example, assuming that index p is on t1(a):
................................................................................
  2533   2533     WhereTerm *pLower,   /* Lower bound on the range. ex: "x>123" Might be NULL */
  2534   2534     WhereTerm *pUpper,   /* Upper bound on the range. ex: "x<455" Might be NULL */
  2535   2535     WhereCost *pnOut     /* IN/OUT: Number of rows visited */
  2536   2536   ){
  2537   2537     int rc = SQLITE_OK;
  2538   2538     int nOut = (int)*pnOut;
  2539   2539   
  2540         -#if defined(SQLITE_ENABLE_STAT4) || defined(SQLITE_ENABLE_STAT3)
         2540  +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  2541   2541     Index *p = pBuilder->pNew->u.btree.pIndex;
  2542   2542     int nEq = pBuilder->pNew->u.btree.nEq;
  2543   2543   
  2544   2544     if( nEq==pBuilder->nRecValid
  2545   2545      && nEq<p->nSampleCol
  2546   2546      && p->nSample 
  2547   2547      && OptimizationEnabled(pParse->db, SQLITE_Stat3) 
................................................................................
  2641   2641       nOut -= 20;        assert( 20==whereCost(4) );
  2642   2642     }
  2643   2643     if( nOut<10 ) nOut = 10;
  2644   2644     *pnOut = (WhereCost)nOut;
  2645   2645     return rc;
  2646   2646   }
  2647   2647   
  2648         -#if defined(SQLITE_ENABLE_STAT4) || defined(SQLITE_ENABLE_STAT3)
         2648  +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  2649   2649   /*
  2650   2650   ** Estimate the number of rows that will be returned based on
  2651   2651   ** an equality constraint x=VALUE and where that VALUE occurs in
  2652   2652   ** the histogram data.  This only works when x is the left-most
  2653   2653   ** column of an index and sqlite_stat3 histogram data is available
  2654   2654   ** for that index.  When pExpr==NULL that means the constraint is
  2655   2655   ** "x IS NULL" instead of "x=VALUE".
................................................................................
  2705   2705   
  2706   2706     whereKeyStats(pParse, p, pRec, 0, a);
  2707   2707     WHERETRACE(0x100,("equality scan regions: %d\n", (int)a[1]));
  2708   2708     *pnRow = a[1];
  2709   2709     
  2710   2710     return rc;
  2711   2711   }
  2712         -#endif /* defined(SQLITE_ENABLE_STAT4) */
         2712  +#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
  2713   2713   
  2714         -#if defined(SQLITE_ENABLE_STAT4) || defined(SQLITE_ENABLE_STAT3)
         2714  +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  2715   2715   /*
  2716   2716   ** Estimate the number of rows that will be returned based on
  2717   2717   ** an IN constraint where the right-hand side of the IN operator
  2718   2718   ** is a list of values.  Example:
  2719   2719   **
  2720   2720   **        WHERE x IN (1,2,3,4)
  2721   2721   **
................................................................................
  2753   2753       if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0];
  2754   2754       *pnRow = nRowEst;
  2755   2755       WHERETRACE(0x100,("IN row estimate: est=%g\n", nRowEst));
  2756   2756     }
  2757   2757     assert( pBuilder->nRecValid==nRecValid );
  2758   2758     return rc;
  2759   2759   }
  2760         -#endif /* defined(SQLITE_ENABLE_STAT4) */
         2760  +#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
  2761   2761   
  2762   2762   /*
  2763   2763   ** Disable a term in the WHERE clause.  Except, do not disable the term
  2764   2764   ** if it controls a LEFT OUTER JOIN and it did not originate in the ON
  2765   2765   ** or USING clause of that join.
  2766   2766   **
  2767   2767   ** Consider the term t2.z='ok' in the following queries:
................................................................................
  4298   4298     saved_wsFlags = pNew->wsFlags;
  4299   4299     saved_prereq = pNew->prereq;
  4300   4300     saved_nOut = pNew->nOut;
  4301   4301     pNew->rSetup = 0;
  4302   4302     rLogSize = estLog(whereCost(pProbe->aiRowEst[0]));
  4303   4303     for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){
  4304   4304       int nIn = 0;
  4305         -#if defined(SQLITE_ENABLE_STAT4) || defined(SQLITE_ENABLE_STAT3)
         4305  +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  4306   4306       int nRecValid = pBuilder->nRecValid;
  4307   4307       assert( pNew->nOut==saved_nOut );
  4308   4308       if( (pTerm->wtFlags & TERM_VNULL)!=0 && pSrc->pTab->aCol[iCol].notNull ){
  4309   4309         continue; /* skip IS NOT NULL constraints on a NOT NULL column */
  4310   4310       }
  4311   4311   #endif
  4312   4312       if( pTerm->prereqRight & pNew->maskSelf ) continue;
................................................................................
  4368   4368                        pNew->aLTerm[pNew->nLTerm-2] : 0;
  4369   4369       }
  4370   4370       if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
  4371   4371         /* Adjust nOut and rRun for STAT3 range values */
  4372   4372         assert( pNew->nOut==saved_nOut );
  4373   4373         whereRangeScanEst(pParse, pBuilder, pBtm, pTop, &pNew->nOut);
  4374   4374       }
  4375         -#if defined(SQLITE_ENABLE_STAT4) || defined(SQLITE_ENABLE_STAT3)
         4375  +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  4376   4376       if( nInMul==0 
  4377   4377        && pProbe->nSample 
  4378   4378        && pNew->u.btree.nEq<=pProbe->nSampleCol
  4379   4379        && OptimizationEnabled(db, SQLITE_Stat3) 
  4380   4380       ){
  4381   4381         Expr *pExpr = pTerm->pExpr;
  4382   4382         tRowcnt nOut = 0;
................................................................................
  4406   4406       rc = whereLoopInsert(pBuilder, pNew);
  4407   4407       if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0
  4408   4408        && pNew->u.btree.nEq<(pProbe->nColumn + (pProbe->zName!=0))
  4409   4409       ){
  4410   4410         whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn);
  4411   4411       }
  4412   4412       pNew->nOut = saved_nOut;
  4413         -#if defined(SQLITE_ENABLE_STAT4) || defined(SQLITE_ENABLE_STAT3)
         4413  +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  4414   4414       pBuilder->nRecValid = nRecValid;
  4415   4415   #endif
  4416   4416     }
  4417   4417     pNew->prereq = saved_prereq;
  4418   4418     pNew->u.btree.nEq = saved_nEq;
  4419   4419     pNew->wsFlags = saved_wsFlags;
  4420   4420     pNew->nOut = saved_nOut;
................................................................................
  4640   4640           }
  4641   4641           rc = whereLoopInsert(pBuilder, pNew);
  4642   4642           if( rc ) break;
  4643   4643         }
  4644   4644       }
  4645   4645   
  4646   4646       rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 0);
  4647         -#if defined(SQLITE_ENABLE_STAT4) || defined(SQLITE_ENABLE_STAT3)
         4647  +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  4648   4648       sqlite3Stat4ProbeFree(pBuilder->pRec);
  4649   4649       pBuilder->nRecValid = 0;
  4650   4650       pBuilder->pRec = 0;
  4651   4651   #endif
  4652   4652   
  4653   4653       /* If there was an INDEXED BY clause, then only that one index is
  4654   4654       ** considered. */