Skip to content

Commit

Permalink
Implement improvement CORE-1069 : Optimize index scan when more than …
Browse files Browse the repository at this point in the history
…one index scanned and ANDed
  • Loading branch information
hvlad committed Dec 27, 2006
1 parent c63ae67 commit 2e343c7
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 23 deletions.
23 changes: 14 additions & 9 deletions src/jrd/btr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ static void print_int64_key(SINT64, SSHORT, INT64_KEY);
#endif
static CONTENTS remove_node(thread_db*, index_insertion*, WIN*);
static CONTENTS remove_leaf_node(thread_db*, index_insertion*, WIN*);
static bool scan(thread_db*, UCHAR*, RecordBitmap**, index_desc*,
static bool scan(thread_db*, UCHAR*, RecordBitmap**, RecordBitmap*, index_desc*,
IndexRetrieval*, USHORT, temporary_key*, const SCHAR,
bool&, const temporary_key&);
static void update_selectivity(index_root_page*, USHORT, const SelectivityList&);
Expand Down Expand Up @@ -560,7 +560,7 @@ static void checkForLowerKeySkip(
}


void BTR_evaluate(thread_db* tdbb, IndexRetrieval* retrieval, RecordBitmap** bitmap)
void BTR_evaluate(thread_db* tdbb, IndexRetrieval* retrieval, RecordBitmap** bitmap, RecordBitmap* bitmap_and)
{
/**************************************
*
Expand Down Expand Up @@ -661,7 +661,7 @@ void BTR_evaluate(thread_db* tdbb, IndexRetrieval* retrieval, RecordBitmap** bit
const UCHAR flags = page->btr_header.pag_flags;
// if there is an upper bound, scan the index pages looking for it
if (retrieval->irb_upper_count) {
while (scan(tdbb, pointer, bitmap, &idx, retrieval, prefix, &upper, flags,
while (scan(tdbb, pointer, bitmap, bitmap_and, &idx, retrieval, prefix, &upper, flags,
skipLowerKey, lower))
{
page = (btree_page*) CCH_HANDOFF(tdbb, &window, page->btr_sibling,
Expand Down Expand Up @@ -705,7 +705,8 @@ void BTR_evaluate(thread_db* tdbb, IndexRetrieval* retrieval, RecordBitmap** bit
}

if (!skipLowerKey) {
RBM_SET(tdbb->getDefaultPool(), bitmap, node.recordNumber.getValue());
if (!bitmap_and || bitmap_and->test(node.recordNumber.getValue()))
RBM_SET(tdbb->getDefaultPool(), bitmap, node.recordNumber.getValue());
}
pointer = BTreeNode::readNode(&node, pointer, flags, true);
// Check if pointer is still valid
Expand Down Expand Up @@ -6391,7 +6392,7 @@ static CONTENTS remove_leaf_node(thread_db* tdbb, index_insertion* insertion, WI
}


static bool scan(thread_db* tdbb, UCHAR* pointer, RecordBitmap** bitmap,
static bool scan(thread_db* tdbb, UCHAR* pointer, RecordBitmap** bitmap, RecordBitmap* bitmap_and,
index_desc* idx, IndexRetrieval* retrieval, USHORT prefix,
temporary_key* key, const SCHAR page_flags,
bool& skipLowerKey, const temporary_key& lowerKey)
Expand Down Expand Up @@ -6566,10 +6567,12 @@ static bool scan(thread_db* tdbb, UCHAR* pointer, RecordBitmap** bitmap,

if (!ignore && !skipLowerKey) {
if ((flag & irb_starting) || !count) {
RBM_SET(tdbb->getDefaultPool(), bitmap, node.recordNumber.getValue());
if (!bitmap_and || bitmap_and->test(node.recordNumber.getValue()))
RBM_SET(tdbb->getDefaultPool(), bitmap, node.recordNumber.getValue());
}
else if (p > (end_key - count)) {
RBM_SET(tdbb->getDefaultPool(), bitmap, node.recordNumber.getValue());
if (!bitmap_and || bitmap_and->test(node.recordNumber.getValue()))
RBM_SET(tdbb->getDefaultPool(), bitmap, node.recordNumber.getValue());
}
}

Expand Down Expand Up @@ -6693,10 +6696,12 @@ static bool scan(thread_db* tdbb, UCHAR* pointer, RecordBitmap** bitmap,

if (!ignore && !skipLowerKey) {
if ((flag & irb_starting) || !count) {
RBM_SET(tdbb->getDefaultPool(), bitmap, number);
if (!bitmap_and || bitmap_and->test(number))
RBM_SET(tdbb->getDefaultPool(), bitmap, number);
}
else if (p > (end_key - count)) {
RBM_SET(tdbb->getDefaultPool(), bitmap, number);
if (!bitmap_and || bitmap_and->test(number))
RBM_SET(tdbb->getDefaultPool(), bitmap, number);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/jrd/btr_proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void BTR_delete_index(Jrd::thread_db*, Jrd::win*, USHORT);
//USHORT BTR_delete_node(Jrd::thread_db*, Ods::btree_page*, USHORT);
bool BTR_description(Jrd::thread_db*, Jrd::jrd_rel*, Ods::index_root_page*, Jrd::index_desc*, SSHORT);
DSC* BTR_eval_expression(Jrd::thread_db*, Jrd::index_desc*, Jrd::Record*, bool&);
void BTR_evaluate(Jrd::thread_db*, Jrd::IndexRetrieval*, Jrd::RecordBitmap**);
void BTR_evaluate(Jrd::thread_db*, Jrd::IndexRetrieval*, Jrd::RecordBitmap**, Jrd::RecordBitmap*);
UCHAR* BTR_find_leaf(Ods::btree_page*, Jrd::temporary_key*, UCHAR*, USHORT*, bool, bool);
Ods::btree_page* BTR_find_page(Jrd::thread_db*, Jrd::IndexRetrieval*, Jrd::win*, Jrd::index_desc*,
Jrd::temporary_key*, Jrd::temporary_key*, bool);
Expand Down
26 changes: 16 additions & 10 deletions src/jrd/evl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ dsc* EVL_assign_to(thread_db* tdbb, jrd_nod* node)
}


RecordBitmap** EVL_bitmap(thread_db* tdbb, jrd_nod* node)
RecordBitmap** EVL_bitmap(thread_db* tdbb, jrd_nod* node, RecordBitmap* bitmap_and)
{
/**************************************
*
Expand All @@ -299,21 +299,25 @@ RecordBitmap** EVL_bitmap(thread_db* tdbb, jrd_nod* node)

switch (node->nod_type) {
case nod_bit_and:
return RecordBitmap::bit_and(
EVL_bitmap(tdbb, node->nod_arg[0]),
EVL_bitmap(tdbb, node->nod_arg[1]));
{
RecordBitmap** bitmap = EVL_bitmap(tdbb, node->nod_arg[0], bitmap_and);
if (!(*bitmap) || !(*bitmap)->getFirst())
return bitmap;
else
return EVL_bitmap(tdbb, node->nod_arg[1], *bitmap);
}

case nod_bit_or:
return RecordBitmap::bit_or(
EVL_bitmap(tdbb, node->nod_arg[0]),
EVL_bitmap(tdbb, node->nod_arg[1]));
EVL_bitmap(tdbb, node->nod_arg[0], bitmap_and),
EVL_bitmap(tdbb, node->nod_arg[1], bitmap_and));

case nod_bit_in:
{
RecordBitmap** inv_bitmap = EVL_bitmap(tdbb, node->nod_arg[0]);
RecordBitmap** inv_bitmap = EVL_bitmap(tdbb, node->nod_arg[0], bitmap_and);
BTR_evaluate(tdbb,
reinterpret_cast<IndexRetrieval*>(node->nod_arg[1]->nod_arg[e_idx_retrieval]),
inv_bitmap);
inv_bitmap, bitmap_and);
return inv_bitmap;
}

Expand All @@ -328,8 +332,10 @@ RecordBitmap** EVL_bitmap(thread_db* tdbb, jrd_nod* node)
RecordNumber rel_dbkey;
rel_dbkey.bid_decode(numbers);
// NS: Why the heck we decrement record number here? I have no idea, but retain the algorithm for now.
// hvlad: because from the user point of view db_key's begins from 1
rel_dbkey.decrement();
RBM_SET(tdbb->getDefaultPool(), &impure->inv_bitmap, rel_dbkey.getValue());
if (bitmap_and && bitmap_and->test(rel_dbkey.getValue()))
RBM_SET(tdbb->getDefaultPool(), &impure->inv_bitmap, rel_dbkey.getValue());
return &impure->inv_bitmap;
}

Expand All @@ -339,7 +345,7 @@ RecordBitmap** EVL_bitmap(thread_db* tdbb, jrd_nod* node)
RecordBitmap::reset(impure->inv_bitmap);
BTR_evaluate(tdbb,
reinterpret_cast<IndexRetrieval*>(node->nod_arg[e_idx_retrieval]),
&impure->inv_bitmap);
&impure->inv_bitmap, bitmap_and);
return &impure->inv_bitmap;
}

Expand Down
2 changes: 1 addition & 1 deletion src/jrd/evl_proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

// Implemented in evl.cpp
dsc* EVL_assign_to(Jrd::thread_db* tdbb, Jrd::jrd_nod*);
Jrd::RecordBitmap** EVL_bitmap(Jrd::thread_db* tdbb, Jrd::jrd_nod*);
Jrd::RecordBitmap** EVL_bitmap(Jrd::thread_db* tdbb, Jrd::jrd_nod*, Jrd::RecordBitmap*);
bool EVL_boolean(Jrd::thread_db* tdbb, Jrd::jrd_nod*);
dsc* EVL_expr(Jrd::thread_db* tdbb, Jrd::jrd_nod* const);
bool EVL_field(Jrd::jrd_rel*, Jrd::Record*, USHORT, dsc*);
Expand Down
2 changes: 1 addition & 1 deletion src/jrd/nav.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1206,7 +1206,7 @@ static bool setup_bitmaps(RecordSource* rsb, IRSB_NAV impure)
// There is no need to reset or release the bitmap, it is
// done in EVL_bitmap ().
impure->irsb_nav_bitmap = EVL_bitmap(tdbb,
reinterpret_cast<jrd_nod*>(rsb->rsb_arg[RSB_NAV_inversion]));
reinterpret_cast<jrd_nod*>(rsb->rsb_arg[RSB_NAV_inversion]), NULL);
return (*impure->irsb_nav_bitmap != NULL);
}

Expand Down
2 changes: 1 addition & 1 deletion src/jrd/rse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ void RSE_open(thread_db* tdbb, RecordSource* rsb)

switch (rsb->rsb_type) {
case rsb_indexed:
impure->irsb_bitmap = EVL_bitmap(tdbb, (jrd_nod*) rsb->rsb_arg[0]);
impure->irsb_bitmap = EVL_bitmap(tdbb, (jrd_nod*) rsb->rsb_arg[0], NULL);
impure->irsb_prefetch_number = -1;

case rsb_navigate:
Expand Down

0 comments on commit 2e343c7

Please sign in to comment.