Skip to content

Commit

Permalink
[CBRD-24436] MAX / MIN performance enhancement (#3746)
Browse files Browse the repository at this point in the history
  • Loading branch information
beyondykk9 authored Aug 8, 2022
1 parent d0e35c3 commit cbae603
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 36 deletions.
33 changes: 15 additions & 18 deletions src/object/schema_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -5782,16 +5782,14 @@ BTID *
sm_find_index (MOP classop, char **att_names, int num_atts, bool unique_index_only, bool skip_prefix_index, BTID * btid)
{
int error = NO_ERROR;
int i;
int i = 0;
SM_CLASS *class_;
SM_CLASS_CONSTRAINT *con = NULL;
SM_ATTRIBUTE *att1, *att2;
BTID *index = NULL;
bool force_local_index = false;
int is_global = 0;

index = NULL;

error = au_fetch_class (classop, &class_, AU_FETCH_READ, AU_SELECT);
if (error != NO_ERROR)
{
Expand Down Expand Up @@ -5840,16 +5838,19 @@ sm_find_index (MOP classop, char **att_names, int num_atts, bool unique_index_on
}
}

if (skip_prefix_index && num_atts > 0 && con->attributes[0] != NULL && con->attrs_prefix_length
&& con->attrs_prefix_length[0] > 0)
if (num_atts > 0)
{
continue;
}
if (skip_prefix_index && con->attributes[0] != NULL && con->attrs_prefix_length
&& con->attrs_prefix_length[0] > 0)
{
continue;
}

if (num_atts == 0)
{
/* we don't care about attributes, any index is a good one */
break;
/* exclude filter or function index */
if (con->filter_predicate || con->func_index_info)
{
continue;
}
}

for (i = 0; i < num_atts; i++)
Expand All @@ -5867,19 +5868,15 @@ sm_find_index (MOP classop, char **att_names, int num_atts, bool unique_index_on
}
}

if ((i == num_atts) && con->attributes[i] == NULL)
if (i == num_atts)
{
/* found it */
BTID_COPY (btid, &con->index_btid);
index = btid;
break;
}
}

if (con)
{
BTID_COPY (btid, &con->index_btid);
index = btid;
}

return (index);
}

Expand Down
18 changes: 15 additions & 3 deletions src/parser/xasl_generation.c
Original file line number Diff line number Diff line change
Expand Up @@ -4179,13 +4179,14 @@ pt_to_aggregate_node (PARSER_CONTEXT * parser, PT_NODE * tree, void *arg, int *c
aggregate_list->flag_agg_optimize = false;
BTID_SET_NULL (&aggregate_list->btid);
if (info->flag_agg_optimize
&& (aggregate_list->function == PT_COUNT_STAR || aggregate_list->function == PT_COUNT
&& (aggregate_list->function == PT_COUNT_STAR
|| aggregate_list->function == PT_MAX || aggregate_list->function == PT_MIN))
{
BTID *btid = NULL;
bool need_unique_index;

classop = sm_find_class (info->class_name);
if (aggregate_list->function == PT_COUNT_STAR || aggregate_list->function == PT_COUNT)
if (aggregate_list->function == PT_COUNT_STAR)
{
need_unique_index = true;
}
Expand All @@ -4197,14 +4198,25 @@ pt_to_aggregate_node (PARSER_CONTEXT * parser, PT_NODE * tree, void *arg, int *c
/* enable count optimization in MVCC if have unique index */
if (aggregate_list->function == PT_COUNT_STAR)
{
BTID *btid = NULL;
btid = sm_find_index (classop, NULL, 0, need_unique_index, false, &aggregate_list->btid);
if (btid != NULL)
{
/* If btree does not exist, optimize with heap in non-MVCC */
aggregate_list->flag_agg_optimize = true;
}
}
else if (tree->info.function.arg_list->node_type == PT_NAME)
{
/* need to get an index has the argument name as first attribute */
/* no prefix, no filter, no function */
btid = sm_find_index (classop, (char **) &tree->info.function.arg_list->info.name.original,
1, need_unique_index, true, &aggregate_list->btid);
if (btid != NULL)
{
/* If btree does not exist, optimize with heap in non-MVCC */
aggregate_list->flag_agg_optimize = true;
}
}
}

if (aggregate_list->function != PT_COUNT_STAR && aggregate_list->function != PT_GROUPBY_NUM)
Expand Down
121 changes: 106 additions & 15 deletions src/storage/btree.c
Original file line number Diff line number Diff line change
Expand Up @@ -1792,6 +1792,71 @@ static DISK_ISVALID btree_check_tree (THREAD_ENTRY * thread_p, const OID * class
static DISK_ISVALID btree_check_by_btid (THREAD_ENTRY * thread_p, BTID * btid);
static char *btree_unpack_mvccinfo (char *ptr, BTREE_MVCC_INFO * mvcc_info, short btree_mvcc_flags);

static int btree_is_key_visible (THREAD_ENTRY * thread_p, BTID_INT * btid, PAGE_PTR pg_ptr,
MVCC_SNAPSHOT * mvcc_snapshot, int slot_id, bool * is_visible, DB_VALUE * key_value);

/*
* btree_is_key_visible(): States if current key is visible or not.
*
* thread_p(in): Thread entry.
* btid(in): B-tree info.
* pg_ptr(in): Page pointer.
* mvcc_snapshot(in): The MVCC snapshot.
* slot_id(in) : Slot id to be looked for.
* is_visible(out): True or False
*
* return: error code if any error occurs.
*/
static int
btree_is_key_visible (THREAD_ENTRY * thread_p, BTID_INT * btid, PAGE_PTR pg_ptr, MVCC_SNAPSHOT * mvcc_snapshot,
int slot_id, bool * is_visible, DB_VALUE * key_value)
{
RECDES record;
LEAF_REC leaf;
int num_visible = 0;
int key_offset = 0;
int ret = NO_ERROR;
int max_visible_oids = 1;
bool dummy_clear_key;

assert (mvcc_snapshot != NULL);

*is_visible = false;

/* Get the record. */
if (spage_get_record (thread_p, pg_ptr, slot_id, &record, PEEK) != S_SUCCESS)
{
assert_release (false);
ret = ER_FAILED;
return ret;
}

/* Read the record. - no need of actual key value */
ret = btree_read_record (thread_p, btid, pg_ptr, &record, key_value, &leaf, BTREE_LEAF_NODE, &dummy_clear_key,
&key_offset, PEEK_KEY_VALUE, NULL);
if (ret != NO_ERROR)
{
ASSERT_ERROR ();
return ret;
}

/* Get the number of visible items. */
ret =
btree_get_num_visible_from_leaf_and_ovf (thread_p, btid, &record, key_offset, &leaf, &max_visible_oids,
mvcc_snapshot, &num_visible);
if (ret != NO_ERROR)
{
return ret;
}

if (num_visible > 0)
{
*is_visible = true;
}

return ret;
}

/*
* btree_fix_root_with_info () - Fix b-tree root page and output its VPID, header and b-tree info if requested.
*
Expand Down Expand Up @@ -16377,14 +16442,13 @@ btree_find_min_or_max_key (THREAD_ENTRY * thread_p, BTID * btid, DB_VALUE * key,
{
VPID root_vpid;
PAGE_PTR root_page_ptr = NULL;
int offset;
bool clear_key = false;
DB_VALUE key_value;
bool is_visible = false;
DB_VALUE key_value, *value_p = &key_value;
BTREE_ROOT_HEADER *root_header = NULL;
RECDES rec;
LEAF_REC leaf_pnt;
BTREE_SCAN btree_scan, *BTS;
int ret = NO_ERROR;
MVCC_SNAPSHOT *mvcc_snapshot;

if (key == NULL)
{
Expand Down Expand Up @@ -16424,8 +16488,6 @@ btree_find_min_or_max_key (THREAD_ENTRY * thread_p, BTID * btid, DB_VALUE * key,

pgbuf_unfix_and_init (thread_p, root_page_ptr);

assert (tp_valid_indextype (TP_DOMAIN_TYPE (BTS->btid_int.key_type)));

/*
* in case of desc domain index,
* we have to find the min/max key in opposite order.
Expand All @@ -16450,25 +16512,54 @@ btree_find_min_or_max_key (THREAD_ENTRY * thread_p, BTID * btid, DB_VALUE * key,
goto exit_on_error;
}

if (!BTREE_END_OF_SCAN (BTS))
if ((mvcc_snapshot = logtb_get_mvcc_snapshot (thread_p)) == NULL)
{
assert (BTS->slot_id > 0);
if (spage_get_record (thread_p, BTS->C_page, BTS->slot_id, &rec, PEEK) != S_SUCCESS)
goto exit_on_error;
}

mvcc_snapshot->snapshot_fnc = mvcc_satisfies_snapshot;

while (!BTREE_END_OF_SCAN (BTS))
{
/* get a visible key on mvcc */
ret =
btree_is_key_visible (thread_p, &BTS->btid_int, BTS->C_page, mvcc_snapshot, BTS->slot_id, &is_visible,
&key_value);

if (ret != NO_ERROR)
{
goto exit_on_error;
}

if (btree_read_record (thread_p, &BTS->btid_int, BTS->C_page, &rec, &key_value, (void *) &leaf_pnt,
BTREE_LEAF_NODE, &clear_key, &offset, PEEK_KEY_VALUE, NULL) != NO_ERROR)
if (is_visible)
{
goto exit_on_error;
}
value_p = &key_value;
if (key_value.domain.general_info.type == DB_TYPE_MIDXKEY)
{
DB_MIDXKEY *midxkey_val;

(void) pr_clone_value (&key_value, key);
midxkey_val = db_get_midxkey (&key_value);
pr_midxkey_get_element_nocopy (midxkey_val, 0, value_p, NULL, NULL);
}

btree_clear_key_value (&clear_key, &key_value);
/* find only not null */
if (db_value_is_null (value_p) == false)
{
break;
}
}

ret = btree_find_next_index_record (thread_p, BTS);
if (ret != NO_ERROR)
{
ASSERT_ERROR ();
goto end;
}
}

(void) pr_clone_value (value_p, key);
btree_clear_key_value (&clear_key, &key_value);

end:

if (BTS->P_page != NULL)
Expand Down

0 comments on commit cbae603

Please sign in to comment.