From: dan Date: Wed, 24 Apr 2019 20:48:55 +0000 (+0000) Subject: Improve the performance of the .recover command. X-Git-Tag: version-3.29.0~176^2~10 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=efa363b84c43d63c706e7997085a2e5977aac64d;p=thirdparty%2Fsqlite.git Improve the performance of the .recover command. FossilOrigin-Name: a50768314d10d743a0cc013b434b516f0763e0a6c5b79655d8fefde7de53e869 --- diff --git a/ext/misc/dbdata.c b/ext/misc/dbdata.c index 7662417ae5..13bb51f08b 100644 --- a/ext/misc/dbdata.c +++ b/ext/misc/dbdata.c @@ -106,6 +106,7 @@ struct DbdataCursor { struct DbdataTable { sqlite3_vtab base; /* Base class. Must be first */ sqlite3 *db; /* The database connection */ + sqlite3_stmt *pStmt; /* For fetching database pages */ int bPtr; /* True for sqlite3_dbptr table */ }; @@ -167,7 +168,11 @@ static int dbdataConnect( ** Disconnect from or destroy a dbdata virtual table. */ static int dbdataDisconnect(sqlite3_vtab *pVtab){ - sqlite3_free(pVtab); + DbdataTable *pTab = (DbdataTable*)pVtab; + if( pTab ){ + sqlite3_finalize(pTab->pStmt); + sqlite3_free(pVtab); + } return SQLITE_OK; } @@ -185,15 +190,15 @@ static int dbdataDisconnect(sqlite3_vtab *pVtab){ ** If both parameters are present, schema is in position 0 and pgno in ** position 1. */ -static int dbdataBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ +static int dbdataBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdx){ DbdataTable *pTab = (DbdataTable*)tab; int i; int iSchema = -1; int iPgno = -1; int colSchema = (pTab->bPtr ? DBPTR_COLUMN_SCHEMA : DBDATA_COLUMN_SCHEMA); - for(i=0; inConstraint; i++){ - struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[i]; + for(i=0; inConstraint; i++){ + struct sqlite3_index_constraint *p = &pIdx->aConstraint[i]; if( p->op==SQLITE_INDEX_CONSTRAINT_EQ ){ if( p->iColumn==colSchema ){ if( p->usable==0 ) return SQLITE_CONSTRAINT; @@ -206,14 +211,29 @@ static int dbdataBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ } if( iSchema>=0 ){ - pIdxInfo->aConstraintUsage[iSchema].argvIndex = 1; - pIdxInfo->aConstraintUsage[iSchema].omit = 1; + pIdx->aConstraintUsage[iSchema].argvIndex = 1; + pIdx->aConstraintUsage[iSchema].omit = 1; } if( iPgno>=0 ){ - pIdxInfo->aConstraintUsage[iPgno].argvIndex = 1 + (iSchema>=0); - pIdxInfo->aConstraintUsage[iPgno].omit = 1; + pIdx->aConstraintUsage[iPgno].argvIndex = 1 + (iSchema>=0); + pIdx->aConstraintUsage[iPgno].omit = 1; + pIdx->estimatedCost = 100; + pIdx->estimatedRows = 50; + + if( pTab->bPtr==0 && pIdx->nOrderBy && pIdx->aOrderBy[0].desc==0 ){ + int iCol = pIdx->aOrderBy[0].iColumn; + if( pIdx->nOrderBy==1 ){ + pIdx->orderByConsumed = (iCol==0 || iCol==1); + }else if( pIdx->nOrderBy==2 && pIdx->aOrderBy[1].desc==0 && iCol==0 ){ + pIdx->orderByConsumed = (pIdx->aOrderBy[1].iColumn==1); + } + } + + }else{ + pIdx->estimatedCost = 100000000; + pIdx->estimatedRows = 1000000000; } - pIdxInfo->idxNum = (iSchema>=0 ? 0x01 : 0x00) | (iPgno>=0 ? 0x02 : 0x00); + pIdx->idxNum = (iSchema>=0 ? 0x01 : 0x00) | (iPgno>=0 ? 0x02 : 0x00); return SQLITE_OK; } @@ -236,7 +256,12 @@ static int dbdataOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ } static void dbdataResetCursor(DbdataCursor *pCsr){ - sqlite3_finalize(pCsr->pStmt); + DbdataTable *pTab = (DbdataTable*)(pCsr->base.pVtab); + if( pTab->pStmt==0 ){ + pTab->pStmt = pCsr->pStmt; + }else{ + sqlite3_finalize(pCsr->pStmt); + } pCsr->pStmt = 0; pCsr->iPgno = 1; pCsr->iCell = 0; @@ -558,7 +583,7 @@ static int dbdataFilter( ){ DbdataCursor *pCsr = (DbdataCursor*)pCursor; DbdataTable *pTab = (DbdataTable*)pCursor->pVtab; - int rc; + int rc = SQLITE_OK; const char *zSchema = "main"; dbdataResetCursor(pCsr); @@ -571,10 +596,15 @@ static int dbdataFilter( pCsr->bOnePage = 1; } - rc = sqlite3_prepare_v2(pTab->db, - "SELECT data FROM sqlite_dbpage(?) WHERE pgno=?", -1, - &pCsr->pStmt, 0 - ); + if( pTab->pStmt ){ + pCsr->pStmt = pTab->pStmt; + pTab->pStmt = 0; + }else{ + rc = sqlite3_prepare_v2(pTab->db, + "SELECT data FROM sqlite_dbpage(?) WHERE pgno=?", -1, + &pCsr->pStmt, 0 + ); + } if( rc==SQLITE_OK ){ rc = sqlite3_bind_text(pCsr->pStmt, 1, zSchema, -1, SQLITE_TRANSIENT); }else{ diff --git a/manifest b/manifest index e3ab13575f..039fa63685 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Have\s".recover"\shandle\s"\\r"\sand\s"\\n"\sin\sthe\ssame\sway\sas\s".dump". -D 2019-04-23T20:48:32.366 +C Improve\sthe\sperformance\sof\sthe\s.recover\scommand. +D 2019-04-24T20:48:55.802 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -284,7 +284,7 @@ F ext/misc/closure.c dbfd8543b2a017ae6b1a5843986b22ddf99ff126ec9634a2f4047cd14c8 F ext/misc/completion.c cec672d40604075bb341a7f11ac48393efdcd90a979269b8fe7977ea62d0547f F ext/misc/compress.c dd4f8a6d0baccff3c694757db5b430f3bbd821d8686d1fc24df55cf9f035b189 F ext/misc/csv.c 7f047aeb68f5802e7ce6639292095d622a488bb43526ed04810e0649faa71ceb -F ext/misc/dbdata.c 6a0ccc33e8c5ef3b6164eec7839cb4dff44e27bee0717f1660dbb8ee77ee4cf8 +F ext/misc/dbdata.c 0c80f0757c3a1b5c57027328ab0ccfe30fcef1a4875b41dd9ddc0f53c7087e97 F ext/misc/dbdump.c baf6e37447c9d6968417b1cd34cbedb0b0ab3f91b5329501d8a8d5be3287c336 F ext/misc/eval.c 4b4757592d00fd32e44c7a067e6a0e4839c81a4d57abc4131ee7806d1be3104e F ext/misc/explain.c d5c12962d79913ef774b297006872af1fccda388f61a11d37758f9179a09551f @@ -520,7 +520,7 @@ F src/random.c 80f5d666f23feb3e6665a6ce04c7197212a88384 F src/resolve.c 567888ee3faec14dae06519b4306201771058364a37560186a3e0e755ebc4cb8 F src/rowset.c d977b011993aaea002cab3e0bb2ce50cf346000dff94e944d547b989f4b1fe93 F src/select.c 9263f5c30dd44c7ac2eb29f40a7ec64322a96885b71c00de6bc30b756c2e1c49 -F src/shell.c.in 6e56c60640410885a8c3c264971619407ae5c7f46165e2bfe5fb1ca1a6a58ad0 +F src/shell.c.in 569ee1f79236f19b44fb4fcb8a0a2f0b2dce9a3d856d41a196aaaaa5f0d37704 F src/sqlite.h.in 38390767acc1914d58930e03149595ee4710afa4e3c43ab6c3a8aea3f1a6b8cd F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8 F src/sqlite3ext.h 9ecc93b8493bd20c0c07d52e2ac0ed8bab9b549c7f7955b59869597b650dd8b5 @@ -1821,7 +1821,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 8dcc1d89d955bf58c80a8c30a37960f0cf95719953951a92626cc332cc75ec60 -R b9c18dd1095e3f7b028bdaea88f9fcb0 +P f95f0f02ab6c6cf45f25b613c7ab57f68249689d0a9eddf4c9518ddf0edad365 +R 0e7ee563b8599ed64c48e30038f5f99f U dan -Z edabfc3afbc2656b0ce1a8fb6b1e492b +Z f093e1d7a99537670d8620da5d0b299b diff --git a/manifest.uuid b/manifest.uuid index fdcc68f9a9..3f61303065 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -f95f0f02ab6c6cf45f25b613c7ab57f68249689d0a9eddf4c9518ddf0edad365 \ No newline at end of file +a50768314d10d743a0cc013b434b516f0763e0a6c5b79655d8fefde7de53e869 \ No newline at end of file diff --git a/src/shell.c.in b/src/shell.c.in index 7989c9dfde..05ce02d605 100644 --- a/src/shell.c.in +++ b/src/shell.c.in @@ -6372,6 +6372,8 @@ static RecoverTable *recoverNewTable( static int recoverDatabaseCmd(ShellState *pState){ int rc = SQLITE_OK; sqlite3_stmt *pLoop = 0; /* Loop through all root pages */ + sqlite3_stmt *pPages = 0; /* Loop through all pages in a group */ + sqlite3_stmt *pCells = 0; /* Loop through all cells in a page */ shellExec(pState->db, &rc, /* Attach an in-memory database named 'recovery'. Create an indexed @@ -6460,6 +6462,15 @@ static int recoverDatabaseCmd(ShellState *pState){ shellFinalize(&rc, pStmt); } + shellPrepare(pState->db, &rc, + "SELECT pgno FROM recovery.map WHERE root=?", &pPages + ); + shellPrepare(pState->db, &rc, + "SELECT max(field), group_concat(shell_escape_crnl(quote(value)), ', ')" + "FROM sqlite_dbdata WHERE pgno = ? AND field != ?" + "GROUP BY cell", &pCells + ); + /* Loop through each root page. */ shellPrepare(pState->db, &rc, "SELECT root, intkey, max(maxlen) FROM recovery.map" @@ -6475,30 +6486,29 @@ static int recoverDatabaseCmd(ShellState *pState){ pTab = recoverNewTable(pState, &rc, iRoot, bIntkey, nCol); if( pTab ){ - sqlite3_stmt *pData = 0; if( 0==sqlite3_stricmp(pTab->zName, "sqlite_sequence") ){ raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n"); } - shellPreparePrintf(pState->db, &rc, &pData, - "SELECT max(field), group_concat(shell_escape_crnl(quote(value)),', ')" - "FROM sqlite_dbdata WHERE pgno IN (" - " SELECT pgno FROM recovery.map WHERE root=%d" - ")" - " AND field!=%d " - "GROUP BY pgno, cell;", iRoot, pTab->iPk - ); - while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pData) ){ - int iMax = sqlite3_column_int(pData, 0); - const char *zVal = (const char*)sqlite3_column_text(pData, 1); - raw_printf(pState->out, "INSERT INTO %s(%s) VALUES( %s );\n", - pTab->zQuoted, pTab->azlCol[iMax>0?iMax:0], zVal - ); + sqlite3_bind_int(pPages, 1, iRoot); + sqlite3_bind_int(pCells, 2, pTab->iPk); + while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){ + sqlite3_bind_int(pCells, 1, sqlite3_column_int(pPages, 0)); + while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){ + int iMax = sqlite3_column_int(pCells, 0); + const char *zVal = (const char*)sqlite3_column_text(pCells, 1); + raw_printf(pState->out, "INSERT INTO %s(%s) VALUES( %s );\n", + pTab->zQuoted, pTab->azlCol[iMax>0?iMax:0], zVal + ); + } + shellReset(&rc, pCells); } - shellFinalize(&rc, pData); + shellReset(&rc, pPages); } recoverFreeTable(pTab); } shellFinalize(&rc, pLoop); + shellFinalize(&rc, pPages); + shellFinalize(&rc, pCells); /* The rest of the schema */ if( rc==SQLITE_OK ){