Skip to content

Commit e6dee57

Browse files
committed
MDEV-9504: ANALYZE TABLE shows wrong 'rows' value for ORDER BY query
- Legacy code would set JOIN_TAB::limit only for EXPLAIN queries (this variable is only used when producing EXPLAIN output) - ANALYZE/SHOW EXPLAIN need to produce EXPLAIN output for non-EXPLAIN queries, too, so we should always set JOIN_TAB::limit.
1 parent 91ff017 commit e6dee57

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-4
lines changed

mysql-test/r/analyze_stmt_orderby.result

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,4 +583,28 @@ ANALYZE
583583
}
584584
}
585585
drop table t2;
586-
drop table t0,t1;
586+
drop table t1;
587+
#
588+
# MDEV-9504: ANALYZE TABLE shows wrong 'rows' value for ORDER BY query
589+
#
590+
create table t1 (
591+
a int,
592+
filler1 char(128),
593+
filler2 char(128),
594+
key(a)
595+
);
596+
insert into t1
597+
select A.a+10*B.a+100*C.a, repeat('abc-',32), repeat('abc-',32)
598+
from t0 A, t0 B, t0 C;
599+
analyze table t1;
600+
Table Op Msg_type Msg_text
601+
test.t1 analyze status Table is already up to date
602+
explain select a from t1 order by a limit 10;
603+
id select_type table type possible_keys key key_len ref rows Extra
604+
1 SIMPLE t1 index NULL a 5 NULL 10 Using index
605+
# 'rows' in ANALYZE output must match 'rows' in EXPLAIN:
606+
analyze select a from t1 order by a limit 10;
607+
id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
608+
1 SIMPLE t1 index NULL a 5 NULL 10 10.00 100.00 100.00 Using index
609+
drop table t1;
610+
drop table t0;

mysql-test/t/analyze_stmt_orderby.test

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,27 @@ select col1 f1, col2 f2, col1 f3 from t2 group by f1;
174174
drop table t2;
175175

176176

177-
drop table t0,t1;
177+
drop table t1;
178+
179+
--echo #
180+
--echo # MDEV-9504: ANALYZE TABLE shows wrong 'rows' value for ORDER BY query
181+
--echo #
182+
create table t1 (
183+
a int,
184+
filler1 char(128),
185+
filler2 char(128),
186+
key(a)
187+
);
188+
189+
insert into t1
190+
select A.a+10*B.a+100*C.a, repeat('abc-',32), repeat('abc-',32)
191+
from t0 A, t0 B, t0 C;
192+
analyze table t1;
193+
194+
explain select a from t1 order by a limit 10;
195+
--echo # 'rows' in ANALYZE output must match 'rows' in EXPLAIN:
196+
analyze select a from t1 order by a limit 10;
197+
198+
drop table t1;
199+
drop table t0;
200+

sql/sql_select.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21031,12 +21031,14 @@ test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit,
2103121031
}
2103221032

2103321033
table->file->ha_index_or_rnd_end();
21034+
21035+
if (select_limit < table->stat_records())
21036+
tab->limit= select_limit;
21037+
2103421038
if (tab->join->select_options & SELECT_DESCRIBE)
2103521039
{
2103621040
tab->ref.key= -1;
2103721041
tab->ref.key_parts= 0;
21038-
if (select_limit < table->stat_records())
21039-
tab->limit= select_limit;
2104021042
table->disable_keyread();
2104121043
}
2104221044
}

0 commit comments

Comments
 (0)