aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordrh <>2021-07-16 01:19:19 +0000
committerdrh <>2021-07-16 01:19:19 +0000
commit0fb78f0cca4eedc1f2e68fc7fa3395ab6d46576a (patch)
treeac60df55a740dad98a806b97836f970719705c38 /src
parentbb301231788a7ca2292005ecf8d924dbf122f2d2 (diff)
downloadsqlite-0fb78f0cca4eedc1f2e68fc7fa3395ab6d46576a.tar.gz
sqlite-0fb78f0cca4eedc1f2e68fc7fa3395ab6d46576a.zip
Get the "omit ORDER BY in FROM-clause subqueries" optimization working for the
core test cases. FossilOrigin-Name: e31c5888659ffd4c6d8b68627123df3bbb84bb010b7766b0a74877bf3ba1e52b
Diffstat (limited to 'src')
-rw-r--r--src/select.c14
-rw-r--r--src/update.c1
2 files changed, 12 insertions, 3 deletions
diff --git a/src/select.c b/src/select.c
index a6513afca..e0ac9db97 100644
--- a/src/select.c
+++ b/src/select.c
@@ -6386,7 +6386,8 @@ int sqlite3Select(
/* If a FROM-clause subquery has an ORDER BY clause that is not
** really doing anything, then delete it now so that it does not
- ** interfere with query flattening.
+ ** interfere with query flattening. See the discussion at
+ ** https://sqlite.org/forum/forumpost/2d76f2bcf65d256a
**
** Beware of these cases where the ORDER BY clause may not be safely
** omitted:
@@ -6394,13 +6395,20 @@ int sqlite3Select(
** (1) There is also a LIMIT clause
** (2) The subquery was added to help with window-function
** processing
- ** (3) The outer query uses an aggregate function other than
+ ** (3) The subquery is in the FROM clause of an UPDATE
+ ** (4) The outer query uses an aggregate function other than
** the built-in count(), min(), or max().
+ ** (5) The ORDER BY isn't going to accomplish anything because
+ ** one of:
+ ** (a) The outer query has a different ORDER BY clause
+ ** (b) The subquery is part of a join
+ ** See forum post 062d576715d277c8
*/
if( pSub->pOrderBy!=0
+ && (p->pOrderBy!=0 || pTabList->nSrc>1) /* Condition (5) */
&& pSub->pLimit==0 /* Condition (1) */
&& (pSub->selFlags & SF_OrderByReqd)==0 /* Condition (2) */
- && (p->selFlags & SF_OrderByReqd)==0 /* Condition (3) */
+ && (p->selFlags & SF_OrderByReqd)==0 /* Condition (3) and (4) */
&& OptimizationEnabled(db, SQLITE_OmitOrderBy)
){
SELECTTRACE(0x100,pParse,p,
diff --git a/src/update.c b/src/update.c
index 45f332bf3..c5a01f896 100644
--- a/src/update.c
+++ b/src/update.c
@@ -261,6 +261,7 @@ static void updateFromSelect(
pSelect = sqlite3SelectNew(pParse, pList,
pSrc, pWhere2, pGrp, 0, pOrderBy2, SF_UFSrcCheck|SF_IncludeHidden, pLimit2
);
+ if( pSelect ) pSelect->selFlags |= SF_OrderByReqd;
sqlite3SelectDestInit(&dest, eDest, iEph);
dest.iSDParm2 = (pPk ? pPk->nKeyCol : -1);
sqlite3Select(pParse, pSelect, &dest);