/ Check-in [ec3ffb17]
Login

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

Overview
Comment:Fix a bug in calculating the average number of entries for keys not present in the sqlite_stat4 table.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | sqlite_stat4
Files: files | file ages | folders
SHA1: ec3ffb174844406a6186c3dcc41b76d0331b502c
User & Date: dan 2013-08-12 11:21:10
Context
2013-08-12
16:34
Re-enable reading from the sqlite_stat3 table (as well as sqlite_stat4). check-in: 6d45078e user: dan tags: sqlite_stat4
11:21
Fix a bug in calculating the average number of entries for keys not present in the sqlite_stat4 table. check-in: ec3ffb17 user: dan tags: sqlite_stat4
09:29
Fix minor problems caused by adding the rowid to the records in stat4. check-in: 088d1ff9 user: dan tags: sqlite_stat4
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/analyze.c.

  1278   1278     sqlite3DbFree(db, zSql);
  1279   1279     if( rc ) return rc;
  1280   1280   
  1281   1281     while( sqlite3_step(pStmt)==SQLITE_ROW ){
  1282   1282       char *zIndex;   /* Index name */
  1283   1283       Index *pIdx;    /* Pointer to the index object */
  1284   1284       int i;          /* Loop counter */
  1285         -    tRowcnt sumEq;  /* Sum of the nEq values */
  1286   1285       int nCol;       /* Number of columns in index */
  1287   1286   
  1288   1287       zIndex = (char *)sqlite3_column_text(pStmt, 0);
  1289   1288       if( zIndex==0 ) continue;
  1290   1289       pIdx = sqlite3FindIndex(db, zIndex, zDb);
  1291   1290       if( pIdx==0 ) continue;
  1292   1291       if( pIdx==pPrevIdx ){
................................................................................
  1300   1299   
  1301   1300       nCol = pIdx->nColumn+1;
  1302   1301       decodeIntArray((char*)sqlite3_column_text(pStmt,1), nCol, pSample->anEq, 0);
  1303   1302       decodeIntArray((char*)sqlite3_column_text(pStmt,2), nCol, pSample->anLt, 0);
  1304   1303       decodeIntArray((char*)sqlite3_column_text(pStmt,3), nCol, pSample->anDLt,0);
  1305   1304   
  1306   1305       if( idx==pIdx->nSample-1 ){
         1306  +      IndexSample *aSample = pIdx->aSample;
  1307   1307         int iCol;
  1308   1308         for(iCol=0; iCol<pIdx->nColumn; iCol++){
         1309  +        tRowcnt sumEq = 0;        /* Sum of the nEq values */
         1310  +        int nSum = 0;             /* Number of terms contributing to sumEq */
  1309   1311           tRowcnt avgEq = 0;
  1310   1312           tRowcnt nDLt = pSample->anDLt[iCol];
  1311         -        if( nDLt>idx ){
  1312         -          for(i=0, sumEq=0; i<idx; i++) sumEq += pIdx->aSample[i].anEq[iCol];
  1313         -          avgEq = (pSample->anLt[iCol] - sumEq)/(nDLt - idx);
         1313  +
         1314  +        /* Set nSum to the number of distinct (iCol+1) field prefixes that
         1315  +        ** occur in the stat4 table for this index before pSample. Set
         1316  +        ** sumEq to the sum of the nEq values for column iCol for the same
         1317  +        ** set (adding the value only once where there exist dupicate 
         1318  +        ** prefixes).  */
         1319  +        for(i=0; i<(pIdx->nSample-1); i++){
         1320  +          if( aSample[i].anDLt[iCol]!=aSample[i+1].anDLt[iCol] ){
         1321  +            sumEq += aSample[i].anEq[iCol];
         1322  +            nSum++;
         1323  +          }
         1324  +        }
         1325  +        if( nDLt>nSum ){
         1326  +          avgEq = (pSample->anLt[iCol] - sumEq)/(nDLt - nSum);
  1314   1327           }
  1315   1328           if( avgEq==0 ) avgEq = 1;
  1316   1329           pIdx->aAvgEq[iCol] = avgEq;
  1317   1330         }
  1318   1331       }
  1319   1332   
  1320   1333       pSample->n = sqlite3_column_bytes(pStmt, 4);