1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-11-16 23:02:26 +03:00

Reduce the N in the logN term for the sorting cost associated with computing

DISTINCT by B-Tree by half, under the assumption that the DISTINCT will
eliminate about half the rows of output.  This is really a wild guess.  But
we do not have any better way of estimating what the row-count reduction due
to DISTINCT will actually be.

FossilOrigin-Name: 8787417ec1da8071d84c6ff0d7a90b5fd458ab6baba871327f36bc4e1bceca61
This commit is contained in:
drh
2020-08-24 23:44:27 +00:00
parent 599456f0ab
commit 58d6f633ba
3 changed files with 18 additions and 10 deletions

View File

@@ -4053,16 +4053,24 @@ static LogEst whereSortingCost(
** cost = (3.0 * N * log(N)) * (Y/X)
**
** The (Y/X) term is implemented using stack variable rScale
** below. */
** below.
*/
LogEst rScale, rSortCost;
assert( nOrderBy>0 && 66==sqlite3LogEst(100) );
rScale = sqlite3LogEst((nOrderBy-nSorted)*100/nOrderBy) - 66;
rSortCost = nRow + rScale + 16;
/* Multiple by log(M) where M is the number of output rows.
** Use the LIMIT for M if it is smaller */
** Use the LIMIT for M if it is smaller. Or if this sort is for
** a DISTINT operator, M will be the number of distinct output
** rows, so fudge it downwards a bit.
*/
if( (pWInfo->wctrlFlags & WHERE_USE_LIMIT)!=0 && pWInfo->iLimit<nRow ){
nRow = pWInfo->iLimit;
}else if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT) ){
/* TUNING: In the sort for a DISTINCT operator, assume that the DISTINCT
** reduces the number of output rows by a factor of 2 */
if( nRow>10 ) nRow -= 10; assert( 10==sqlite3LogEst(2) );
}
rSortCost += estLog(nRow);
return rSortCost;