From e08e8d6b61187ecfd19f98f863a2a35f6b80152b Mon Sep 17 00:00:00 2001 From: drh Date: Mon, 1 May 2017 15:15:41 +0000 Subject: [PATCH] Initial implementation of an optimization that attempts to reuse the same materialization of a view on a self-join of the view. FossilOrigin-Name: 478c34b9a8b5127d13024e10307aa832f160b89720c46424dd17555bd36f590d --- manifest | 19 +++++++++++-------- manifest.uuid | 2 +- src/select.c | 31 ++++++++++++++++++++++++++++--- src/vdbe.c | 28 ++++++++++++++++++++++++++++ src/vdbeaux.c | 2 +- 5 files changed, 69 insertions(+), 13 deletions(-) diff --git a/manifest b/manifest index 22a472ed22..14baff5c8e 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Add\sa\ssingle\stestcase()\smacro\sto\sthe\ssubquery\sprocessing\slogic. -D 2017-04-29T19:29:49.700 +C Initial\simplementation\sof\san\soptimization\sthat\sattempts\sto\sreuse\sthe\ssame\nmaterialization\sof\sa\sview\son\sa\sself-join\sof\sthe\sview. +D 2017-05-01T15:15:41.259 F Makefile.in 1cc758ce3374a32425e4d130c2fe7b026b20de5b8843243de75f087c0a2661fb F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434 F Makefile.msc 6a8c838220f7c00820e1fc0ac1bccaaa8e5676067e1dbfa1bafa7a4ffecf8ae6 @@ -402,7 +402,7 @@ F src/printf.c 8757834f1b54dae512fb25eb1acc8e94a0d15dd2290b58f2563f65973265adb2 F src/random.c 80f5d666f23feb3e6665a6ce04c7197212a88384 F src/resolve.c 3e518b962d932a997fae373366880fc028c75706 F src/rowset.c 7b7e7e479212e65b723bf40128c7b36dc5afdfac -F src/select.c 478e95d424bb86d34c7c95d20872cbd78df97af5f83c3fd7de55d5b2413f927d +F src/select.c 1fba56aa7d6530a37e173e5144df92ad6140c472f9807c168a2ebff096b590c7 F src/shell.c 21b79c0e1b93f8e35fd7b4087d6ba438326c3d7e285d0dd51dfd741475f858a1 F src/sqlite.h.in 40233103e3e4e10f8a63523498d0259d232e42aba478e2d3fb914799185aced6 F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8 @@ -469,11 +469,11 @@ F src/update.c c443935c652af9365e033f756550b5032d02e1b06eb2cb890ed7511ae0c051dc F src/utf.c 699001c79f28e48e9bcdf8a463da029ea660540c F src/util.c ca8440ede81e155d15cff7c101654f60b55a9ae6 F src/vacuum.c 1fe4555cd8c9b263afb85b5b4ee3a4a4181ad569 -F src/vdbe.c 349eb6789cf8b03ef1aa42271aa9a17a6a7794c07448d27c5405904ba1b21715 +F src/vdbe.c 7c37c36f3d528b10e5a5df76267ca42ed604fb3f15ff1d656afe1f33ff5d1f76 F src/vdbe.h f7d1456e28875c2dcb964056589b5b7149ab7edf39edeca801596a39bb3d3848 F src/vdbeInt.h c070bc5c8b913bda0ceaa995cd4d939ded5e4fc96cf7c3c1c602d41b871f8ade F src/vdbeapi.c 5b08d82592bcff4470601fe78aaabebd50837860 -F src/vdbeaux.c 6b3f6ce909e206d4c918988b13b7fa687e92b4471d137e0f2a37edac80ec60be +F src/vdbeaux.c bb99698cfee2d2fd9fa2870f8a24153475793b9c85cb626b412d6bad8c3a73a8 F src/vdbeblob.c 359891617358deefc85bef7bcf787fa6b77facb9 F src/vdbemem.c 2c70f8f5de6c71fb99a22c5b83be9fab5c47cdd8e279fa44a8c00cfed06d7e89 F src/vdbesort.c e72fe02a2121386ba767ede8942e9450878b8fc873abf3d1b6824485f092570c @@ -1577,7 +1577,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 e54c9f8db5b2fa8ea82c6eab7482255431af16901f7992c9667b56a0e50a9f4f -R 6f21ea13ed166571d20351d2145d6640 +P 4e1df76e3d85922648e0e1cce73a266c3b1ed4511ace259ec0a01d7693af9e6f +R 9a92f7b5439fceb732be32bb3fe5b30d +T *branch * materialized-view-reuse +T *sym-materialized-view-reuse * +T -sym-trunk * U drh -Z 4a7e86f7be3de966a53a14c1a17e31d5 +Z 0a08980cdb3c200bc98cb9ecc97a5f40 diff --git a/manifest.uuid b/manifest.uuid index 45bf2ccb33..5ca2086bf7 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -4e1df76e3d85922648e0e1cce73a266c3b1ed4511ace259ec0a01d7693af9e6f \ No newline at end of file +478c34b9a8b5127d13024e10307aa832f160b89720c46424dd17555bd36f590d \ No newline at end of file diff --git a/src/select.c b/src/select.c index 77824c4aaa..69c75fb96a 100644 --- a/src/select.c +++ b/src/select.c @@ -4879,6 +4879,24 @@ static void explainSimpleCount( # define explainSimpleCount(a,b,c) #endif +/* +** Check to see if the pThis entry of pTabList is a self-join of a prior view. +** If it is, then return the SrcList_item for the prior view. If it is not, +** then return 0. +*/ +static struct SrcList_item *isSelfJoinView( + SrcList *pTabList, /* Search for self-joins in this FROM clause */ + struct SrcList_item *pThis /* Search for prior reference to this subquery */ +){ + struct SrcList_item *pItem; + for(pItem = pTabList->a; pItempSelect==0 ) continue; + if( pItem->fg.viaCoroutine ) continue; + if( sqlite3StrICmp(pItem->zName, pThis->zName)==0 ) return pItem; + } + return 0; +} + /* ** Generate code for the SELECT statement given in the p argument. ** @@ -5113,6 +5131,8 @@ int sqlite3Select( int topAddr; int onceAddr = 0; int retAddr; + struct SrcList_item *pPrior; + assert( pItem->addrFillSub==0 ); pItem->regReturn = ++pParse->nMem; topAddr = sqlite3VdbeAddOp2(v, OP_Integer, 0, pItem->regReturn); @@ -5126,9 +5146,14 @@ int sqlite3Select( }else{ VdbeNoopComment((v, "materialize \"%s\"", pItem->pTab->zName)); } - sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor); - explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId); - sqlite3Select(pParse, pSub, &dest); + pPrior = isSelfJoinView(pTabList, pItem); + if( pPrior ){ + sqlite3VdbeAddOp2(v, OP_OpenDup, pItem->iCursor, pPrior->iCursor); + }else{ + sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor); + explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId); + sqlite3Select(pParse, pSub, &dest); + } pItem->pTab->nRowLogEst = pSub->nSelectRow; if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr); retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn); diff --git a/src/vdbe.c b/src/vdbe.c index 15f015a598..ebf80c8beb 100644 --- a/src/vdbe.c +++ b/src/vdbe.c @@ -3540,6 +3540,34 @@ open_cursor_set_hints: break; } +/* Opcode: OpenDup P1 P2 * * * +** +** Open a new cursor P1 that points to the same ephemeral table as +** cursor P2. The P2 cursor must have been opened by a prior OP_OpenEphemeral +** opcode. Only ephemeral cursors may be duplicated. +** +** Duplicate ephemeral cursors are used for self-joins of materialized views. +*/ +case OP_OpenDup: { + VdbeCursor *pOrig; /* The original cursor to be duplicated */ + VdbeCursor *pCx; /* The new cursor */ + + pOrig = p->apCsr[pOp->p2]; + assert( pOrig->pBtx!=0 ); /* Only ephemeral cursors can be duplicated */ + + pCx = allocateCursor(p, pOp->p1, pOrig->nField, -1, CURTYPE_BTREE); + if( pCx==0 ) goto no_mem; + pCx->nullRow = 1; + pCx->isEphemeral = 1; + pCx->pKeyInfo = pOrig->pKeyInfo; + pCx->isTable = pOrig->isTable; + rc = sqlite3BtreeCursor(pOrig->pBtx, MASTER_ROOT, BTREE_WRCSR, + pCx->pKeyInfo, pCx->uc.pCursor); + if( rc ) goto abort_due_to_error; + break; +} + + /* Opcode: OpenEphemeral P1 P2 * P4 P5 ** Synopsis: nColumn=P2 ** diff --git a/src/vdbeaux.c b/src/vdbeaux.c index 00a5ec91a9..e903f4bbcd 100644 --- a/src/vdbeaux.c +++ b/src/vdbeaux.c @@ -2039,7 +2039,7 @@ void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){ sqlite3BtreeClose(pCx->pBtx); /* The pCx->pCursor will be close automatically, if it exists, by ** the call above. */ - }else{ + }else if( !pCx->isEphemeral ){ assert( pCx->uc.pCursor!=0 ); sqlite3BtreeCloseCursor(pCx->uc.pCursor); }