From: drh Date: Fri, 21 Sep 2018 18:43:51 +0000 (+0000) Subject: Put a limit counter on the query planner that restricts the number of X-Git-Tag: version-3.26.0~138 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=fc9098a45edd457b143cee75abb708fef440a34d;p=thirdparty%2Fsqlite.git Put a limit counter on the query planner that restricts the number of index+constraint options that can be considered for each table in a join. This prevents certain pathological queries from taking up too much time in the query planner. FossilOrigin-Name: 8690b5a0cc08eeb175230de45d4ca9b9f7b9b22aeebea70b8b7151f10b130969 --- diff --git a/manifest b/manifest index 20bfd8e4f6..8c8cc6575e 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Optimization\sto\sthe\sOP_MakeRecord\sopcode\smakes\sspeed-check.sh\srun\sabout\n1.1\smillion\scycles\sfaster,\sand\sresults\sin\sa\sslightly\ssmaller\slibrary. -D 2018-09-21T13:07:14.225 +C Put\sa\slimit\scounter\son\sthe\squery\splanner\sthat\srestricts\sthe\snumber\sof\nindex+constraint\soptions\sthat\scan\sbe\sconsidered\sfor\seach\stable\sin\sa\sjoin.\nThis\sprevents\scertain\spathological\squeries\sfrom\staking\sup\stoo\smuch\stime\nin\sthe\squery\splanner. +D 2018-09-21T18:43:51.308 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F Makefile.in 01e95208a78b57d056131382c493c963518f36da4c42b12a97eb324401b3a334 @@ -587,8 +587,8 @@ F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9 F src/wal.c 3f4f653daf234fe713edbcbca3fec2350417d159d28801feabc702a22c4e213f F src/wal.h 606292549f5a7be50b6227bd685fa76e3a4affad71bb8ac5ce4cb5c79f6a176a F src/walker.c fb94aadc9099ff9c6506d0a8b88d51266005bcaa265403f3d7caf732a562eb66 -F src/where.c 2019126801437944c38cc62a99491e98591460b7cc07ab57eb66165f710a289b -F src/whereInt.h b90ef9b9707ef750eab2a7a080c48fb4900315033274689def32d0cf5a81ebe4 +F src/where.c e5c4f5ab05e5112d971510bfd4cc673657e02673ac8a76db4296d703237a2461 +F src/whereInt.h 2c4d99faf71c35339a33b20d11c5cb8921416a21d6816d830b85df320303e5bd F src/wherecode.c 3df0a541373d5f999684d761e4bd700d57adb46c7d39da4e77b767b5adcd5893 F src/whereexpr.c 1b5a5a7876997f65232bbf19c5c1eeb47eb328b8fa5b28c865543052904cde00 F src/window.c a28d8d42c51c7e31136a42f3e245282049d4a9466b36d7bd765772991472df41 @@ -1769,7 +1769,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 22ae8a52dd2fb744f467c7dccf1d7fe7c7cef0e1dcc897dd492f897e84c9facb -R 33a3ccc9bec749141fe1417d8f2213ed +P d10e63629183f6daf0c263cd4dae052a3786c8c1480b3b6a73124b3315e41951 +R 56d8cd6e634d174344ffe41fce1a9ac6 U drh -Z 4fef5bba9e51ca59b92e81f3a1e63e07 +Z a5b6a9aaf70c834d7cee7cb5aaf91158 diff --git a/manifest.uuid b/manifest.uuid index 9e827d388a..3543112358 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -d10e63629183f6daf0c263cd4dae052a3786c8c1480b3b6a73124b3315e41951 \ No newline at end of file +8690b5a0cc08eeb175230de45d4ca9b9f7b9b22aeebea70b8b7151f10b130969 \ No newline at end of file diff --git a/src/where.c b/src/where.c index d8d577d521..f8727b0620 100644 --- a/src/where.c +++ b/src/where.c @@ -2126,6 +2126,10 @@ static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){ sqlite3 *db = pWInfo->pParse->db; int rc; + /* Stop the search once we hit the query planner search limit */ + if( pBuilder->iPlanLimit==0 ) return SQLITE_DONE; + pBuilder->iPlanLimit--; + /* If pBuilder->pOrSet is defined, then only keep track of the costs ** and prereqs. */ @@ -3532,9 +3536,17 @@ static int whereLoopAddAll(WhereLoopBuilder *pBuilder){ /* Loop over the tables in the join, from left to right */ pNew = pBuilder->pNew; whereLoopInit(pNew); + /* Some pathological queries provide an unreasonable number of indexing + ** options. The iPlanLimit value prevents these queries from taking up + ** too much time in the planner. When iPlanLimit reaches zero, no further + ** index+constraint options are considered. Seed iPlanLimit to 10K but + ** also add an extra 1K to each table of the join, to ensure that each + ** table at least gets 1K opportunities. */ + pBuilder->iPlanLimit = 10000; for(iTab=0, pItem=pTabList->a; pItemiTab = iTab; + pBuilder->iPlanLimit += 1000; /* 1000 bonus for each table in the join */ pNew->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, pItem->iCursor); if( ((pItem->fg.jointype|priorJointype) & (JT_LEFT|JT_CROSS))!=0 ){ /* This condition is true when pItem is the FROM clause term on the @@ -3560,7 +3572,14 @@ static int whereLoopAddAll(WhereLoopBuilder *pBuilder){ rc = whereLoopAddOr(pBuilder, mPrereq, mUnusable); } mPrior |= pNew->maskSelf; - if( rc || db->mallocFailed ) break; + if( rc || db->mallocFailed ){ + if( rc==SQLITE_DONE ){ + /* We hit the query planner search limit set by iPlanLimit */ + rc = SQLITE_OK; + }else{ + break; + } + } } whereLoopClear(db, pNew); diff --git a/src/whereInt.h b/src/whereInt.h index 8c87effac0..a34708df40 100644 --- a/src/whereInt.h +++ b/src/whereInt.h @@ -402,6 +402,7 @@ struct WhereLoopBuilder { int nRecValid; /* Number of valid fields currently in pRec */ #endif unsigned int bldFlags; /* SQLITE_BLDF_* flags */ + unsigned int iPlanLimit; /* Search limiter */ }; /* Allowed values for WhereLoopBuider.bldFlags */