/ Check-in [4229caec]
Login

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

Overview
Comment:Fix errors in the table resize detection.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | auto-analyze
Files: files | file ages | folders
SHA1: 4229caec0b60a1617b9d5ff94b47271cbd7be1e0
User & Date: drh 2017-02-18 02:42:54
Context
2017-02-18
15:58
Add the OP_SqlExec opcode and use it to implement "PRAGMA analyze_as_needed", invoking ANALYZE subcommands as necessary. This simplifies the implementation. check-in: d386015f user: drh tags: auto-analyze
02:42
Fix errors in the table resize detection. check-in: 4229caec user: drh tags: auto-analyze
02:19
In the analyze_as_needed pragma, avoid running unnecessary OP_LoadAnalysis and OP_Expire opcodes. Make the analyze_as_needed pragma responsive to the schema name. check-in: 882599a4 user: drh tags: auto-analyze
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/analyze.c.

1015
1016
1017
1018
1019
1020
1021

1022
1023
1024
1025
1026
1027
1028
....
1292
1293
1294
1295
1296
1297
1298
1299
1300

1301
1302
1303
1304
1305
1306
1307
1308
  sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
  iTabCur = iTab++;
  iIdxCur = iTab++;
  pParse->nTab = MAX(pParse->nTab, iTab);
  sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
  if( szOld>0 ){
    addrSizeCk = sqlite3VdbeAddOp3(v, OP_IfSmaller, iTabCur, 0, szOld);

  }
  sqlite3VdbeLoadString(v, regTabname, pTab->zName);

  for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
    int nCol;                     /* Number of columns in pIdx. "N" */
    int addrRewind;               /* Address of "OP_Rewind iIdxCur" */
    int addrNextRow;              /* Address of "next_row:" */
................................................................................
** than the threshold. Not that the threshold is a logarithmic LogEst
** value.
*/
static int analyzeNeeded(Table *pTab, LogEst *pThreshold){
  Index *pIdx;
  if( (pTab->tabFlags & TF_StatsUsed)==0 ) return 0;

  /* If TF_StatsUsed is true, then we might need to reanalyze.  But
  ** only reanalyze if the table size has grown by a factor of 10 or more */

  *pThreshold = pTab->nRowLogEst + 33;  assert( sqlite3LogEst(10)==33 );

  /* Except, if any of the indexes of the table do not have valid
  ** sqlite_stat1 entries, then set the size threshold to zero to
  ** ensure the analysis will always occur. */
  for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
    if( !pIdx->hasStat1 ){
      *pThreshold = 0;







>







 







|
|
>
|







1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
....
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
  sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
  iTabCur = iTab++;
  iIdxCur = iTab++;
  pParse->nTab = MAX(pParse->nTab, iTab);
  sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
  if( szOld>0 ){
    addrSizeCk = sqlite3VdbeAddOp3(v, OP_IfSmaller, iTabCur, 0, szOld);
    VdbeCoverage(v);
  }
  sqlite3VdbeLoadString(v, regTabname, pTab->zName);

  for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
    int nCol;                     /* Number of columns in pIdx. "N" */
    int addrRewind;               /* Address of "OP_Rewind iIdxCur" */
    int addrNextRow;              /* Address of "next_row:" */
................................................................................
** than the threshold. Not that the threshold is a logarithmic LogEst
** value.
*/
static int analyzeNeeded(Table *pTab, LogEst *pThreshold){
  Index *pIdx;
  if( (pTab->tabFlags & TF_StatsUsed)==0 ) return 0;

  /* If TF_StatsUsed is true, then we might need to reanalyze.
  ** TUNING: Only reanalyze if the table size has grown by a factor
  ** of 25 or more. */
  *pThreshold = pTab->nRowLogEst + 46;  assert( sqlite3LogEst(25)==46 );

  /* Except, if any of the indexes of the table do not have valid
  ** sqlite_stat1 entries, then set the size threshold to zero to
  ** ensure the analysis will always occur. */
  for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
    if( !pIdx->hasStat1 ){
      *pThreshold = 0;

Changes to src/btree.c.

5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
i64 sqlite3BtreeRowCountEst(BtCursor *pCur){
  i64 n;
  u8 i;

  assert( cursorOwnsBtShared(pCur) );
  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
  if( pCur->eState!=CURSOR_VALID ) return -1;
  if( pCur->apPage[pCur->iPage-1]->leaf==0 ) return -1;
  for(n=1, i=0; i<pCur->iPage; i++){
    n *= pCur->apPage[i]->nCell;
  }
  return n;
}

/*
** Advance the cursor to the next entry in the database.  If







|
|







5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
i64 sqlite3BtreeRowCountEst(BtCursor *pCur){
  i64 n;
  u8 i;

  assert( cursorOwnsBtShared(pCur) );
  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
  if( pCur->eState!=CURSOR_VALID ) return -1;
  if( pCur->apPage[pCur->iPage]->leaf==0 ) return -1;
  for(n=1, i=0; i<=pCur->iPage; i++){
    n *= pCur->apPage[i]->nCell;
  }
  return n;
}

/*
** Advance the cursor to the next entry in the database.  If