Skip to content

Commit cd22b99

Browse files
committed
Rework fix for #5751 (Available indices are not used in some cases if ORDER BY expression is a filtered one) and also attempt to fix the special case for OR conditions partially matched to an index
1 parent 3d22e37 commit cd22b99

File tree

1 file changed

+52
-28
lines changed

1 file changed

+52
-28
lines changed

src/jrd/optimizer/Retrieval.cpp

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,45 +1414,69 @@ InversionCandidate* Retrieval::makeInversion(InversionCandidateList& inversions)
14141414
return invCandidate;
14151415
}
14161416

1417-
// Look if a match is already used by previous matches.
1418-
bool anyMatchAlreadyUsed = false, matchUsedByNavigation = false;
1419-
for (const auto currentMatch : currentInv->matches)
1417+
if (!customPlan)
14201418
{
1421-
if (matches.exist(currentMatch))
1422-
{
1423-
anyMatchAlreadyUsed = true;
1419+
// Look if a match is already used by previous matches
1420+
bool anyMatchAlreadyUsed = false, matchUsedByNavigation = false;
14241421

1425-
if (navigationCandidate &&
1426-
navigationCandidate->matches.exist(currentMatch))
1422+
for (const auto currentMatch : currentInv->matches)
1423+
{
1424+
if (matches.exist(currentMatch))
14271425
{
1428-
matchUsedByNavigation = true;
1429-
}
1426+
anyMatchAlreadyUsed = true;
14301427

1431-
break;
1428+
if (navigationCandidate &&
1429+
navigationCandidate->matches.exist(currentMatch))
1430+
{
1431+
matchUsedByNavigation = true;
1432+
}
1433+
1434+
break;
1435+
}
14321436
}
1433-
}
14341437

1435-
if (currentInv->boolean && matches.exist(currentInv->boolean))
1436-
anyMatchAlreadyUsed = true;
1438+
if (const auto currentMatch = currentInv->boolean)
1439+
{
1440+
if (matches.exist(currentMatch))
1441+
{
1442+
anyMatchAlreadyUsed = true;
14371443

1438-
if (anyMatchAlreadyUsed && !customPlan)
1439-
{
1440-
currentInv->used = true;
1444+
if (navigationCandidate &&
1445+
navigationCandidate->matches.exist(currentMatch))
1446+
{
1447+
matchUsedByNavigation = true;
1448+
}
1449+
}
1450+
else if (matchUsedByNavigation)
1451+
anyMatchAlreadyUsed = false;
1452+
}
14411453

1442-
if (matchUsedByNavigation)
1443-
continue;
1454+
// If some match was already used by another index, skip this index
14441455

1445-
// If a match on this index was already used by another
1446-
// index, add also the other matches from this index.
1447-
for (const auto currentMatch : currentInv->matches)
1456+
if (anyMatchAlreadyUsed)
14481457
{
1449-
if (!matches.exist(currentMatch))
1450-
matches.add(currentMatch);
1451-
}
1458+
if (!matchUsedByNavigation)
1459+
{
1460+
// Add the other matches from this index
1461+
1462+
for (const auto currentMatch : currentInv->matches)
1463+
{
1464+
if (!matches.exist(currentMatch))
1465+
matches.add(currentMatch);
1466+
}
1467+
1468+
if (const auto currentMatch = currentInv->boolean)
1469+
{
1470+
if (!matches.exist(currentMatch))
1471+
matches.add(currentMatch);
1472+
}
1473+
}
14521474

1453-
// Restart loop, because other indexes could also be excluded now.
1454-
restartLoop = true;
1455-
break;
1475+
// Restart loop, because other indexes could also be excluded now
1476+
currentInv->used = true;
1477+
restartLoop = true;
1478+
break;
1479+
}
14561480
}
14571481

14581482
if (!bestCandidate)

0 commit comments

Comments
 (0)