Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Add the sqlite_stat3.nDLT field. Use an linear congruence PRNG to choose which samples to select from among those with the same nEq field. |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | stat3-enhancement |
Files: | files | file ages | folders |
SHA1: |
1dcd24283e6c1cc638eb9ffac4340464 |
User & Date: | drh 2011-08-13 15:25:10.607 |
Context
2011-08-13
| ||
19:35 | Further testing and bug fixing for sqlite_stat3. Added the Index.avgEq field to index statistics. Fixed several problems in the query planner associated with stat3. (check-in: 89b2f70884 user: drh tags: stat3-enhancement) | |
15:25 | Add the sqlite_stat3.nDLT field. Use an linear congruence PRNG to choose which samples to select from among those with the same nEq field. (check-in: 1dcd24283e user: drh tags: stat3-enhancement) | |
00:58 | The ANALYZE command picks for 15 samples for sqlite_stat3 with the largest nEq fields, plus 5 other evenly spaced samples. (check-in: 8225924ea0 user: drh tags: stat3-enhancement) | |
Changes
Changes to src/analyze.c.
︙ | ︙ | |||
15 16 17 18 19 20 21 | ** and indices. These statistics are made available to the query planner ** to help it make better decisions about how to perform queries. ** ** The following system tables are or have been supported: ** ** CREATE TABLE sqlite_stat1(tbl, idx, stat); ** CREATE TABLE sqlite_stat2(tbl, idx, sampleno, sample); | | | 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | ** and indices. These statistics are made available to the query planner ** to help it make better decisions about how to perform queries. ** ** The following system tables are or have been supported: ** ** CREATE TABLE sqlite_stat1(tbl, idx, stat); ** CREATE TABLE sqlite_stat2(tbl, idx, sampleno, sample); ** CREATE TABLE sqlite_stat3(tbl, idx, nEq, nLt, nDLt, sample); ** ** Additional tables might be added in future releases of SQLite. ** The sqlite_stat2 table is not created or used unless the SQLite version ** is between 3.6.18 and 3.7.7, inclusive, and unless SQLite is compiled ** with SQLITE_ENABLE_STAT2. The sqlite_stat2 table is deprecated. ** The sqlite_stat2 table is superceded by sqlite_stat3, which is only ** created and used by SQLite versions after 2011-08-09 with |
︙ | ︙ | |||
84 85 86 87 88 89 90 | ** Format for sqlite_stat3: ** ** The sqlite_stat3 is an enhancement to sqlite_stat2. A new name is ** used to avoid compatibility problems. ** ** The format of the sqlite_stat3 table is similar to the format for ** the sqlite_stat2 table, with the following changes: (1) | | | | > | | | | 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | ** Format for sqlite_stat3: ** ** The sqlite_stat3 is an enhancement to sqlite_stat2. A new name is ** used to avoid compatibility problems. ** ** The format of the sqlite_stat3 table is similar to the format for ** the sqlite_stat2 table, with the following changes: (1) ** The sampleno column is removed. (2) Every sample has nEq, nLt, and nDLt ** columns which hold the approximate number of rows in the table that ** exactly match the sample, the approximate number of rows with values ** less than the sample, and the approximate number of distinct key values ** less than the sample, respectively. (3) The number of samples can very ** from one table to the next; the sample count does not have to be ** exactly 10 as it is with sqlite_stat2. ** ** The ANALYZE command will typically generate sqlite_stat3 tables ** that contain between 10 and 40 samples which are distributed across ** the key space, though not uniformly, and which include samples with ** largest possible nEq values. */ #ifndef SQLITE_OMIT_ANALYZE |
︙ | ︙ | |||
128 129 130 131 132 133 134 | ){ static const struct { const char *zName; const char *zCols; } aTable[] = { { "sqlite_stat1", "tbl,idx,stat" }, #ifdef SQLITE_ENABLE_STAT3 | | | 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | ){ static const struct { const char *zName; const char *zCols; } aTable[] = { { "sqlite_stat1", "tbl,idx,stat" }, #ifdef SQLITE_ENABLE_STAT3 { "sqlite_stat3", "tbl,idx,neq,nlt,ndlt,sample" }, #endif }; static const char *azToDrop[] = { "sqlite_stat2", #ifndef SQLITE_ENABLE_STAT3 "sqlite_stat3", #endif |
︙ | ︙ | |||
203 204 205 206 207 208 209 | } } /* ** Recommended number of samples for sqlite_stat3 */ #ifndef SQLITE_STAT3_SAMPLES | | > > | 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | } } /* ** Recommended number of samples for sqlite_stat3 */ #ifndef SQLITE_STAT3_SAMPLES # define SQLITE_STAT3_SAMPLES 24 #endif /* ** Three SQL functions - stat3_init(), stat3_push(), and stat3_pop() - ** share an instance of the following structure to hold their state ** information. */ typedef struct Stat3Accum Stat3Accum; struct Stat3Accum { tRowcnt nRow; /* Number of rows in the entire table */ tRowcnt nPSample; /* How often to do a periodic sample */ int iMin; /* Index of entry with minimum nEq and hash */ int mxSample; /* Maximum number of samples to accumulate */ int nSample; /* Current number of samples */ u32 iPrn; /* Pseudo-random number used for sampling */ struct Stat3Sample { i64 iRowid; /* Rowid in main table of the key */ tRowcnt nEq; /* sqlite_stat3.nEq */ tRowcnt nLt; /* sqlite_stat3.nLt */ tRowcnt nDLt; /* sqlite_stat3.nDLt */ u8 isPSample; /* True if a periodic sample */ u32 iHash; /* Tiebreaker hash */ } *a; /* An array of samples */ }; #ifdef SQLITE_ENABLE_STAT3 /* |
︙ | ︙ | |||
259 260 261 262 263 264 265 | sqlite3_result_error_nomem(context); return; } memset(p, 0, n); p->a = (struct Stat3Sample*)&p[1]; p->nRow = nRow; p->mxSample = mxSample; | | > | | > | | < < < < | > > > | > | 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 | sqlite3_result_error_nomem(context); return; } memset(p, 0, n); p->a = (struct Stat3Sample*)&p[1]; p->nRow = nRow; p->mxSample = mxSample; p->nPSample = p->nRow/(mxSample/3+1) + 1; sqlite3_randomness(sizeof(p->iPrn), &p->iPrn); sqlite3_result_blob(context, p, sizeof(p), sqlite3_free); } static const FuncDef stat3InitFuncdef = { 2, /* nArg */ SQLITE_UTF8, /* iPrefEnc */ 0, /* flags */ 0, /* pUserData */ 0, /* pNext */ stat3Init, /* xFunc */ 0, /* xStep */ 0, /* xFinalize */ "stat3_init", /* zName */ 0, /* pHash */ 0 /* pDestructor */ }; /* ** Implementation of the stat3_push(nEq,nLt,nDLt,rowid,P) SQL function. The ** arguments describe a single key instance. This routine makes the ** decision about whether or not to retain this key for the sqlite_stat3 ** table. ** ** The return value is NULL. */ static void stat3Push( sqlite3_context *context, int argc, sqlite3_value **argv ){ Stat3Accum *p = (Stat3Accum*)sqlite3_value_blob(argv[4]); tRowcnt nEq = sqlite3_value_int64(argv[0]); tRowcnt nLt = sqlite3_value_int64(argv[1]); tRowcnt nDLt = sqlite3_value_int64(argv[2]); i64 rowid = sqlite3_value_int64(argv[3]); u8 isPSample = 0; u8 doInsert = 0; int iMin = p->iMin; struct Stat3Sample *pSample; int i; u32 h; if( nEq==0 ) return; h = p->iPrn = p->iPrn*1103515245 + 12345; if( (nLt/p->nPSample)!=((nEq+nLt)/p->nPSample) ){ doInsert = isPSample = 1; }else if( p->nSample<p->mxSample ){ doInsert = 1; }else{ if( nEq>p->a[iMin].nEq || (nEq==p->a[iMin].nEq && h>p->a[iMin].iHash) ){ doInsert = 1; } } if( !doInsert ) return; if( p->nSample==p->mxSample ){ if( iMin<p->nSample ){ memcpy(&p->a[iMin], &p->a[iMin+1], sizeof(p->a[0])*(p->nSample-iMin)); } pSample = &p->a[p->nSample-1]; }else{ pSample = &p->a[p->nSample++]; } pSample->iRowid = rowid; pSample->nEq = nEq; pSample->nLt = nLt; pSample->nDLt = nDLt; pSample->iHash = h; pSample->isPSample = isPSample; /* Find the new minimum */ if( p->nSample==p->mxSample ){ pSample = p->a; i = 0; |
︙ | ︙ | |||
354 355 356 357 358 359 360 | h = pSample->iHash; } } p->iMin = iMin; } } static const FuncDef stat3PushFuncdef = { | | | 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 | h = pSample->iHash; } } p->iMin = iMin; } } static const FuncDef stat3PushFuncdef = { 5, /* nArg */ SQLITE_UTF8, /* iPrefEnc */ 0, /* flags */ 0, /* pUserData */ 0, /* pNext */ stat3Push, /* xFunc */ 0, /* xStep */ 0, /* xFinalize */ |
︙ | ︙ | |||
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 | ** used to query the results. Content is returned for the Nth sqlite_stat3 ** row where N is between 0 and S-1 and S is the number of samples. The ** value returned depends on the number of arguments. ** ** argc==2 result: rowid ** argc==3 result: nEq ** argc==4 result: nLt */ static void stat3Get( sqlite3_context *context, int argc, sqlite3_value **argv ){ int n = sqlite3_value_int(argv[1]); Stat3Accum *p = (Stat3Accum*)sqlite3_value_blob(argv[0]); assert( p!=0 ); if( p->nSample<=n ) return; switch( argc ){ case 2: sqlite3_result_int64(context, p->a[n].iRowid); break; case 3: sqlite3_result_int64(context, p->a[n].nEq); break; case 4: sqlite3_result_int64(context, p->a[n].nLt); break; } } static const FuncDef stat3GetFuncdef = { -1, /* nArg */ SQLITE_UTF8, /* iPrefEnc */ 0, /* flags */ 0, /* pUserData */ | > > | 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 | ** used to query the results. Content is returned for the Nth sqlite_stat3 ** row where N is between 0 and S-1 and S is the number of samples. The ** value returned depends on the number of arguments. ** ** argc==2 result: rowid ** argc==3 result: nEq ** argc==4 result: nLt ** argc==5 result: nDLt */ static void stat3Get( sqlite3_context *context, int argc, sqlite3_value **argv ){ int n = sqlite3_value_int(argv[1]); Stat3Accum *p = (Stat3Accum*)sqlite3_value_blob(argv[0]); assert( p!=0 ); if( p->nSample<=n ) return; switch( argc ){ case 2: sqlite3_result_int64(context, p->a[n].iRowid); break; case 3: sqlite3_result_int64(context, p->a[n].nEq); break; case 4: sqlite3_result_int64(context, p->a[n].nLt); break; case 5: sqlite3_result_int64(context, p->a[n].nDLt); break; } } static const FuncDef stat3GetFuncdef = { -1, /* nArg */ SQLITE_UTF8, /* iPrefEnc */ 0, /* flags */ 0, /* pUserData */ |
︙ | ︙ | |||
437 438 439 440 441 442 443 444 445 446 447 448 449 450 | int iDb; /* Index of database containing pTab */ int regTabname = iMem++; /* Register containing table name */ int regIdxname = iMem++; /* Register containing index name */ int regStat1 = iMem++; /* The stat column of sqlite_stat1 */ #ifdef SQLITE_ENABLE_STAT3 int regNumEq = regStat1; /* Number of instances. Same as regStat1 */ int regNumLt = iMem++; /* Number of keys less than regSample */ int regSample = iMem++; /* The next sample value */ int regRowid = regSample; /* Rowid of a sample */ int regAccum = iMem++; /* Register to hold Stat3Accum object */ int regLoop = iMem++; /* Loop counter */ int regCount = iMem++; /* Number of rows in the table or index */ int regTemp1 = iMem++; /* Intermediate register */ int regTemp2 = iMem++; /* Intermediate register */ | > | 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 | int iDb; /* Index of database containing pTab */ int regTabname = iMem++; /* Register containing table name */ int regIdxname = iMem++; /* Register containing index name */ int regStat1 = iMem++; /* The stat column of sqlite_stat1 */ #ifdef SQLITE_ENABLE_STAT3 int regNumEq = regStat1; /* Number of instances. Same as regStat1 */ int regNumLt = iMem++; /* Number of keys less than regSample */ int regNumDLt = iMem++; /* Number of distinct keys less than regSample */ int regSample = iMem++; /* The next sample value */ int regRowid = regSample; /* Rowid of a sample */ int regAccum = iMem++; /* Register to hold Stat3Accum object */ int regLoop = iMem++; /* Loop counter */ int regCount = iMem++; /* Number of rows in the table or index */ int regTemp1 = iMem++; /* Intermediate register */ int regTemp2 = iMem++; /* Intermediate register */ |
︙ | ︙ | |||
516 517 518 519 520 521 522 523 524 525 526 527 528 529 | once = 0; sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead); } sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regCount); sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_STAT3_SAMPLES, regTemp1); sqlite3VdbeAddOp2(v, OP_Integer, 0, regNumEq); sqlite3VdbeAddOp2(v, OP_Integer, 0, regNumLt); sqlite3VdbeAddOp4(v, OP_Function, 1, regCount, regAccum, (char*)&stat3InitFuncdef, P4_FUNCDEF); sqlite3VdbeChangeP5(v, 2); #endif /* SQLITE_ENABLE_STAT3 */ /* The block of memory cells initialized here is used as follows. ** | > | 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 | once = 0; sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead); } sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regCount); sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_STAT3_SAMPLES, regTemp1); sqlite3VdbeAddOp2(v, OP_Integer, 0, regNumEq); sqlite3VdbeAddOp2(v, OP_Integer, 0, regNumLt); sqlite3VdbeAddOp2(v, OP_Integer, -1, regNumDLt); sqlite3VdbeAddOp4(v, OP_Function, 1, regCount, regAccum, (char*)&stat3InitFuncdef, P4_FUNCDEF); sqlite3VdbeChangeP5(v, 2); #endif /* SQLITE_ENABLE_STAT3 */ /* The block of memory cells initialized here is used as follows. ** |
︙ | ︙ | |||
580 581 582 583 584 585 586 | for(i=0; i<nCol; i++){ sqlite3VdbeJumpHere(v, aChngAddr[i]); /* Set jump dest for the OP_Ne */ if( i==0 ){ sqlite3VdbeJumpHere(v, addrIfNot); /* Jump dest for OP_IfNot */ #ifdef SQLITE_ENABLE_STAT3 sqlite3VdbeAddOp4(v, OP_Function, 1, regNumEq, regTemp2, (char*)&stat3PushFuncdef, P4_FUNCDEF); | | > | > > > | | 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 | for(i=0; i<nCol; i++){ sqlite3VdbeJumpHere(v, aChngAddr[i]); /* Set jump dest for the OP_Ne */ if( i==0 ){ sqlite3VdbeJumpHere(v, addrIfNot); /* Jump dest for OP_IfNot */ #ifdef SQLITE_ENABLE_STAT3 sqlite3VdbeAddOp4(v, OP_Function, 1, regNumEq, regTemp2, (char*)&stat3PushFuncdef, P4_FUNCDEF); sqlite3VdbeChangeP5(v, 5); sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, pIdx->nColumn, regRowid); sqlite3VdbeAddOp3(v, OP_Add, regNumEq, regNumLt, regNumLt); sqlite3VdbeAddOp2(v, OP_AddImm, regNumDLt, 1); sqlite3VdbeAddOp2(v, OP_Integer, 1, regNumEq); #endif } sqlite3VdbeAddOp2(v, OP_AddImm, iMem+i+1, 1); sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, iMem+nCol+i+1); } sqlite3DbFree(db, aChngAddr); /* Always jump here after updating the iMem+1...iMem+1+nCol counters */ sqlite3VdbeResolveLabel(v, endOfLoop); sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, topOfLoop); sqlite3VdbeAddOp1(v, OP_Close, iIdxCur); #ifdef SQLITE_ENABLE_STAT3 sqlite3VdbeAddOp4(v, OP_Function, 1, regNumEq, regTemp2, (char*)&stat3PushFuncdef, P4_FUNCDEF); sqlite3VdbeChangeP5(v, 5); sqlite3VdbeAddOp2(v, OP_Integer, -1, regLoop); shortJump = sqlite3VdbeAddOp2(v, OP_AddImm, regLoop, 1); sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regTemp1, (char*)&stat3GetFuncdef, P4_FUNCDEF); sqlite3VdbeChangeP5(v, 2); sqlite3VdbeAddOp1(v, OP_IsNull, regTemp1); sqlite3VdbeAddOp3(v, OP_NotExists, iTabCur, shortJump, regTemp1); sqlite3VdbeAddOp3(v, OP_Column, iTabCur, pIdx->aiColumn[0], regSample); sqlite3ColumnDefault(v, pTab, pIdx->aiColumn[0], regSample); sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumEq, (char*)&stat3GetFuncdef, P4_FUNCDEF); sqlite3VdbeChangeP5(v, 3); sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumLt, (char*)&stat3GetFuncdef, P4_FUNCDEF); sqlite3VdbeChangeP5(v, 4); sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumDLt, (char*)&stat3GetFuncdef, P4_FUNCDEF); sqlite3VdbeChangeP5(v, 5); sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 6, regRec, "bbbbbb", 0); sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regNewRowid); sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regRec, regNewRowid); sqlite3VdbeAddOp2(v, OP_Goto, 0, shortJump); sqlite3VdbeJumpHere(v, shortJump+2); #endif /* Store the results in sqlite_stat1. |
︙ | ︙ | |||
949 950 951 952 953 954 955 | sqlite3_finalize(pStmt); return SQLITE_NOMEM; } } sqlite3_finalize(pStmt); zSql = sqlite3MPrintf(db, | | < | 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 | sqlite3_finalize(pStmt); return SQLITE_NOMEM; } } sqlite3_finalize(pStmt); zSql = sqlite3MPrintf(db, "SELECT idx,nlt,neq,sample FROM %Q.sqlite_stat3", zDb); if( !zSql ){ return SQLITE_NOMEM; } rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0); sqlite3DbFree(db, zSql); if( rc ) return rc; |
︙ | ︙ |