1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-11-12 13:01:09 +03:00

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
This commit is contained in:
drh
2017-05-01 15:15:41 +00:00
parent 7555581fc1
commit e08e8d6b61
5 changed files with 69 additions and 13 deletions

View File

@@ -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; pItem<pThis; pItem++){
if( pItem->pSelect==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);