| 353 | // |
| 354 | |
| 355 | void InnerJoin::findBestOrder(unsigned position, |
| 356 | StreamInfo* stream, |
| 357 | IndexedRelationships& processList, |
| 358 | double cost, |
| 359 | double cardinality) |
| 360 | { |
| 361 | const auto tail = &csb->csb_rpt[stream->number]; |
| 362 | |
| 363 | // Do some initializations |
| 364 | tail->activate(); |
| 365 | joinedStreams[position].reset(stream->number); |
| 366 | |
| 367 | // Save the various flag bits from the optimizer block to reset its |
| 368 | // state after each test |
| 369 | HalfStaticArray<bool, OPT_STATIC_ITEMS> streamFlags(innerStreams.getCount()); |
| 370 | for (const auto innerStream : innerStreams) |
| 371 | streamFlags.add(innerStream->used); |
| 372 | |
| 373 | // Compute delta and total estimate cost to fetch this stream |
| 374 | double positionCost = 0, positionCardinality = cardinality, newCost = 0, newCardinality = 0; |
| 375 | |
| 376 | if (!plan) |
| 377 | { |
| 378 | estimateCost(position, stream, positionCost, positionCardinality); |
| 379 | newCost = cost + positionCost; |
| 380 | newCardinality = cardinality * positionCardinality; |
| 381 | } |
| 382 | |
| 383 | position++; |
| 384 | |
| 385 | // If the partial order is either longer than any previous partial order, |
| 386 | // or the same length and cheap, save order as "best" |
| 387 | if (position > bestCount || (position == bestCount && newCost < bestCost)) |
| 388 | { |
| 389 | bestCount = position; |
| 390 | bestCost = newCost; |
| 391 | bestStreams.assign(joinedStreams.begin(), position); |
| 392 | } |
| 393 | |
| 394 | #ifdef OPT_DEBUG |
| 395 | // Debug information |
| 396 | printFoundOrder(position, positionCost, positionCardinality, newCost, newCardinality); |
| 397 | #endif |
| 398 | |
| 399 | // Mark this stream as "used" in the sense that it is already included |
| 400 | // in this particular proposed stream ordering |
| 401 | stream->used = true; |
| 402 | bool done = false; |
| 403 | |
| 404 | // If we've used up all the streams there's no reason to go any further |
| 405 | if (position == remainingStreams) |
| 406 | done = true; |
| 407 | |
| 408 | // If we know a combination with all streams used and the |
| 409 | // current cost is higher as the one from the best we're done |
| 410 | if (bestCount == remainingStreams && bestCost < newCost) |
| 411 | done = true; |
| 412 | |