Lines 97323-97328
SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
Link Here
|
97323 |
int bestJ = -1; /* The value of j */ |
97323 |
int bestJ = -1; /* The value of j */ |
97324 |
Bitmask m; /* Bitmask value for j or bestJ */ |
97324 |
Bitmask m; /* Bitmask value for j or bestJ */ |
97325 |
int isOptimal; /* Iterator for optimal/non-optimal search */ |
97325 |
int isOptimal; /* Iterator for optimal/non-optimal search */ |
|
|
97326 |
int nUnconstrained; /* Number tables without INDEXED BY */ |
97327 |
Bitmask notIndexed; /* Mask of tables that cannot use an index */ |
97326 |
|
97328 |
|
97327 |
memset(&bestPlan, 0, sizeof(bestPlan)); |
97329 |
memset(&bestPlan, 0, sizeof(bestPlan)); |
97328 |
bestPlan.rCost = SQLITE_BIG_DBL; |
97330 |
bestPlan.rCost = SQLITE_BIG_DBL; |
Lines 97364-97369
SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
Link Here
|
97364 |
** algorithm may choose to use t2 for the outer loop, which is a much |
97366 |
** algorithm may choose to use t2 for the outer loop, which is a much |
97365 |
** costlier approach. |
97367 |
** costlier approach. |
97366 |
*/ |
97368 |
*/ |
|
|
97369 |
nUnconstrained = 0; |
97370 |
notIndexed = 0; |
97367 |
for(isOptimal=(iFrom<nTabList-1); isOptimal>=0; isOptimal--){ |
97371 |
for(isOptimal=(iFrom<nTabList-1); isOptimal>=0; isOptimal--){ |
97368 |
Bitmask mask; /* Mask of tables not yet ready */ |
97372 |
Bitmask mask; /* Mask of tables not yet ready */ |
97369 |
for(j=iFrom, pTabItem=&pTabList->a[j]; j<nTabList; j++, pTabItem++){ |
97373 |
for(j=iFrom, pTabItem=&pTabList->a[j]; j<nTabList; j++, pTabItem++){ |
Lines 97380-97385
SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
Link Here
|
97380 |
} |
97384 |
} |
97381 |
mask = (isOptimal ? m : notReady); |
97385 |
mask = (isOptimal ? m : notReady); |
97382 |
pOrderBy = ((i==0 && ppOrderBy )?*ppOrderBy:0); |
97386 |
pOrderBy = ((i==0 && ppOrderBy )?*ppOrderBy:0); |
|
|
97387 |
if( pTabItem->pIndex==0 ) nUnconstrained++; |
97383 |
|
97388 |
|
97384 |
assert( pTabItem->pTab ); |
97389 |
assert( pTabItem->pTab ); |
97385 |
#ifndef SQLITE_OMIT_VIRTUALTABLE |
97390 |
#ifndef SQLITE_OMIT_VIRTUALTABLE |
Lines 97393-97401
SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
Link Here
|
97393 |
} |
97398 |
} |
97394 |
assert( isOptimal || (sCost.used¬Ready)==0 ); |
97399 |
assert( isOptimal || (sCost.used¬Ready)==0 ); |
97395 |
|
97400 |
|
97396 |
if( (sCost.used¬Ready)==0 |
97401 |
/* If an INDEXED BY clause is present, then the plan must use that |
97397 |
&& (bestJ<0 || sCost.rCost<bestPlan.rCost |
97402 |
** index if it uses any index at all */ |
97398 |
|| (sCost.rCost<=bestPlan.rCost && sCost.nRow<bestPlan.nRow)) |
97403 |
assert( pTabItem->pIndex==0 |
|
|
97404 |
|| (sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0 |
97405 |
|| sCost.plan.u.pIdx==pTabItem->pIndex ); |
97406 |
|
97407 |
if( isOptimal && (sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ){ |
97408 |
notIndexed |= m; |
97409 |
} |
97410 |
|
97411 |
/* Conditions under which this table becomes the best so far: |
97412 |
** |
97413 |
** (1) The table must not depend on other tables that have not |
97414 |
** yet run. |
97415 |
** |
97416 |
** (2) A full-table-scan plan cannot supercede another plan unless |
97417 |
** it is an "optimal" plan as defined above. |
97418 |
** |
97419 |
** (3) All tables have an INDEXED BY clause or this table lacks an |
97420 |
** INDEXED BY clause or this table uses the specific |
97421 |
** index specified by its INDEXED BY clause. This rule ensures |
97422 |
** that a best-so-far is always selected even if an impossible |
97423 |
** combination of INDEXED BY clauses are given. The error |
97424 |
** will be detected and relayed back to the application later. |
97425 |
** The NEVER() comes about because rule (2) above prevents |
97426 |
** An indexable full-table-scan from reaching rule (3). |
97427 |
** |
97428 |
** (4) The plan cost must be lower than prior plans or else the |
97429 |
** cost must be the same and the number of rows must be lower. |
97430 |
*/ |
97431 |
if( (sCost.used¬Ready)==0 /* (1) */ |
97432 |
&& (bestJ<0 || (notIndexed&m)!=0 /* (2) */ |
97433 |
|| (sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0) |
97434 |
&& (nUnconstrained==0 || pTabItem->pIndex==0 /* (3) */ |
97435 |
|| NEVER((sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0)) |
97436 |
&& (bestJ<0 || sCost.rCost<bestPlan.rCost /* (4) */ |
97437 |
|| (sCost.rCost<=bestPlan.rCost && sCost.nRow<bestPlan.nRow)) |
97399 |
){ |
97438 |
){ |
97400 |
WHERETRACE(("... best so far with cost=%g and nRow=%g\n", |
97439 |
WHERETRACE(("... best so far with cost=%g and nRow=%g\n", |
97401 |
sCost.rCost, sCost.nRow)); |
97440 |
sCost.rCost, sCost.nRow)); |