Skip to content

Commit c6be744

Browse files
committed
MDEV-6398: ANALYZE UPDATE does not populate r_rows
- In print_explain_row(), do not forget to print r_rows. - Switch Explain_update from using its own counters to re-using Table_access_tracker. - Make ANALYZE UPDATE code structure uniform with ANALYZE DELETE.
1 parent a787edd commit c6be744

File tree

7 files changed

+51
-22
lines changed

7 files changed

+51
-22
lines changed

mysql-test/r/analyze_stmt.result

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ id select_type table type possible_keys key key_len ref rows r_rows filtered r_f
1616
# ANALYZE DELETE will delete rows:
1717
analyze delete from t1 where a in (2,3,4);
1818
id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
19-
1 SIMPLE t1 ALL NULL NULL NULL NULL 10 NULL 100.00 30.00 Using where
19+
1 SIMPLE t1 ALL NULL NULL NULL NULL 10 10 100.00 30.00 Using where
2020
select * from t1;
2121
a
2222
0
@@ -32,7 +32,7 @@ create table t1(a int, b int);
3232
insert into t1 select a,a from t0;
3333
analyze update t1 set b=100+b where a in (6,7,8);
3434
id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
35-
1 SIMPLE t1 ALL NULL NULL NULL NULL 10 NULL 100.00 30.00 Using where
35+
1 SIMPLE t1 ALL NULL NULL NULL NULL 10 10 100.00 30.00 Using where
3636
select * from t1;
3737
a b
3838
0 0
@@ -233,3 +233,12 @@ analyze update t1 set b=12345 where a > 30 and a < 10;
233233
id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
234234
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
235235
drop table t1;
236+
#
237+
# MDEV-6398: ANALYZE UPDATE does not populate r_rows
238+
#
239+
create table t1 (i int);
240+
insert into t1 values (1),(2),(3),(4);
241+
analyze update t1 set i=8;
242+
id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
243+
1 SIMPLE t1 ALL NULL NULL NULL NULL 4 4 100.00 100.00
244+
drop table t1;

mysql-test/t/analyze_stmt.test

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,11 @@ analyze update t1 set b=12345 where 1 > 2;
183183
analyze update t1 set b=12345 where a > 30 and a < 10;
184184

185185
drop table t1;
186+
--echo #
187+
--echo # MDEV-6398: ANALYZE UPDATE does not populate r_rows
188+
--echo #
189+
create table t1 (i int);
190+
insert into t1 values (1),(2),(3),(4);
191+
analyze update t1 set i=8;
192+
drop table t1;
186193

sql/sql_delete.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -539,10 +539,12 @@ bool mysql_delete(THD *thd, TABLE_LIST *table_list, COND *conds,
539539
}
540540

541541
explain= (Explain_delete*)thd->lex->explain->get_upd_del_plan();
542+
explain->tracker.on_scan_init();
543+
542544
while (!(error=info.read_record(&info)) && !thd->killed &&
543545
! thd->is_error())
544546
{
545-
explain->on_record_read();
547+
explain->tracker.on_record_read();
546548
if (table->vfield)
547549
update_virtual_fields(thd, table,
548550
table->triggers ? VCOL_UPDATE_ALL :
@@ -551,7 +553,7 @@ bool mysql_delete(THD *thd, TABLE_LIST *table_list, COND *conds,
551553
// thd->is_error() is tested to disallow delete row on error
552554
if (!select || select->skip_record(thd) > 0)
553555
{
554-
explain->on_record_after_where();
556+
explain->tracker.on_record_after_where();
555557
if (table->triggers &&
556558
table->triggers->process_triggers(thd, TRG_EVENT_DELETE,
557559
TRG_ACTION_BEFORE, FALSE))

sql/sql_explain.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,8 @@ int Explain_update::print_explain(Explain_query *query,
957957
Single-table DELETE commands do not do "Using temporary".
958958
"Using index condition" is also not possible (which is an unjustified limitation)
959959
*/
960-
double r_filtered= 100 * (r_rows?((double)r_rows_after_where/r_rows):1.0);
960+
double r_filtered= 100 * tracker.get_filtered_after_where();
961+
ha_rows r_rows= tracker.get_avg_rows();
961962

962963
print_explain_row(output, explain_flags, is_analyze,
963964
1, /* id */
@@ -970,7 +971,7 @@ int Explain_update::print_explain(Explain_query *query,
970971
key_len_buf.length() ? key_len_buf.c_ptr() : NULL,
971972
NULL, /* 'ref' is always NULL in single-table EXPLAIN DELETE */
972973
&rows,
973-
&r_rows,
974+
tracker.has_scans()? &r_rows : NULL,
974975
r_filtered,
975976
extra_str.c_ptr_safe());
976977

sql/sql_explain.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ class Table_access_tracker
4444

4545
return r_filtered;
4646
}
47+
48+
inline void on_scan_init() { r_scans++; }
49+
inline void on_record_read() { r_rows++; }
50+
inline void on_record_after_where() { r_rows_after_where++; }
4751
};
4852

4953

@@ -576,14 +580,8 @@ class Explain_update : public Explain_node
576580
bool using_io_buffer;
577581

578582
/* ANALYZE members and methods */
579-
ha_rows r_rows;
580-
ha_rows r_rows_after_where;
581-
inline void on_record_read() { r_rows++; }
582-
inline void on_record_after_where() { r_rows_after_where++; }
583+
Table_access_tracker tracker;
583584

584-
Explain_update() :
585-
r_rows(0), r_rows_after_where(0)
586-
{}
587585
virtual int print_explain(Explain_query *query, select_result_sink *output,
588586
uint8 explain_flags, bool is_analyze);
589587
};

sql/sql_select.cc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23131,7 +23131,15 @@ int print_explain_row(select_result_sink *result,
2313123131

2313223132
/* 'r_rows' */
2313323133
if (is_analyze)
23134-
item_list.push_back(item_null);
23134+
{
23135+
if (r_rows)
23136+
{
23137+
item_list.push_back(new Item_int(*r_rows,
23138+
MY_INT64_NUM_DECIMAL_DIGITS));
23139+
}
23140+
else
23141+
item_list.push_back(item_null);
23142+
}
2313523143

2313623144
/* 'filtered' */
2313723145
const double filtered=100.0;

sql/sql_update.cc

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -725,9 +725,11 @@ int mysql_update(THD *thd,
725725
if all updated columns are read
726726
*/
727727
can_compare_record= records_are_comparable(table);
728+
explain->tracker.on_scan_init();
729+
728730
while (!(error=info.read_record(&info)) && !thd->killed)
729731
{
730-
explain->on_record_read();
732+
explain->tracker.on_record_read();
731733
if (table->vfield)
732734
update_virtual_fields(thd, table,
733735
table->triggers ? VCOL_UPDATE_ALL :
@@ -738,7 +740,7 @@ int mysql_update(THD *thd,
738740
if (table->file->was_semi_consistent_read())
739741
continue; /* repeat the read of the same row if it still exists */
740742

741-
explain->on_record_after_where();
743+
explain->tracker.on_record_after_where();
742744
store_record(table,record[1]);
743745
if (fill_record_n_invoke_before_triggers(thd, table, fields, values, 0,
744746
TRG_EVENT_UPDATE))
@@ -947,6 +949,7 @@ int mysql_update(THD *thd,
947949

948950
end_read_record(&info);
949951
delete select;
952+
select= NULL;
950953
THD_STAGE_INFO(thd, stage_end);
951954
(void) table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
952955

@@ -996,11 +999,7 @@ int mysql_update(THD *thd,
996999
id= thd->arg_of_last_insert_id_function ?
9971000
thd->first_successful_insert_id_in_prev_stmt : 0;
9981001

999-
if (thd->lex->analyze_stmt)
1000-
{
1001-
error= thd->lex->explain->send_explain(thd);
1002-
}
1003-
else if (error < 0)
1002+
if (error < 0 && !thd->lex->analyze_stmt)
10041003
{
10051004
char buff[MYSQL_ERRMSG_SIZE];
10061005
my_snprintf(buff, sizeof(buff), ER(ER_UPDATE_INFO), (ulong) found,
@@ -1019,10 +1018,14 @@ int mysql_update(THD *thd,
10191018
}
10201019
*found_return= found;
10211020
*updated_return= updated;
1021+
1022+
1023+
if (thd->lex->analyze_stmt)
1024+
goto emit_explain_and_leave;
1025+
10221026
DBUG_RETURN((error >= 0 || thd->is_error()) ? 1 : 0);
10231027

10241028
err:
1025-
10261029
delete select;
10271030
free_underlaid_joins(thd, select_lex);
10281031
table->disable_keyread();
@@ -1036,6 +1039,7 @@ int mysql_update(THD *thd,
10361039
*/
10371040
query_plan.save_explain_data(thd->lex->explain);
10381041

1042+
emit_explain_and_leave:
10391043
int err2= thd->lex->explain->send_explain(thd);
10401044

10411045
delete select;

0 commit comments

Comments
 (0)