/ Check-in [affc2ef5]
Login

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

Overview
Comment:Add the btree_sample(INDEX,LOCATION,LIMIT) pragma.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | est_count_pragma
Files: files | file ages | folders
SHA1:affc2ef5ee3d8885e74051fd508a3d6f8c313857
User & Date: drh 2016-10-21 17:25:47
Context
2016-10-21
17:45
Merge updates from trunk, and especially the ".mode quote" enhancement to the shell. check-in: 0c8a5b88 user: drh tags: est_count_pragma
17:25
Add the btree_sample(INDEX,LOCATION,LIMIT) pragma. check-in: affc2ef5 user: drh tags: est_count_pragma
15:36
Fix problems in the est_count pragma for indexes and WITHOUT ROWID tables. check-in: c39fd9b8 user: drh tags: est_count_pragma
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/btree.c.

5091
5092
5093
5094
5095
5096
5097

5098
5099

5100
5101
5102
5103
5104
5105
5106
  }
  *pnRowEst = n*pPage->nCell;
  if( pPage->nCell==0 ){
    rx = 0;
  }else{
    perChild = mx/pPage->nCell;
    rx = x/perChild;

  }
  pCur->aiIdx[pCur->iPage] = rx;

  return SQLITE_OK;
}

/* Move the cursor so that it points to an entry near the key 
** specified by pIdxKey or intKey.   Return a success code.
**
** For INTKEY tables, the intKey parameter is used.  pIdxKey 







>


>







5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
  }
  *pnRowEst = n*pPage->nCell;
  if( pPage->nCell==0 ){
    rx = 0;
  }else{
    perChild = mx/pPage->nCell;
    rx = x/perChild;
    if( rx>=pPage->nCell ) rx = pPage->nCell-1;
  }
  pCur->aiIdx[pCur->iPage] = rx;

  return SQLITE_OK;
}

/* Move the cursor so that it points to an entry near the key 
** specified by pIdxKey or intKey.   Return a success code.
**
** For INTKEY tables, the intKey parameter is used.  pIdxKey 

Changes to src/pragma.c.

1426
1427
1428
1429
1430
1431
1432














































































1433
1434
1435
1436
1437
1438
1439
    sqlite3VdbeAddOp4Int(v, OP_OpenRead, 0, iRoot, iDb, 1);
    if( pIdx ) sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
    sqlite3VdbeAddOp3(v, OP_EstRowCnt, 0, regResult, (int)(r*1000000000));
    sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 1);
  }
  break;















































































#ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
# define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
#endif

#ifndef SQLITE_OMIT_INTEGRITY_CHECK
  /* Pragma "quick_check" is reduced version of 
  ** integrity_check designed to detect most database corruption







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
    sqlite3VdbeAddOp4Int(v, OP_OpenRead, 0, iRoot, iDb, 1);
    if( pIdx ) sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
    sqlite3VdbeAddOp3(v, OP_EstRowCnt, 0, regResult, (int)(r*1000000000));
    sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 1);
  }
  break;

  /*
  **   PRAGMA btree_sample(<table-or-index>,<fraction>,<limit>);
  **
  ** Seek in <table-or-index> through the first <fraction> of rows and
  ** then begin returning rows, one by one.  A max of <limit> rows will
  ** be returned.
  */
  case PragTyp_BTREE_SAMPLE: {
    Index *pIdx;
    Table *pTab = 0;
    Pgno iRoot = 0;
    int nCol = 0;
    const char *zName = 0;
    int iLimit = 10;
    int i;
    int regResult;
    int regLimit;
    int addrTop;
    int addrJmp;
    int addrSkip;
    double r;
    if( (pIdx = sqlite3FindIndex(db, zRight, zDb))!=0 ){
      iRoot = pIdx->tnum;
      zName = pIdx->zName;
      nCol = pIdx->nColumn;
    }else if( (pTab = sqlite3FindTable(db, zRight, zDb))!=0 ){
      zName = pTab->zName;
      if( HasRowid(pTab) ){
        iRoot = pTab->tnum;
        nCol = pTab->nCol;
      }else{
        pIdx = sqlite3PrimaryKeyIndex(pTab);
        iRoot = pIdx->tnum;
        nCol = pIdx->nColumn;
      }
    }else{
      break;
    }
    sqlite3VdbeSetNumCols(v, nCol);
    for(i=0; i<nCol; i++){
      char zCol[30];
      sqlite3_snprintf(sizeof(zCol),zCol,"c%06d",i);
      sqlite3VdbeSetColName(v, i, COLNAME_NAME, zCol, SQLITE_TRANSIENT);
    }
    if( pValues->nId>=2 ){
      const char *z = pValues->a[1].zName;
      sqlite3AtoF(z, &r, sqlite3Strlen30(z), SQLITE_UTF8);
    }else{
      r = 0.5;
    }
    if( r<0.0 ) r = 0.0;
    if( r>1.0 ) r = 1.0;
    if( pValues->nId>=3 ){
      iLimit = sqlite3Atoi(pValues->a[2].zName);
    }
    pParse->nTab++;
    sqlite3TableLock(pParse, iDb, iRoot, 0, zName);
    sqlite3CodeVerifySchema(pParse, iDb);
    sqlite3VdbeAddOp4Int(v, OP_OpenRead, 0, iRoot, iDb, nCol);
    if( pIdx ) sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
    regLimit = ++pParse->nMem;
    regResult = pParse->nMem+1;
    pParse->nMem += nCol;
    sqlite3VdbeAddOp2(v, OP_Integer, iLimit, regLimit);
    addrSkip = sqlite3VdbeAddOp1(v, OP_Rewind, 0); VdbeCoverage(v);
    sqlite3VdbeAddOp3(v, OP_EstRowCnt, 0, regResult, (int)(r*1000000000));
    addrTop = sqlite3VdbeCurrentAddr(v);
    for(i=0; i<nCol; i++){
      sqlite3VdbeAddOp3(v, OP_Column, 0, i, regResult+i);
    }
    sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, nCol);
    addrJmp = sqlite3VdbeAddOp1(v, OP_DecrJumpZero, regLimit); VdbeCoverage(v);
    sqlite3VdbeAddOp2(v, OP_Next, 0, addrTop); VdbeCoverage(v);
    sqlite3VdbeJumpHere(v, addrJmp);
    sqlite3VdbeJumpHere(v, addrSkip);
  }
  break;

#ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
# define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
#endif

#ifndef SQLITE_OMIT_INTEGRITY_CHECK
  /* Pragma "quick_check" is reduced version of 
  ** integrity_check designed to detect most database corruption

Changes to src/pragma.h.

2
3
4
5
6
7
8

9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
..
77
78
79
80
81
82
83




84
85
86
87
88
89
90
...
462
463
464
465
466
467
468
469
** This file is automatically generated by the script at
** ../tool/mkpragmatab.tcl.  To update the set of pragmas, edit
** that script and rerun it.
*/
#define PragTyp_HEADER_VALUE                   0
#define PragTyp_AUTO_VACUUM                    1
#define PragTyp_FLAG                           2

#define PragTyp_BUSY_TIMEOUT                   3
#define PragTyp_CACHE_SIZE                     4
#define PragTyp_CACHE_SPILL                    5
#define PragTyp_CASE_SENSITIVE_LIKE            6
#define PragTyp_COLLATION_LIST                 7
#define PragTyp_COMPILE_OPTIONS                8
#define PragTyp_DATA_STORE_DIRECTORY           9
#define PragTyp_DATABASE_LIST                 10
#define PragTyp_DEFAULT_CACHE_SIZE            11
#define PragTyp_ENCODING                      12
#define PragTyp_EST_COUNT                     13
#define PragTyp_FOREIGN_KEY_CHECK             14
#define PragTyp_FOREIGN_KEY_LIST              15
#define PragTyp_INCREMENTAL_VACUUM            16
#define PragTyp_INDEX_INFO                    17
#define PragTyp_INDEX_LIST                    18
#define PragTyp_INTEGRITY_CHECK               19
#define PragTyp_JOURNAL_MODE                  20
#define PragTyp_JOURNAL_SIZE_LIMIT            21
#define PragTyp_LOCK_PROXY_FILE               22
#define PragTyp_LOCKING_MODE                  23
#define PragTyp_PAGE_COUNT                    24
#define PragTyp_MMAP_SIZE                     25
#define PragTyp_PAGE_SIZE                     26
#define PragTyp_SECURE_DELETE                 27
#define PragTyp_SHRINK_MEMORY                 28
#define PragTyp_SOFT_HEAP_LIMIT               29
#define PragTyp_STATS                         30
#define PragTyp_SYNCHRONOUS                   31
#define PragTyp_TABLE_INFO                    32
#define PragTyp_TEMP_STORE                    33
#define PragTyp_TEMP_STORE_DIRECTORY          34
#define PragTyp_THREADS                       35
#define PragTyp_WAL_AUTOCHECKPOINT            36
#define PragTyp_WAL_CHECKPOINT                37
#define PragTyp_ACTIVATE_EXTENSIONS           38
#define PragTyp_HEXKEY                        39
#define PragTyp_KEY                           40
#define PragTyp_REKEY                         41
#define PragTyp_LOCK_STATUS                   42
#define PragTyp_PARSER_TRACE                  43
#define PragFlag_NeedSchema           0x01
#define PragFlag_ReadOnly             0x02
static const struct sPragmaNames {
  const char *const zName;  /* Name of pragma */
  u8 ePragTyp;              /* PragTyp_XXX value */
  u8 mPragFlag;             /* Zero or more PragFlag_XXX values */
  u32 iArg;                 /* Extra argument */
................................................................................
#if !defined(SQLITE_OMIT_AUTOMATIC_INDEX)
  { /* zName:     */ "automatic_index",
    /* ePragTyp:  */ PragTyp_FLAG,
    /* ePragFlag: */ 0,
    /* iArg:      */ SQLITE_AutoIndex },
#endif
#endif




  { /* zName:     */ "busy_timeout",
    /* ePragTyp:  */ PragTyp_BUSY_TIMEOUT,
    /* ePragFlag: */ 0,
    /* iArg:      */ 0 },
#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
  { /* zName:     */ "cache_size",
    /* ePragTyp:  */ PragTyp_CACHE_SIZE,
................................................................................
#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
  { /* zName:     */ "writable_schema",
    /* ePragTyp:  */ PragTyp_FLAG,
    /* ePragFlag: */ 0,
    /* iArg:      */ SQLITE_WriteSchema|SQLITE_RecoveryMode },
#endif
};
/* Number of pragmas: 61 on by default, 74 total. */







>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|







 







>
>
>
>







 







|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
..
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
...
467
468
469
470
471
472
473
474
** This file is automatically generated by the script at
** ../tool/mkpragmatab.tcl.  To update the set of pragmas, edit
** that script and rerun it.
*/
#define PragTyp_HEADER_VALUE                   0
#define PragTyp_AUTO_VACUUM                    1
#define PragTyp_FLAG                           2
#define PragTyp_BTREE_SAMPLE                   3
#define PragTyp_BUSY_TIMEOUT                   4
#define PragTyp_CACHE_SIZE                     5
#define PragTyp_CACHE_SPILL                    6
#define PragTyp_CASE_SENSITIVE_LIKE            7
#define PragTyp_COLLATION_LIST                 8
#define PragTyp_COMPILE_OPTIONS                9
#define PragTyp_DATA_STORE_DIRECTORY          10
#define PragTyp_DATABASE_LIST                 11
#define PragTyp_DEFAULT_CACHE_SIZE            12
#define PragTyp_ENCODING                      13
#define PragTyp_EST_COUNT                     14
#define PragTyp_FOREIGN_KEY_CHECK             15
#define PragTyp_FOREIGN_KEY_LIST              16
#define PragTyp_INCREMENTAL_VACUUM            17
#define PragTyp_INDEX_INFO                    18
#define PragTyp_INDEX_LIST                    19
#define PragTyp_INTEGRITY_CHECK               20
#define PragTyp_JOURNAL_MODE                  21
#define PragTyp_JOURNAL_SIZE_LIMIT            22
#define PragTyp_LOCK_PROXY_FILE               23
#define PragTyp_LOCKING_MODE                  24
#define PragTyp_PAGE_COUNT                    25
#define PragTyp_MMAP_SIZE                     26
#define PragTyp_PAGE_SIZE                     27
#define PragTyp_SECURE_DELETE                 28
#define PragTyp_SHRINK_MEMORY                 29
#define PragTyp_SOFT_HEAP_LIMIT               30
#define PragTyp_STATS                         31
#define PragTyp_SYNCHRONOUS                   32
#define PragTyp_TABLE_INFO                    33
#define PragTyp_TEMP_STORE                    34
#define PragTyp_TEMP_STORE_DIRECTORY          35
#define PragTyp_THREADS                       36
#define PragTyp_WAL_AUTOCHECKPOINT            37
#define PragTyp_WAL_CHECKPOINT                38
#define PragTyp_ACTIVATE_EXTENSIONS           39
#define PragTyp_HEXKEY                        40
#define PragTyp_KEY                           41
#define PragTyp_REKEY                         42
#define PragTyp_LOCK_STATUS                   43
#define PragTyp_PARSER_TRACE                  44
#define PragFlag_NeedSchema           0x01
#define PragFlag_ReadOnly             0x02
static const struct sPragmaNames {
  const char *const zName;  /* Name of pragma */
  u8 ePragTyp;              /* PragTyp_XXX value */
  u8 mPragFlag;             /* Zero or more PragFlag_XXX values */
  u32 iArg;                 /* Extra argument */
................................................................................
#if !defined(SQLITE_OMIT_AUTOMATIC_INDEX)
  { /* zName:     */ "automatic_index",
    /* ePragTyp:  */ PragTyp_FLAG,
    /* ePragFlag: */ 0,
    /* iArg:      */ SQLITE_AutoIndex },
#endif
#endif
  { /* zName:     */ "btree_sample",
    /* ePragTyp:  */ PragTyp_BTREE_SAMPLE,
    /* ePragFlag: */ PragFlag_NeedSchema,
    /* iArg:      */ 0 },
  { /* zName:     */ "busy_timeout",
    /* ePragTyp:  */ PragTyp_BUSY_TIMEOUT,
    /* ePragFlag: */ 0,
    /* iArg:      */ 0 },
#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
  { /* zName:     */ "cache_size",
    /* ePragTyp:  */ PragTyp_CACHE_SIZE,
................................................................................
#if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
  { /* zName:     */ "writable_schema",
    /* ePragTyp:  */ PragTyp_FLAG,
    /* ePragFlag: */ 0,
    /* iArg:      */ SQLITE_WriteSchema|SQLITE_RecoveryMode },
#endif
};
/* Number of pragmas: 62 on by default, 75 total. */

Changes to src/vdbe.c.

4915
4916
4917
4918
4919
4920
4921



4922
4923
4924
4925
4926
4927
4928
  pCrsr = pC->uc.pCursor;
  assert( pCrsr );
  rc = sqlite3BtreeMovetoProportional(pCrsr, pOp->p3, &n);
  if( rc ) goto abort_due_to_error;
  pOut = out2Prerelease(p, pOp);
  pOut->flags = MEM_Real;
  pOut->u.r = n;



  break;
}

/* Opcode: Next P1 P2 P3 P4 P5
**
** Advance cursor P1 so that it points to the next key/data pair in its
** table or index.  If there are no more key/value pairs then fall through







>
>
>







4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
  pCrsr = pC->uc.pCursor;
  assert( pCrsr );
  rc = sqlite3BtreeMovetoProportional(pCrsr, pOp->p3, &n);
  if( rc ) goto abort_due_to_error;
  pOut = out2Prerelease(p, pOp);
  pOut->flags = MEM_Real;
  pOut->u.r = n;
  pC->nullRow = 0;
  pC->deferredMoveto = 0;
  pC->cacheStatus = CACHE_STALE;
  break;
}

/* Opcode: Next P1 P2 P3 P4 P5
**
** Advance cursor P1 so that it points to the next key/data pair in its
** table or index.  If there are no more key/value pairs then fall through

Changes to tool/mkpragmatab.tcl.

249
250
251
252
253
254
255



256
257
258
259
260
261
262
  NAME: quick_check
  TYPE: INTEGRITY_CHECK
  FLAG: NeedSchema
  IF:   !defined(SQLITE_OMIT_INTEGRITY_CHECK)

  NAME: est_count
  FLAG: NeedSchema




  NAME: encoding
  IF:   !defined(SQLITE_OMIT_UTF16)

  NAME: schema_version
  TYPE: HEADER_VALUE
  ARG:  BTREE_SCHEMA_VERSION







>
>
>







249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
  NAME: quick_check
  TYPE: INTEGRITY_CHECK
  FLAG: NeedSchema
  IF:   !defined(SQLITE_OMIT_INTEGRITY_CHECK)

  NAME: est_count
  FLAG: NeedSchema

  NAME: btree_sample
  FLAG: NeedSchema

  NAME: encoding
  IF:   !defined(SQLITE_OMIT_UTF16)

  NAME: schema_version
  TYPE: HEADER_VALUE
  ARG:  BTREE_SCHEMA_VERSION