Skip to content

Commit

Permalink
Improve the performance of ANALYZE when SQLITE_ENABLE_STAT4 is defined.
Browse files Browse the repository at this point in the history
  • Loading branch information
danielk-1977 committed Feb 22, 2017
1 parent c6b4b1d commit 0779941
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/analyze.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ struct Stat4Accum {
Stat4Sample *aBest; /* Array of nCol best samples */
int iMin; /* Index in a[] of entry with minimum score */
int nSample; /* Current number of samples */
int nMaxEqZero; /* Max leading 0 in anEq[] for any a[] entry */
int iGet; /* Index of current sample accessed by stat_get() */
Stat4Sample *a; /* Array of mxSample Stat4Sample objects */
sqlite3 *db; /* Database connection, for malloc() */
Expand Down Expand Up @@ -551,7 +552,14 @@ static void sampleInsert(Stat4Accum *p, Stat4Sample *pNew, int nEqZero){
Stat4Sample *pSample = 0;
int i;

/* Stat4Accum.nMaxEqZero is set to the maximum number of leading 0
** values in the anEq[] array of any sample in Stat4Accum.a[]. In
** other words, if nMaxEqZero is n, then it is guaranteed that there
** are no samples with Stat4Sample.anEq[m]==0 for (m>=n). */
assert( IsStat4 || nEqZero==0 );
if( nEqZero>p->nMaxEqZero ){
p->nMaxEqZero = nEqZero;
}

#ifdef SQLITE_ENABLE_STAT4
if( pNew->isPSample==0 ){
Expand Down Expand Up @@ -651,12 +659,22 @@ static void samplePushPrevious(Stat4Accum *p, int iChng){
}
}

/* Update the anEq[] fields of any samples already collected. */
/* Check that no sample contains an anEq[] entry with an index of
** p->nMaxEqZero or greater set to zero. */
for(i=p->nSample-1; i>=0; i--){
int j;
for(j=iChng; j<p->nCol; j++){
if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j];
for(j=p->nMaxEqZero; j<p->nCol; j++) assert( p->a[i].anEq[j]>0 );
}

/* Update the anEq[] fields of any samples already collected. */
if( iChng<p->nMaxEqZero ){
for(i=p->nSample-1; i>=0; i--){
int j;
for(j=iChng; j<p->nCol; j++){
if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j];
}
}
p->nMaxEqZero = iChng;
}
#endif

Expand Down

0 comments on commit 0779941

Please sign in to comment.