Skip to content

Commit f4e59d9

Browse files
author
Venkatesh Prasad
committed
PS-9647: MySQL Perf Improvements
https://perconadev.atlassian.net/browse/PS-9647 This patch uses list instead of deque for memory efficiency reasons, primarily to optimize operations like bulk insert
1 parent f9d0c3e commit f4e59d9

File tree

10 files changed

+260
-656
lines changed

10 files changed

+260
-656
lines changed

include/mem_root_deque.h

Lines changed: 226 additions & 631 deletions
Large diffs are not rendered by default.

sql/item_func.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,9 +575,10 @@ inline bool param_type_uses_non_param_inner(THD *thd, uint arg_count,
575575
return false;
576576
if (args[j]->type() == Item::ROW_ITEM)
577577
arguments[j] = down_cast<Item_row *>(args[j])->element_index(i);
578-
else if (args[j]->type() == Item::SUBSELECT_ITEM)
578+
else if (args[j]->type() == Item::SUBSELECT_ITEM) {
579579
arguments[j] = (*down_cast<Item_subselect *>(args[j])
580580
->unit->get_unit_column_types())[i];
581+
}
581582
}
582583
if (param_type_uses_non_param_inner(thd, arg_count, arguments, def))
583584
return true;

sql/sql_base.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9482,7 +9482,8 @@ bool insert_fields(THD *thd, Query_block *query_block, const char *db_name,
94829482
**it = item; /* Replace '*' with the first found item. */
94839483
} else {
94849484
/* Add 'item' to the SELECT list, after the current one. */
9485-
*it = fields->insert(*it + 1, item);
9485+
mem_root_deque<Item *>::iterator next_it = ++(*it);
9486+
*it = fields->insert(next_it, item);
94869487
}
94879488

94889489
/*

sql/sql_derived.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -545,10 +545,9 @@ bool copy_field_info(THD *thd, Item *orig_expr, Item *cloned_expr) {
545545
}
546546
if (inner_item->type() == Item::FIELD_ITEM) {
547547
Item_field *field = down_cast<Item_field *>(inner_item);
548-
if (field_info.push_back(
548+
field_info.push_back(
549549
Field_info(context, field->table_ref, depended_from,
550-
field->cached_table, field->field)))
551-
return true;
550+
field->cached_table, field->field));
552551
}
553552
return false;
554553
}))

sql/sql_executor.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4345,7 +4345,7 @@ bool change_to_use_tmp_fields(mem_root_deque<Item *> *fields, THD *thd,
43454345
Item_field *new_field = new Item_field(field);
43464346
if (!suv || !new_field) return true; // Fatal error
43474347
mem_root_deque<Item *> list(thd->mem_root);
4348-
if (list.push_back(new_field)) return true;
4348+
list.push_back(new_field);
43494349
if (suv->set_arguments(&list, true)) return true;
43504350
new_item = suv;
43514351
} else

sql/sql_optimizer.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7048,11 +7048,13 @@ static uint get_semi_join_select_list_index(Item_field *item_field) {
70487048
if (emb_sj_nest && emb_sj_nest->is_sj_or_aj_nest()) {
70497049
const mem_root_deque<Item *> &items =
70507050
emb_sj_nest->nested_join->sj_inner_exprs;
7051-
for (size_t i = 0; i < items.size(); i++) {
7052-
const Item *sel_item = items[i];
7051+
size_t i = 0;
7052+
for (auto it = items.begin(); it != items.end(); ++it) {
7053+
const Item *sel_item = *it;
70537054
if (sel_item->type() == Item::FIELD_ITEM &&
70547055
down_cast<const Item_field *>(sel_item)->field->eq(item_field->field))
70557056
return i;
7057+
i++;
70567058
}
70577059
}
70587060
return UINT_MAX;

sql/sql_parse.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1304,7 +1304,9 @@ void free_items(Item *item) {
13041304
*/
13051305
void cleanup_items(Item *item) {
13061306
DBUG_TRACE;
1307-
for (; item; item = item->next_free) item->cleanup();
1307+
for (; item; item = item->next_free) {
1308+
item->cleanup();
1309+
}
13081310
}
13091311

13101312
/**

sql/sql_resolver.cc

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5324,6 +5324,7 @@ bool Query_block::resolve_table_value_constructor_values(THD *thd) {
53245324
}
53255325

53265326
size_t item_index = 0;
5327+
auto field_it = fields.begin();
53275328
for (auto it = values_row->begin(); it != values_row->end(); ++it) {
53285329
Item *item = *it;
53295330
if ((!item->fixed && item->fix_fields(thd, &*it)) ||
@@ -5356,7 +5357,9 @@ bool Query_block::resolve_table_value_constructor_values(THD *thd) {
53565357
// Make sure to also replace the reference in item_list. In the case
53575358
// where fix_fields transforms an item, it.ref() will only update the
53585359
// reference of values_row.
5359-
if (first_execution) fields[item_index] = item;
5360+
if (first_execution) {
5361+
*field_it = item;
5362+
}
53605363
} else {
53615364
Item_values_column *column = down_cast<Item_values_column *>(
53625365
GetNthVisibleField(fields, item_index));
@@ -5365,6 +5368,7 @@ bool Query_block::resolve_table_value_constructor_values(THD *thd) {
53655368
column->fixed = true; // Does not have regular fix_fields()
53665369
}
53675370

5371+
field_it++;
53685372
++item_index;
53695373
}
53705374

@@ -5561,8 +5565,12 @@ bool Query_block::transform_table_subquery_to_join_with_derived(
55615565

55625566
// Append inner expressions of decorrelated equalities to the SELECT
55635567
// list. Correct context info of outer expressions.
5564-
auto it_outer = sj_outer_exprs.begin() + initial_sj_inner_exprs_count;
5565-
auto it_inner = sj_inner_exprs.begin() + initial_sj_inner_exprs_count;
5568+
auto it_outer = sj_outer_exprs.begin();
5569+
auto it_inner = sj_inner_exprs.begin();
5570+
for (int i = 0; i < initial_sj_inner_exprs_count; i++) {
5571+
it_outer++;
5572+
it_inner++;
5573+
}
55665574
for (int i = 0; it_outer != sj_outer_exprs.end();
55675575
++it_outer, ++it_inner, ++i) {
55685576
Item *inner = *it_inner;
@@ -6738,12 +6746,13 @@ bool Query_block::nest_derived(THD *thd, Item *join_cond,
67386746
return tl->join_cond() == join_cond;
67396747
});
67406748
assert(it != copy_list.end()); // assert that we found it
6741-
const size_t idx = it - copy_list.begin();
6742-
6743-
// Insert back all outer tables to the inner containing the condition.
6744-
// Normally only one.
6745-
for (size_t i = 0; i < idx; i++) {
6746-
jlist.push_front(copy_list[i]);
6749+
size_t idx = 0;
6750+
for (auto tmp = copy_list.begin(); tmp != copy_list.end(); ++tmp) {
6751+
if (it == tmp) {
6752+
break;
6753+
}
6754+
jlist.push_front(*tmp);
6755+
idx++;
67476756
}
67486757

67496758
// Insert the derived table and nest it with the outer(s)
@@ -6854,11 +6863,6 @@ bool Query_block::decorrelate_derived_scalar_subquery_pre(
68546863
if (selected_field == nullptr || f->field != selected_field->field) {
68556864
m_added_non_hidden_fields++;
68566865

6857-
// If f->hidden, f should be among the hidden fields in 'fields'.
6858-
assert(std::any_of(fields.cbegin(), fields.cbegin() + first_non_hidden,
6859-
[&f](const Item *item) { return f == item; }) ==
6860-
f->hidden);
6861-
68626866
Item_field *inner_field;
68636867

68646868
if (f->hidden) {

sql/sql_update.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2125,7 +2125,8 @@ static bool AddRowIdAsTempTableField(THD *thd, TABLE *table,
21252125
Item_field *ifield = new (thd->mem_root) Item_field(field);
21262126
if (ifield == nullptr) return true;
21272127
ifield->set_nullable(false);
2128-
return fields->push_back(ifield);
2128+
fields->push_back(ifield);
2129+
return false;
21292130
}
21302131

21312132
/// Stores the current row ID of "table" in the specified field of "tmp_table".

unittest/gunit/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ SET(TESTS
142142
like_range
143143
m_string
144144
mdl
145-
mem_root_deque
146145
mutex_lock
147146
my_alloc
148147
my_bitmap

0 commit comments

Comments
 (0)