From: drh Date: Thu, 20 Oct 2016 22:02:43 +0000 (+0000) Subject: Experimental est_count pragma. X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=a74480a21744055b10ca8f2f44d92370e5fac791;p=thirdparty%2Fsqlite.git Experimental est_count pragma. FossilOrigin-Name: 340822afbe0c6b47fdd789c56acd9e1058a434cb --- diff --git a/manifest b/manifest index 335c3844f3..615523ae78 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Add\sthe\sability\sfor\sthe\sPRAGMA\sstatement\sto\saccept\smultiple\sarguments.\nCurrently\sall\sarguments\sother\sthan\sthe\sfirst\sare\signored. -D 2016-10-20T18:20:10.293 +C Experimental\sest_count\spragma. +D 2016-10-20T22:02:43.512 F Makefile.in 6fd48ffcf7c2deea7499062d1f3747f986c19678 F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434 F Makefile.msc 5151cc64c4c05f3455f4f692ad11410a810d937f @@ -329,8 +329,8 @@ F src/auth.c 930b376a9c56998557367e6f7f8aaeac82a2a792 F src/backup.c faf17e60b43233c214aae6a8179d24503a61e83b F src/bitvec.c 3ee4c8b2c94ed3a7377256e18199e6ff5cf33f63 F src/btmutex.c bc87dd3b062cc26edfe79918de2200ccb8d41e73 -F src/btree.c 4d035fb52b0ef9658d43dfe604413185dff7a5d1 -F src/btree.h d05b2fcc290991a8a3d9ea1816ddd55a4359dcde +F src/btree.c c58b8a52ad8a74d48835a0471fadec0851d113fb +F src/btree.h 012576c1d0000e96ccc785c6fe22b4be8211f0d9 F src/btreeInt.h c18b7d2a3494695133e4e60ee36061d37f45d9a5 F src/build.c e22e66fb34e1e50bb7869a510923bfe58db3071a F src/callback.c 2e76147783386374bf01b227f752c81ec872d730 @@ -379,8 +379,8 @@ F src/parse.y 07f68096c97093793ed04d69db8bb75e55c3e00e F src/pcache.c 5ff2a08f76a9c1b22f43eb063b7068fb085465ac F src/pcache.h 2cedcd8407eb23017d92790b112186886e179490 F src/pcache1.c e3967219b2a92b9edcb9324a4ba75009090d3953 -F src/pragma.c 3ad504c61bcddacc231662f83ac41aceea639ff2 -F src/pragma.h 64c78a648751b9f4f297276c4eb7507b14b4628c +F src/pragma.c baf707463944a0788df1503e9920c36edca1fe8f +F src/pragma.h 30d6559672c6755be452ea5fdf2e9bf32acc69a3 F src/prepare.c b1140c3d0cf59bc85ace00ce363153041b424b7a F src/printf.c a5f0ca08ddede803c241266abb46356ec748ded1 F src/random.c ba2679f80ec82c4190062d756f22d0c358180696 @@ -453,7 +453,7 @@ F src/update.c 8179e699dbd45b92934fd02d3d8e3732e8da8802 F src/utf.c 699001c79f28e48e9bcdf8a463da029ea660540c F src/util.c 3e2da6101888d073e79ecc6af5e0a2f70fa1e498 F src/vacuum.c 913970b9d86dd6c2b8063ef1af421880f1464ec3 -F src/vdbe.c c93ae647b3b302234f3593fd686577216aa22543 +F src/vdbe.c c07f9a771c82619be14cc5368b19623ed8f3a30d F src/vdbe.h c044be7050ac6bf596eecc6ab159f5dbc020a3b7 F src/vdbeInt.h 0a18713d0a2fec6807d076bd333d9bf3e57530cd F src/vdbeapi.c 8272f9342c39ab8d7eb4b9decc6caa7bc75b7d83 @@ -1464,7 +1464,7 @@ F tool/mkmsvcmin.tcl 2f12f7fa8858bbe61cf81820a2da96c79ed1ca8d F tool/mkopcodec.tcl d1b6362bd3aa80d5520d4d6f3765badf01f6c43c F tool/mkopcodeh.tcl a01d2c1d8a6205b03fc635adf3735b4c523befd3 F tool/mkopts.tcl 66ac10d240cc6e86abd37dc908d50382f84ff46e -F tool/mkpragmatab.tcl f0d5bb266d1d388cf86fce5ba01a891e95d72d41 +F tool/mkpragmatab.tcl bf6417cc44d42c1d024ac7036b59b9a8d4476470 F tool/mkspeedsql.tcl a1a334d288f7adfe6e996f2e712becf076745c97 F tool/mksqlite3c-noext.tcl fef88397668ae83166735c41af99d79f56afaabb F tool/mksqlite3c.tcl 06b2e6a0f21cc0a5d70fbbd136b3e0a96470645e @@ -1526,10 +1526,10 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 56474ebca3fdddb8f3c5156f06dc42dc0a65256c -R 7df9a33a3d365e65a71615f92b26cc61 -T *branch * multi-arg-pragma -T *sym-multi-arg-pragma * -T -sym-trunk * +P fd81d8a4300c36d02f07371b722124ccf619654c +R 43cd10c5c43a5cc4c174b3be405d48f3 +T *branch * est_count_pragma +T *sym-est_count_pragma * +T -sym-multi-arg-pragma * U drh -Z 4afce9abc23c76c5686e4f8a5162325b +Z 5a2d49857d0ed608295dc142049c4e6c diff --git a/manifest.uuid b/manifest.uuid index 4f7a6dca15..641e3013bc 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -fd81d8a4300c36d02f07371b722124ccf619654c \ No newline at end of file +340822afbe0c6b47fdd789c56acd9e1058a434cb \ No newline at end of file diff --git a/src/btree.c b/src/btree.c index 1f7225cd8b..696e1be98c 100644 --- a/src/btree.c +++ b/src/btree.c @@ -5037,6 +5037,69 @@ int sqlite3BtreeLast(BtCursor *pCur, int *pRes){ return rc; } +/* +** Move the cursor pCur to a location within its b-tree that is +** approximately the x/1e9*nRow entry in the table, assuming the +** table contains nRow entries. So, in other words, if x==0 move +** to the first entry and if x=1e9 move to the last entry and if +** x=5e8 move to the middle entry. The final landing spot is +** approximate. +** +** Write an estimate of the number of entries in the b-tree into +** the *pnRowEst variable. +** +** This routine works by first moving the cursor to the root of the +** b-tree, then following pointers down to a leaf, selecting a pointer +** according to x. +** +** The estimated number of entries is found by multiplying the number of +** entries on the leaf page by the number of pointers at each layer of +** non-leaf pages. +** +** Return SQLITE_OK on success or an error code if problems are encountered. +*/ +int sqlite3BtreeMovetoProportional( + BtCursor *pCur, /* Cursor to reposition */ + u32 x, /* approximate location to position the cursor */ + sqlite3_uint64 *pnRowEst /* Write estimated entry count here */ +){ + sqlite3_uint64 n = 1; + int rc; + Pgno chldPg; + u32 mx = 1000000000; + u32 perChild; + u16 rx; + MemPage *pPage; + rc = moveToRoot(pCur); + if( rc ) return rc; + pPage = pCur->apPage[0]; + while( !pPage->leaf ){ + perChild = (mx+pPage->nCell)/(pPage->nCell+1); + rx = x/perChild; + x %= perChild; + mx = perChild; + if( rx>=pPage->nCell ){ + chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]); + }else{ + chldPg = get4byte(findCell(pPage,rx)); + } + n *= pPage->nCell+1; + pCur->aiIdx[pCur->iPage] = rx; + rc = moveToChild(pCur, chldPg); + if( rc ) return rc; + pPage = pCur->apPage[pCur->iPage]; + } + *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. ** diff --git a/src/btree.h b/src/btree.h index 0df98a3a64..9d23da4008 100644 --- a/src/btree.h +++ b/src/btree.h @@ -238,6 +238,7 @@ void sqlite3BtreeCursorHint(BtCursor*, int, ...); #endif int sqlite3BtreeCloseCursor(BtCursor*); +int sqlite3BtreeMovetoProportional(BtCursor*,u32,u64*); int sqlite3BtreeMovetoUnpacked( BtCursor*, UnpackedRecord *pUnKey, diff --git a/src/pragma.c b/src/pragma.c index fef044d7d6..d88a9a0bf8 100644 --- a/src/pragma.c +++ b/src/pragma.c @@ -1381,6 +1381,49 @@ void sqlite3Pragma( } break; + /* + ** PRAGMA est_row_cnt(,); + ** + ** Seek in through the first of rows and + ** estimate the total number of rows based on the path back up to the + ** root. + */ + case PragTyp_EST_COUNT: { + Index *pIdx; + Table *pTab; + Pgno iRoot = 0; + const char *zName = 0; + int regResult; + double r; + static const char *azCol[] = { "est" }; + if( (pIdx = sqlite3FindIndex(db, zRight, zDb))!=0 ){ + iRoot = pIdx->tnum; + zName = pIdx->zName; + }else if( (pTab = sqlite3FindTable(db, zRight, zDb))!=0 ){ + iRoot = pTab->tnum; + zName = pTab->zName; + }else{ + break; + } + sqlite3TableLock(pParse, iDb, iRoot, 0, zName); + regResult = ++pParse->nMem; + setAllColumnNames(v, 1, azCol); + 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; + sqlite3CodeVerifySchema(pParse, iDb); + pParse->nTab++; + sqlite3VdbeAddOp4Int(v, OP_OpenRead, 0, iRoot, iDb, 1); + 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 diff --git a/src/pragma.h b/src/pragma.h index 81779e9d4a..182addc70f 100644 --- a/src/pragma.h +++ b/src/pragma.h @@ -16,36 +16,37 @@ #define PragTyp_DATABASE_LIST 10 #define PragTyp_DEFAULT_CACHE_SIZE 11 #define PragTyp_ENCODING 12 -#define PragTyp_FOREIGN_KEY_CHECK 13 -#define PragTyp_FOREIGN_KEY_LIST 14 -#define PragTyp_INCREMENTAL_VACUUM 15 -#define PragTyp_INDEX_INFO 16 -#define PragTyp_INDEX_LIST 17 -#define PragTyp_INTEGRITY_CHECK 18 -#define PragTyp_JOURNAL_MODE 19 -#define PragTyp_JOURNAL_SIZE_LIMIT 20 -#define PragTyp_LOCK_PROXY_FILE 21 -#define PragTyp_LOCKING_MODE 22 -#define PragTyp_PAGE_COUNT 23 -#define PragTyp_MMAP_SIZE 24 -#define PragTyp_PAGE_SIZE 25 -#define PragTyp_SECURE_DELETE 26 -#define PragTyp_SHRINK_MEMORY 27 -#define PragTyp_SOFT_HEAP_LIMIT 28 -#define PragTyp_STATS 29 -#define PragTyp_SYNCHRONOUS 30 -#define PragTyp_TABLE_INFO 31 -#define PragTyp_TEMP_STORE 32 -#define PragTyp_TEMP_STORE_DIRECTORY 33 -#define PragTyp_THREADS 34 -#define PragTyp_WAL_AUTOCHECKPOINT 35 -#define PragTyp_WAL_CHECKPOINT 36 -#define PragTyp_ACTIVATE_EXTENSIONS 37 -#define PragTyp_HEXKEY 38 -#define PragTyp_KEY 39 -#define PragTyp_REKEY 40 -#define PragTyp_LOCK_STATUS 41 -#define PragTyp_PARSER_TRACE 42 +#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 { @@ -172,6 +173,10 @@ static const struct sPragmaNames { /* ePragFlag: */ 0, /* iArg: */ 0 }, #endif + { /* zName: */ "est_count", + /* ePragTyp: */ PragTyp_EST_COUNT, + /* ePragFlag: */ PragFlag_NeedSchema, + /* iArg: */ 0 }, #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER) { /* zName: */ "foreign_key_check", /* ePragTyp: */ PragTyp_FOREIGN_KEY_CHECK, @@ -461,4 +466,4 @@ static const struct sPragmaNames { /* iArg: */ SQLITE_WriteSchema|SQLITE_RecoveryMode }, #endif }; -/* Number of pragmas: 60 on by default, 73 total. */ +/* Number of pragmas: 61 on by default, 74 total. */ diff --git a/src/vdbe.c b/src/vdbe.c index 09019f3728..40db24d0e4 100644 --- a/src/vdbe.c +++ b/src/vdbe.c @@ -4897,6 +4897,31 @@ case OP_Rewind: { /* jump */ break; } +/* +** Opcode: EstRowCnt P1 P2 P3 * * * +** +** Estimate the number of entries in btree for cursor P1 do a proportional +** seek to of P3. Store the result as a floating point value in P2. +*/ +case OP_EstRowCnt: { /* out2 */ + VdbeCursor *pC; + BtCursor *pCrsr; + int rc; + sqlite3_uint64 n = 0; + + assert( pOp->p1>=0 && pOp->p1nCursor ); + pC = p->apCsr[pOp->p1]; + assert( pC->eCurType==CURTYPE_BTREE ); + 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 diff --git a/tool/mkpragmatab.tcl b/tool/mkpragmatab.tcl index 145a365c54..cc2c3e2630 100644 --- a/tool/mkpragmatab.tcl +++ b/tool/mkpragmatab.tcl @@ -251,6 +251,9 @@ set pragma_def { FLAG: NeedSchema IF: !defined(SQLITE_OMIT_INTEGRITY_CHECK) + NAME: est_count + FLAG: NeedSchema + NAME: encoding IF: !defined(SQLITE_OMIT_UTF16)