mirror of
https://github.com/sqlite/sqlite.git
synced 2025-11-16 23:02:26 +03:00
Merge the sqlite3WhereBegin() simplification from trunk.
FossilOrigin-Name: 41ba2dfdd3a18671fc78d60935a16fa50f36af3d6481eff2ca9fba88e7093997
This commit is contained in:
12
manifest
12
manifest
@@ -1,5 +1,5 @@
|
|||||||
C Merge\srecent\strunk\senhancements\sinto\sthe\sbloom-filter\sbranch.
|
C Merge\sthe\ssqlite3WhereBegin()\ssimplification\sfrom\strunk.
|
||||||
D 2021-12-03T16:02:05.551
|
D 2021-12-03T19:10:17.141
|
||||||
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
|
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
|
||||||
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
|
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
|
||||||
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
|
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
|
||||||
@@ -637,7 +637,7 @@ F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9
|
|||||||
F src/wal.c ed0398a7adf02c31e34aada42cc86c58f413a7afe5f741a5d373ad087abde028
|
F src/wal.c ed0398a7adf02c31e34aada42cc86c58f413a7afe5f741a5d373ad087abde028
|
||||||
F src/wal.h c3aa7825bfa2fe0d85bef2db94655f99870a285778baa36307c0a16da32b226a
|
F src/wal.h c3aa7825bfa2fe0d85bef2db94655f99870a285778baa36307c0a16da32b226a
|
||||||
F src/walker.c f890a3298418d7cba3b69b8803594fdc484ea241206a8dfa99db6dd36f8cbb3b
|
F src/walker.c f890a3298418d7cba3b69b8803594fdc484ea241206a8dfa99db6dd36f8cbb3b
|
||||||
F src/where.c c04c4108028513cf2f264505f2bc3e861c2ed96985d7a5feead4cf76fe057dc1
|
F src/where.c a0787ad0a3447685e553725dab50e52c9763b5f10e2506255713dbd736c89998
|
||||||
F src/whereInt.h 83ae6f7d0fce8b5164d97698790e0005a909b3d06d73df86a4a6bb0d9411861e
|
F src/whereInt.h 83ae6f7d0fce8b5164d97698790e0005a909b3d06d73df86a4a6bb0d9411861e
|
||||||
F src/wherecode.c 620e81077588a727876a86f87aab310c02fc4e4960691e48153d8a87ca1fa453
|
F src/wherecode.c 620e81077588a727876a86f87aab310c02fc4e4960691e48153d8a87ca1fa453
|
||||||
F src/whereexpr.c 19394cb463003e9cc9305730b1508b8817a22bb7247170d81234b691a7f05b89
|
F src/whereexpr.c 19394cb463003e9cc9305730b1508b8817a22bb7247170d81234b691a7f05b89
|
||||||
@@ -1933,7 +1933,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
|
|||||||
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
|
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
|
||||||
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
|
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
|
||||||
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
|
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
|
||||||
P 0864bfbf079a98beaa484911edb17c3dc728e78eca231d4247b282fd824cc098 8029e6bab508bf0b8ec1df00a829c5d4de2a84d66dc56b9ff03da879ceef7788
|
P 11d97fb8be6b5155f38df130d7e828edd0f381e32f651458939b1cb9cb973fff 6225e9abcb0261fefca4a26530524ffc449f937f8ae1ece718af2c3c3d73d78d
|
||||||
R 62eaa6fad42f4285b48b7186b5cb4970
|
R 91b705f2730ed0122ffcaf8efbe0af73
|
||||||
U drh
|
U drh
|
||||||
Z 2d614121b81c56be6760e2166ceb6c54
|
Z dab48a2f628412943ed7bfde21b0a086
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
11d97fb8be6b5155f38df130d7e828edd0f381e32f651458939b1cb9cb973fff
|
41ba2dfdd3a18671fc78d60935a16fa50f36af3d6481eff2ca9fba88e7093997
|
||||||
171
src/where.c
171
src/where.c
@@ -4751,6 +4751,96 @@ static void showAllWhereLoops(WhereInfo *pWInfo, WhereClause *pWC){
|
|||||||
# define WHERETRACE_ALL_LOOPS(W,C)
|
# define WHERETRACE_ALL_LOOPS(W,C)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* Attempt to omit tables from a join that do not affect the result.
|
||||||
|
** For a table to not affect the result, the following must be true:
|
||||||
|
**
|
||||||
|
** 1) The query must not be an aggregate.
|
||||||
|
** 2) The table must be the RHS of a LEFT JOIN.
|
||||||
|
** 3) Either the query must be DISTINCT, or else the ON or USING clause
|
||||||
|
** must contain a constraint that limits the scan of the table to
|
||||||
|
** at most a single row.
|
||||||
|
** 4) The table must not be referenced by any part of the query apart
|
||||||
|
** from its own USING or ON clause.
|
||||||
|
**
|
||||||
|
** For example, given:
|
||||||
|
**
|
||||||
|
** CREATE TABLE t1(ipk INTEGER PRIMARY KEY, v1);
|
||||||
|
** CREATE TABLE t2(ipk INTEGER PRIMARY KEY, v2);
|
||||||
|
** CREATE TABLE t3(ipk INTEGER PRIMARY KEY, v3);
|
||||||
|
**
|
||||||
|
** then table t2 can be omitted from the following:
|
||||||
|
**
|
||||||
|
** SELECT v1, v3 FROM t1
|
||||||
|
** LEFT JOIN t2 ON (t1.ipk=t2.ipk)
|
||||||
|
** LEFT JOIN t3 ON (t1.ipk=t3.ipk)
|
||||||
|
**
|
||||||
|
** or from:
|
||||||
|
**
|
||||||
|
** SELECT DISTINCT v1, v3 FROM t1
|
||||||
|
** LEFT JOIN t2
|
||||||
|
** LEFT JOIN t3 ON (t1.ipk=t3.ipk)
|
||||||
|
*/
|
||||||
|
static SQLITE_NOINLINE Bitmask whereOmitNoopJoin(
|
||||||
|
WhereInfo *pWInfo,
|
||||||
|
Bitmask notReady
|
||||||
|
){
|
||||||
|
int i;
|
||||||
|
Bitmask tabUsed;
|
||||||
|
|
||||||
|
/* Preconditions checked by the caller */
|
||||||
|
assert( pWInfo->nLevel>=2 );
|
||||||
|
assert( OptimizationEnabled(pWInfo->pParse->db, SQLITE_OmitNoopJoin) );
|
||||||
|
|
||||||
|
/* These two preconditions checked by the caller combine to guarantee
|
||||||
|
** condition (1) of the header comment */
|
||||||
|
assert( pWInfo->pResultSet!=0 );
|
||||||
|
assert( 0==(pWInfo->wctrlFlags & WHERE_AGG_DISTINCT) );
|
||||||
|
|
||||||
|
tabUsed = sqlite3WhereExprListUsage(&pWInfo->sMaskSet, pWInfo->pResultSet);
|
||||||
|
if( pWInfo->pOrderBy ){
|
||||||
|
tabUsed |= sqlite3WhereExprListUsage(&pWInfo->sMaskSet, pWInfo->pOrderBy);
|
||||||
|
}
|
||||||
|
for(i=pWInfo->nLevel-1; i>=1; i--){
|
||||||
|
WhereTerm *pTerm, *pEnd;
|
||||||
|
SrcItem *pItem;
|
||||||
|
WhereLoop *pLoop;
|
||||||
|
pLoop = pWInfo->a[i].pWLoop;
|
||||||
|
pItem = &pWInfo->pTabList->a[pLoop->iTab];
|
||||||
|
if( (pItem->fg.jointype & JT_LEFT)==0 ) continue;
|
||||||
|
if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)==0
|
||||||
|
&& (pLoop->wsFlags & WHERE_ONEROW)==0
|
||||||
|
){
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if( (tabUsed & pLoop->maskSelf)!=0 ) continue;
|
||||||
|
pEnd = pWInfo->sWC.a + pWInfo->sWC.nTerm;
|
||||||
|
for(pTerm=pWInfo->sWC.a; pTerm<pEnd; pTerm++){
|
||||||
|
if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
|
||||||
|
if( !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
|
||||||
|
|| pTerm->pExpr->iRightJoinTable!=pItem->iCursor
|
||||||
|
){
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if( pTerm<pEnd ) continue;
|
||||||
|
WHERETRACE(0xffff, ("-> drop loop %c not used\n", pLoop->cId));
|
||||||
|
notReady &= ~pLoop->maskSelf;
|
||||||
|
for(pTerm=pWInfo->sWC.a; pTerm<pEnd; pTerm++){
|
||||||
|
if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
|
||||||
|
pTerm->wtFlags |= TERM_CODED;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if( i!=pWInfo->nLevel-1 ){
|
||||||
|
int nByte = (pWInfo->nLevel-1-i) * sizeof(WhereLevel);
|
||||||
|
memmove(&pWInfo->a[i], &pWInfo->a[i+1], nByte);
|
||||||
|
}
|
||||||
|
pWInfo->nLevel--;
|
||||||
|
assert( pWInfo->nLevel>0 );
|
||||||
|
}
|
||||||
|
return notReady;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Generate the beginning of the loop used for WHERE clause processing.
|
** Generate the beginning of the loop used for WHERE clause processing.
|
||||||
** The return value is a pointer to an opaque structure that contains
|
** The return value is a pointer to an opaque structure that contains
|
||||||
@@ -5119,34 +5209,15 @@ WhereInfo *sqlite3WhereBegin(
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Attempt to omit tables from the join that do not affect the result.
|
/* Attempt to omit tables from a join that do not affect the result.
|
||||||
** For a table to not affect the result, the following must be true:
|
** See the comment on whereOmitNoopJoin() for further information.
|
||||||
**
|
**
|
||||||
** 1) The query must not be an aggregate.
|
** This query optimization is factored out into a separate "no-inline"
|
||||||
** 2) The table must be the RHS of a LEFT JOIN.
|
** procedure to keep the sqlite3WhereBegin() procedure from becoming
|
||||||
** 3) Either the query must be DISTINCT, or else the ON or USING clause
|
** too large. If sqlite3WhereBegin() becomes too large, that prevents
|
||||||
** must contain a constraint that limits the scan of the table to
|
** some C-compiler optimizers from in-lining the
|
||||||
** at most a single row.
|
** sqlite3WhereCodeOneLoopStart() procedure, and it is important to
|
||||||
** 4) The table must not be referenced by any part of the query apart
|
** in-line sqlite3WhereCodeOneLoopStart() for performance reasons.
|
||||||
** from its own USING or ON clause.
|
|
||||||
**
|
|
||||||
** For example, given:
|
|
||||||
**
|
|
||||||
** CREATE TABLE t1(ipk INTEGER PRIMARY KEY, v1);
|
|
||||||
** CREATE TABLE t2(ipk INTEGER PRIMARY KEY, v2);
|
|
||||||
** CREATE TABLE t3(ipk INTEGER PRIMARY KEY, v3);
|
|
||||||
**
|
|
||||||
** then table t2 can be omitted from the following:
|
|
||||||
**
|
|
||||||
** SELECT v1, v3 FROM t1
|
|
||||||
** LEFT JOIN t2 ON (t1.ipk=t2.ipk)
|
|
||||||
** LEFT JOIN t3 ON (t1.ipk=t3.ipk)
|
|
||||||
**
|
|
||||||
** or from:
|
|
||||||
**
|
|
||||||
** SELECT DISTINCT v1, v3 FROM t1
|
|
||||||
** LEFT JOIN t2
|
|
||||||
** LEFT JOIN t3 ON (t1.ipk=t3.ipk)
|
|
||||||
*/
|
*/
|
||||||
notReady = ~(Bitmask)0;
|
notReady = ~(Bitmask)0;
|
||||||
if( pWInfo->nLevel>=2
|
if( pWInfo->nLevel>=2
|
||||||
@@ -5154,49 +5225,11 @@ WhereInfo *sqlite3WhereBegin(
|
|||||||
&& 0==(wctrlFlags & WHERE_AGG_DISTINCT) /* condition (1) above */
|
&& 0==(wctrlFlags & WHERE_AGG_DISTINCT) /* condition (1) above */
|
||||||
&& OptimizationEnabled(db, SQLITE_OmitNoopJoin)
|
&& OptimizationEnabled(db, SQLITE_OmitNoopJoin)
|
||||||
){
|
){
|
||||||
int i;
|
notReady = whereOmitNoopJoin(pWInfo, notReady);
|
||||||
Bitmask tabUsed = sqlite3WhereExprListUsage(pMaskSet, pResultSet);
|
nTabList = pWInfo->nLevel;
|
||||||
if( sWLB.pOrderBy ){
|
assert( nTabList>0 );
|
||||||
tabUsed |= sqlite3WhereExprListUsage(pMaskSet, sWLB.pOrderBy);
|
|
||||||
}
|
|
||||||
for(i=pWInfo->nLevel-1; i>=1; i--){
|
|
||||||
WhereTerm *pTerm, *pEnd;
|
|
||||||
SrcItem *pItem;
|
|
||||||
pLoop = pWInfo->a[i].pWLoop;
|
|
||||||
pItem = &pWInfo->pTabList->a[pLoop->iTab];
|
|
||||||
if( (pItem->fg.jointype & JT_LEFT)==0 ) continue;
|
|
||||||
if( (wctrlFlags & WHERE_WANT_DISTINCT)==0
|
|
||||||
&& (pLoop->wsFlags & WHERE_ONEROW)==0
|
|
||||||
){
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if( (tabUsed & pLoop->maskSelf)!=0 ) continue;
|
|
||||||
pEnd = sWLB.pWC->a + sWLB.pWC->nTerm;
|
|
||||||
for(pTerm=sWLB.pWC->a; pTerm<pEnd; pTerm++){
|
|
||||||
if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
|
|
||||||
if( !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
|
|
||||||
|| pTerm->pExpr->iRightJoinTable!=pItem->iCursor
|
|
||||||
){
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if( pTerm<pEnd ) continue;
|
|
||||||
WHERETRACE(0xffff, ("-> drop loop %c not used\n", pLoop->cId));
|
|
||||||
notReady &= ~pLoop->maskSelf;
|
|
||||||
for(pTerm=sWLB.pWC->a; pTerm<pEnd; pTerm++){
|
|
||||||
if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
|
|
||||||
pTerm->wtFlags |= TERM_CODED;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if( i!=pWInfo->nLevel-1 ){
|
|
||||||
int nByte = (pWInfo->nLevel-1-i) * sizeof(WhereLevel);
|
|
||||||
memmove(&pWInfo->a[i], &pWInfo->a[i+1], nByte);
|
|
||||||
}
|
|
||||||
pWInfo->nLevel--;
|
|
||||||
nTabList--;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(WHERETRACE_ENABLED)
|
#if defined(WHERETRACE_ENABLED)
|
||||||
if( sqlite3WhereTrace & 0x100 ){ /* Display all terms of the WHERE clause */
|
if( sqlite3WhereTrace & 0x100 ){ /* Display all terms of the WHERE clause */
|
||||||
sqlite3DebugPrintf("---- WHERE clause at end of analysis:\n");
|
sqlite3DebugPrintf("---- WHERE clause at end of analysis:\n");
|
||||||
|
|||||||
Reference in New Issue
Block a user