From: drh <> Date: Wed, 15 Feb 2023 17:53:17 +0000 (+0000) Subject: Do not compute result columns of subqueries that are never used. Make those X-Git-Tag: version-3.42.0~332^2~4 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=e3ec00ccb8ea49dbe8c99fd5e5967dadaddd9852;p=thirdparty%2Fsqlite.git Do not compute result columns of subqueries that are never used. Make those columns NULL instead. This optimization potentially resolves the enhancement request described by [/tktview/baa5bb76c35a124c|ticket baa5bb76c35a124c]. FossilOrigin-Name: 5dec3cc0225296a043d17f73126d477d90a604f82b3180628176d8f950adbce8 --- diff --git a/manifest b/manifest index 91f4eba3e3..a52c37a9b2 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Update\stestrunner.tcl\sto\srun\szipvfs\stest\sscripts\son\sunix. -D 2023-02-14T18:09:40.825 +C Do\snot\scompute\sresult\scolumns\sof\ssubqueries\sthat\sare\snever\sused.\s\sMake\sthose\ncolumns\sNULL\sinstead.\s\sThis\soptimization\spotentially\sresolves\sthe\senhancement\nrequest\sdescribed\sby\s[/tktview/baa5bb76c35a124c|ticket\sbaa5bb76c35a124c]. +D 2023-02-15T17:53:17.086 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -621,7 +621,7 @@ F src/printf.c ff4b05e38bf928ff1b80d3dda4f977b10fe39ecbfe69c018224c7e5594fb2455 F src/random.c 606b00941a1d7dd09c381d3279a058d771f406c5213c9932bbd93d5587be4b9c F src/resolve.c d62c5665279cc7485f9d45b5e20911cc7b19c203f268321a90d05d74f4725750 F src/rowset.c ba9515a922af32abe1f7d39406b9d35730ed65efab9443dc5702693b60854c92 -F src/select.c c3ce1b49cca2c66c8c88fe7d9e1f3db23590deb4dd631619ad90e1e5d21bcf1f +F src/select.c 49d9336c4108a765831b2483d17cf52e196caaf341cba95646a220ec70ae080f F src/shell.c.in 5cb750dc00bb8b20b020031fd1bb0cb8759a6e567c27aee6b0af31d187984985 F src/sqlite.h.in c060f9e6f646fd817abf273c068666e079fc6e8467ea1d3e8b20f355f82722e9 F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8 @@ -1918,7 +1918,7 @@ F test/windowerr.tcl f5acd6fbc210d7b5546c0e879d157888455cd4a17a1d3f28f07c1c8a387 F test/windowerr.test a8b752402109c15aa1c5efe1b93ccb0ce1ef84fa964ae1cd6684dd0b3cc1819b F test/windowfault.test 15094c1529424e62f798bc679e3fe9dfab6e8ba2f7dfe8c923b6248c31660a7c F test/windowpushd.test d8895d08870b7226f7693665bd292eb177e62ca06799184957b3ca7dc03067df -F test/with1.test 9ad67fdeb2bbd808a5763c9060e214ea232f9b18d846ea3a59756dc38bdc3880 +F test/with1.test b93833890e5d2a368e78747f124503a0159aa029b98e9ed4795ebf630b2efd3d F test/with2.test a1df41b987198383b9b70bf5e5fda390582e46398653858dbc6ceb24253b28df F test/with3.test e7bf809bf75c1f44f98bca78bc331dbf542002c5227bf53c1261144db4e824c8 F test/with4.test 257be66c0c67fee1defbbac0f685c3465e2cad037f21ce65f23f86084f198205 @@ -2045,8 +2045,11 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P ecdeef43b27412b0b0b09e09a62ad3a03836a3fc80f2070268090e7ca8f02712 -R 008d808935e4782501905c43f1b48b8b -U dan -Z 0a26d7ad99fa8ab0735a6c8903ff6006 +P e6c8e19ab0d6e7526d4596b75a45bb6becaf3c029690f7e75c016eac803c9990 +R 790a0333fb08765177eb98bd169ddbf7 +T *branch * omit-unused-subquery-columns +T *sym-omit-unused-subquery-columns * +T -sym-trunk * +U drh +Z e4cd68bda867ee6d3fbf83551561c523 # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index d9053da512..715b2f5ee3 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -e6c8e19ab0d6e7526d4596b75a45bb6becaf3c029690f7e75c016eac803c9990 \ No newline at end of file +5dec3cc0225296a043d17f73126d477d90a604f82b3180628176d8f950adbce8 \ No newline at end of file diff --git a/src/select.c b/src/select.c index b0e303066a..4958ddefe9 100644 --- a/src/select.c +++ b/src/select.c @@ -5235,6 +5235,57 @@ static int pushDownWhereTerms( } #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */ +/* +** Check to see if a subquery contains result-set columns that are +** never used. If it does, change the value of those result-set columns +** to NULL so that they do not cause unnecessary work to compute. +** +** Return the number of column that were changed to NULL. +*/ +static int disableUnusedSubqueryResultColumns(SrcItem *pItem){ + int nCol; + Select *pSub; /* The subquery to be simplified */ + Select *pX; /* For looping over compound elements of pSub */ + Table *pTab; /* The table that describes the subquery */ + int j; /* Column number */ + int nChng = 0; /* Number of columns converted to NULL */ + + assert( pItem!=0 ); + assert( pItem->pTab!=0 ); + pTab = pItem->pTab; + if( pTab->tabFlags & TF_Ephemeral ) return 0; + assert( pItem->pSelect!=0 ); + pSub = pItem->pSelect; + assert( pSub->pEList->nExpr==pTab->nCol ); + for(pX=pSub; pX; pX=pX->pPrior){ + if( pX->pPrior && pX->op!=TK_ALL ){ + /* This optimization does not work for compound subqueries that + ** use UNION, INTERSECT, or EXCEPT. Only UNION ALL is allowed. */ + return 0; + } + if( pX->pWin ){ + /* This optimization does not work for subqueries that use window + ** functions. */ + return 0; + } + } + nCol = pTab->nCol; + for(j=0; jcolUsed)!=0 ) continue; + if( MASKBIT(j)==0 ) break; + for(pX=pSub; pX; pX=pX->pPrior) { + Expr *pY = pX->pEList->a[j].pExpr; + if( pY->op==TK_NULL ) continue; + pY->op = TK_NULL; + pX->selFlags |= SF_PushDown; + nChng++; + } + } + return nChng; +} + + /* ** The pFunc is the only aggregate function in the query. Check to see ** if the query is a candidate for the min/max optimization. @@ -7333,6 +7384,20 @@ int sqlite3Select( TREETRACE(0x4000,pParse,p,("Push-down not possible\n")); } + /* Convert unused result columns of the subquery into simple NULL + ** expressions, to avoid unneeded searching and computation. + */ + if( disableUnusedSubqueryResultColumns(pItem) ){ +#if TREETRACE_ENABLED + if( sqlite3TreeTrace & 0x4000 ){ + TREETRACE(0x4000,pParse,p, + ("Change unused result columns to NULL for subquery %d:\n", + pSub->selId)); + sqlite3TreeViewSelect(0, p, 0); + } +#endif + } + zSavedAuthContext = pParse->zAuthContext; pParse->zAuthContext = pItem->zName; diff --git a/test/with1.test b/test/with1.test index 91cbc79524..7400a7adf3 100644 --- a/test/with1.test +++ b/test/with1.test @@ -1124,7 +1124,7 @@ do_execsql_test 24.1 { CREATE VIEW v1 AS SELECT max(a), min(b) FROM t1 GROUP BY c; } do_test 24.1 { - set program [db eval {EXPLAIN SELECT 1 FROM v1,v1,v1}] + set program [db eval {EXPLAIN SELECT * FROM v1 AS aa, v1 AS bb, v1 AS cc}] expr [lsearch $program OpenDup]>0 } {1} do_execsql_test 24.2 {