Commit 071cfc03 authored by Sergey Vojtovich's avatar Sergey Vojtovich

BUG#39185 - Cardinality for merge tables calculated incorrectly.

Every subsequent query to a merge table with indexes was lowering
down cardinality.

The problem was that key statistics was not cleared when merge
children were detached. Causing next attach children perform
incremental key statistics calculation.

Fixed by clearing key statistics when attaching first child.

mysql-test/r/merge.result:
  A test case for BUG#39185.
mysql-test/t/merge.test:
  A test case for BUG#39185.
storage/myisammrg/myrg_open.c:
  Clear key statistics when we're attaching first child, even
  if it's buffer was allocated before. This is needed because
  detach_children() doesn't clear statistics, causing incremental
  statistics calculation.
parent b9d02d46
......@@ -2041,4 +2041,23 @@ EXPLAIN SELECT COUNT(*) FROM t4;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
DROP TABLE t1, t2, t3, t4;
CREATE TABLE t1(a INT, KEY(a));
INSERT INTO t1 VALUES(0),(1),(2),(3),(4);
ANALYZE TABLE t1;
Table Op Msg_type Msg_text
test.t1 analyze status OK
CREATE TABLE m1(a INT, KEY(a)) ENGINE=MERGE UNION=(t1);
SELECT CARDINALITY FROM INFORMATION_SCHEMA.STATISTICS WHERE TABLE_SCHEMA='test' AND TABLE_NAME='m1';
CARDINALITY
5
SELECT CARDINALITY FROM INFORMATION_SCHEMA.STATISTICS WHERE TABLE_SCHEMA='test' AND TABLE_NAME='m1';
CARDINALITY
5
SELECT CARDINALITY FROM INFORMATION_SCHEMA.STATISTICS WHERE TABLE_SCHEMA='test' AND TABLE_NAME='m1';
CARDINALITY
5
SELECT CARDINALITY FROM INFORMATION_SCHEMA.STATISTICS WHERE TABLE_SCHEMA='test' AND TABLE_NAME='m1';
CARDINALITY
5
DROP TABLE t1, m1;
End of 5.1 tests
......@@ -1435,4 +1435,17 @@ EXPLAIN SELECT COUNT(*) FROM t1;
EXPLAIN SELECT COUNT(*) FROM t4;
DROP TABLE t1, t2, t3, t4;
#
# BUG#39185 - Cardinality for merge tables calculated incorrectly.
#
CREATE TABLE t1(a INT, KEY(a));
INSERT INTO t1 VALUES(0),(1),(2),(3),(4);
ANALYZE TABLE t1;
CREATE TABLE m1(a INT, KEY(a)) ENGINE=MERGE UNION=(t1);
SELECT CARDINALITY FROM INFORMATION_SCHEMA.STATISTICS WHERE TABLE_SCHEMA='test' AND TABLE_NAME='m1';
SELECT CARDINALITY FROM INFORMATION_SCHEMA.STATISTICS WHERE TABLE_SCHEMA='test' AND TABLE_NAME='m1';
SELECT CARDINALITY FROM INFORMATION_SCHEMA.STATISTICS WHERE TABLE_SCHEMA='test' AND TABLE_NAME='m1';
SELECT CARDINALITY FROM INFORMATION_SCHEMA.STATISTICS WHERE TABLE_SCHEMA='test' AND TABLE_NAME='m1';
DROP TABLE t1, m1;
--echo End of 5.1 tests
......@@ -428,10 +428,11 @@ int myrg_attach_children(MYRG_INFO *m_info, int handle_locking,
if (!m_info->rec_per_key_part)
{
if(!(m_info->rec_per_key_part= (ulong*)
my_malloc(key_parts * sizeof(long), MYF(MY_WME|MY_ZEROFILL))))
my_malloc(key_parts * sizeof(long), MYF(MY_WME))))
goto err; /* purecov: inspected */
errpos= 1;
}
bzero((char*) m_info->rec_per_key_part, key_parts * sizeof(long));
}
/* Add MyISAM table info. */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment