1
0
mirror of https://github.com/sqlite/sqlite.git synced 2026-01-06 08:01:16 +03:00

Bug fixes in the ORDER BY optimizer.

FossilOrigin-Name: 301bbee4045aa169e29fb4fb75743b71eb4760a1
This commit is contained in:
drh
2012-10-08 19:41:38 +00:00
parent 8e4af1b997
commit 0a4c741cab
3 changed files with 29 additions and 10 deletions

View File

@@ -2803,17 +2803,30 @@ static int isSortingIndex(
sqlite3 *db = pParse->db; /* Database connection */
int nPriorSat; /* ORDER BY terms satisfied by outer loops */
int seenRowid = 0; /* True if an ORDER BY rowid term is seen */
int uniqueNotNull = 1; /* pIdx is UNIQUE with all terms are NOT NULL */
int uniqueNotNull; /* pIdx is UNIQUE with all terms are NOT NULL */
if( p->i==0 ){
nPriorSat = 0;
}else{
nPriorSat = p->aLevel[p->i-1].plan.nOBSat;
if( OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ) return nPriorSat;
if( (p->aLevel[p->i-1].plan.wsFlags & WHERE_ORDERED)==0 ){
/* This loop cannot be ordered unless the next outer loop is
** also ordered */
return nPriorSat;
}
if( OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ){
/* Only look at the outer-most loop if the OrderByIdxJoin
** optimization is disabled */
return nPriorSat;
}
}
pOrderBy = p->pOrderBy;
assert( pOrderBy!=0 );
if( pIdx->bUnordered ) return nPriorSat;
if( pIdx->bUnordered ){
/* Hash indices (indicated by the "unordered" tag on sqlite_stat1) cannot
** be used for sorting */
return nPriorSat;
}
nTerm = pOrderBy->nExpr;
uniqueNotNull = pIdx->onError!=OE_None;
assert( nTerm>0 );
@@ -2936,6 +2949,12 @@ static int isSortingIndex(
uniqueNotNull = 0;
}
}
/* If we have not found at least one ORDER BY term that matches the
** index, then show no progress. */
if( pOBItem==&pOrderBy->a[nPriorSat] ) return nPriorSat;
/* Return the necessary scan order back to the caller */
*pbRev = sortOrder & 1;
/* If there was an "ORDER BY rowid" term that matched, or it is only