From: shess Date: Tue, 29 Jul 2008 01:13:02 +0000 (+0000) Subject: Re-used prepared statement from fts3 cursor. Previously, each call to X-Git-Tag: version-3.6.10~688 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=b5f94870c24c3a4c9e83adadcb38d42691b4d9d8;p=thirdparty%2Fsqlite.git Re-used prepared statement from fts3 cursor. Previously, each call to fulltextFilter() finalized any existing prepared statement and prepared a new one. In the case where idxNum has not changed, simply reseting the statement suffices. This provides an order of magnitude speedup in incoming joins against docid. (CVS 5489) FossilOrigin-Name: a08a5f2b1256b8a93beca5a359ccfc28d403efa3 --- diff --git a/ext/fts3/fts3.c b/ext/fts3/fts3.c index f9f8c4aa31..cc6dcd82d2 100644 --- a/ext/fts3/fts3.c +++ b/ext/fts3/fts3.c @@ -4102,21 +4102,43 @@ static int fulltextFilter( fulltext_cursor *c = (fulltext_cursor *) pCursor; fulltext_vtab *v = cursor_vtab(c); int rc; - StringBuffer sb; FTSTRACE(("FTS3 Filter %p\n",pCursor)); - initStringBuffer(&sb); - append(&sb, "SELECT docid, "); - appendList(&sb, v->nColumn, v->azContentColumn); - append(&sb, " FROM %_content"); - if( idxNum!=QUERY_GENERIC ) append(&sb, " WHERE docid = ?"); - sqlite3_finalize(c->pStmt); - rc = sql_prepare(v->db, v->zDb, v->zName, &c->pStmt, stringBufferData(&sb)); - stringBufferDestroy(&sb); - if( rc!=SQLITE_OK ) return rc; + /* If the cursor has a statement that was not prepared according to + ** idxNum, clear it. I believe all calls to fulltextFilter with a + ** given cursor will have the same idxNum , but in this case it's + ** easy to be safe. + */ + if( c->pStmt && c->iCursorType!=idxNum ){ + sqlite3_finalize(c->pStmt); + c->pStmt = NULL; + } + + /* Get a fresh statement appropriate to idxNum. */ + /* TODO(shess): Add a prepared-statement cache in the vt structure. + ** The cache must handle multiple open cursors. Easier to cache the + ** statement variants at the vt to reduce malloc/realloc/free here. + ** Or we could have a StringBuffer variant which allowed stack + ** construction for small values. + */ + if( !c->pStmt ){ + StringBuffer sb; + initStringBuffer(&sb); + append(&sb, "SELECT docid, "); + appendList(&sb, v->nColumn, v->azContentColumn); + append(&sb, " FROM %_content"); + if( idxNum!=QUERY_GENERIC ) append(&sb, " WHERE docid = ?"); + rc = sql_prepare(v->db, v->zDb, v->zName, &c->pStmt, + stringBufferData(&sb)); + stringBufferDestroy(&sb); + if( rc!=SQLITE_OK ) return rc; + c->iCursorType = idxNum; + }else{ + sqlite3_reset(c->pStmt); + assert( c->iCursorType==idxNum ); + } - c->iCursorType = idxNum; switch( idxNum ){ case QUERY_GENERIC: break; diff --git a/manifest b/manifest index 22da79fbfc..db8d84e71d 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Implement\sthe\s"lookaside"\smemory\sallocation\scache.\s\sUse\sof\sthis\scache\smakes\nthe\sspeed1.test\sscript\srun\sabout\s15%\sfaster.\s\sAdded\snew\sinterfaces\sto\ncontrol\sthe\scache.\s(CVS\s5488) -D 2008-07-28T19:34:53 +C Re-used\sprepared\sstatement\sfrom\sfts3\scursor.\s\sPreviously,\seach\scall\sto\nfulltextFilter()\sfinalized\sany\sexisting\sprepared\sstatement\sand\nprepared\sa\snew\sone.\s\sIn\sthe\scase\swhere\sidxNum\shas\snot\schanged,\ssimply\nreseting\sthe\sstatement\ssuffices.\s\sThis\sprovides\san\sorder\sof\smagnitude\nspeedup\sin\sincoming\sjoins\sagainst\sdocid.\s(CVS\s5489) +D 2008-07-29T01:13:02 F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0 F Makefile.in bbb62eecc851379aef5a48a1bf8787eb13e6ec06 F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654 @@ -51,7 +51,7 @@ F ext/fts2/fts2_tokenizer1.c 8545ce12b41922004da46e91a7b023b92b76f94e F ext/fts2/mkfts2amal.tcl 974d5d438cb3f7c4a652639262f82418c1e4cff0 F ext/fts3/README.tokenizers 226644a0eab97724e8de83061912e8bb248461b6 F ext/fts3/README.txt 8c18f41574404623b76917b9da66fcb0ab38328d -F ext/fts3/fts3.c 14ac2e37889fda1555d903648772b6be3061d2bb +F ext/fts3/fts3.c fa37048c369491ba6fb74e1732f24a8a8daaef65 F ext/fts3/fts3.h 3a10a0af180d502cecc50df77b1b22df142817fe F ext/fts3/fts3_hash.c 83e7bb4042106b32811681dd2859b4577a7a6b35 F ext/fts3/fts3_hash.h 004b759e1602ff16dfa02fea3ca1c77336ad6798 @@ -612,7 +612,7 @@ F tool/speedtest16.c c8a9c793df96db7e4933f0852abb7a03d48f2e81 F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff F tool/speedtest8.c 1dbced29de5f59ba2ebf877edcadf171540374d1 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e -P 51be2e4463ca32f290feb610f59553b55bc67a5c -R 1624ec122f5ffb86a37761ed2b47d75a -U drh -Z 5a311db47e066a208f5f741cb81e1882 +P e48f9697e9fea339e150ddc32940760027dd07d9 +R 680fb3be123f951c8c77ef4a340cd813 +U shess +Z b0abfd8b0f9f50c33d751a3bf926e781 diff --git a/manifest.uuid b/manifest.uuid index 5aad7c290a..ffd7b409b3 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -e48f9697e9fea339e150ddc32940760027dd07d9 \ No newline at end of file +a08a5f2b1256b8a93beca5a359ccfc28d403efa3 \ No newline at end of file