From 96f8058a7ceeb34d8f32a0b9df6baf0ca5e281a8 Mon Sep 17 00:00:00 2001 From: Igor Babaev Date: Wed, 27 Nov 2013 09:06:34 -0800 Subject: [PATCH] Fixed bug mdev-5204. Always use the value of table::file->stats.records when checking whether a table with HA_STATS_RECORDS_IS_EXACT flag contains not more than 1 record. --- mysql-test/r/stat_tables.result | 36 ++++++++++++++++++++++++++ mysql-test/r/stat_tables_innodb.result | 36 ++++++++++++++++++++++++++ mysql-test/t/stat_tables.test | 28 ++++++++++++++++++++ sql/sql_select.cc | 2 +- 4 files changed, 101 insertions(+), 1 deletion(-) diff --git a/mysql-test/r/stat_tables.result b/mysql-test/r/stat_tables.result index 6905725a6a2..63c65e23c19 100644 --- a/mysql-test/r/stat_tables.result +++ b/mysql-test/r/stat_tables.result @@ -381,3 +381,39 @@ id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE t1 ALL NULL NULL NULL NULL 2 DROP TABLE t1; set use_stat_tables=@save_use_stat_tables; +# +# Bug mdev-5204: invalid impossible where after reading const tables +# when use_stat_tables = 'preferably' +# +set use_stat_tables = 'preferably'; +CREATE TABLE t1 (id int PRIMARY KEY) ENGINE=MyISAM; +INSERT INTO t1 VALUES (1),(2); +ANALYZE TABLE t1; +Table Op Msg_type Msg_text +test.t1 analyze status OK +CREATE TABLE t2 (name char(3)) ENGINE=MyISAM; +ANALYZE TABLE t2; +Table Op Msg_type Msg_text +test.t2 analyze status Table is already up to date +INSERT INTO t2 VALUES ('USA'),('AUS'); +SELECT * FROM t1 STRAIGHT_JOIN t2 WHERE name IN ( 'AUS','YEM' ) AND id = 1; +id name +1 AUS +EXPLAIN +SELECT * FROM t1 STRAIGHT_JOIN t2 WHERE name IN ( 'AUS','YEM' ) AND id = 1; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 const PRIMARY PRIMARY 4 const 1 Using index +1 SIMPLE t2 ALL NULL NULL NULL NULL 0 Using where +ANALYZE TABLE t2; +Table Op Msg_type Msg_text +test.t2 analyze status OK +SELECT * FROM t1 STRAIGHT_JOIN t2 WHERE name IN ( 'AUS','YEM' ) AND id = 1; +id name +1 AUS +EXPLAIN +SELECT * FROM t1 STRAIGHT_JOIN t2 WHERE name IN ( 'AUS','YEM' ) AND id = 1; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 const PRIMARY PRIMARY 4 const 1 Using index +1 SIMPLE t2 ALL NULL NULL NULL NULL 2 Using where +DROP TABLE t1,t2; +set use_stat_tables=@save_use_stat_tables; diff --git a/mysql-test/r/stat_tables_innodb.result b/mysql-test/r/stat_tables_innodb.result index 51e1567309b..c601271c4d0 100644 --- a/mysql-test/r/stat_tables_innodb.result +++ b/mysql-test/r/stat_tables_innodb.result @@ -408,5 +408,41 @@ id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE t1 ALL NULL NULL NULL NULL 2 DROP TABLE t1; set use_stat_tables=@save_use_stat_tables; +# +# Bug mdev-5204: invalid impossible where after reading const tables +# when use_stat_tables = 'preferably' +# +set use_stat_tables = 'preferably'; +CREATE TABLE t1 (id int PRIMARY KEY) ENGINE=MyISAM; +INSERT INTO t1 VALUES (1),(2); +ANALYZE TABLE t1; +Table Op Msg_type Msg_text +test.t1 analyze status OK +CREATE TABLE t2 (name char(3)) ENGINE=MyISAM; +ANALYZE TABLE t2; +Table Op Msg_type Msg_text +test.t2 analyze status Table is already up to date +INSERT INTO t2 VALUES ('USA'),('AUS'); +SELECT * FROM t1 STRAIGHT_JOIN t2 WHERE name IN ( 'AUS','YEM' ) AND id = 1; +id name +1 AUS +EXPLAIN +SELECT * FROM t1 STRAIGHT_JOIN t2 WHERE name IN ( 'AUS','YEM' ) AND id = 1; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 const PRIMARY PRIMARY 4 const 1 Using index +1 SIMPLE t2 ALL NULL NULL NULL NULL 0 Using where +ANALYZE TABLE t2; +Table Op Msg_type Msg_text +test.t2 analyze status OK +SELECT * FROM t1 STRAIGHT_JOIN t2 WHERE name IN ( 'AUS','YEM' ) AND id = 1; +id name +1 AUS +EXPLAIN +SELECT * FROM t1 STRAIGHT_JOIN t2 WHERE name IN ( 'AUS','YEM' ) AND id = 1; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE t1 const PRIMARY PRIMARY 4 const 1 Using index +1 SIMPLE t2 ALL NULL NULL NULL NULL 2 Using where +DROP TABLE t1,t2; +set use_stat_tables=@save_use_stat_tables; set optimizer_switch=@save_optimizer_switch_for_stat_tables_test; SET SESSION STORAGE_ENGINE=DEFAULT; diff --git a/mysql-test/t/stat_tables.test b/mysql-test/t/stat_tables.test index 848fa272d39..25ca322ca0a 100644 --- a/mysql-test/t/stat_tables.test +++ b/mysql-test/t/stat_tables.test @@ -205,3 +205,31 @@ DROP TABLE t1; set use_stat_tables=@save_use_stat_tables; +--echo # +--echo # Bug mdev-5204: invalid impossible where after reading const tables +--echo # when use_stat_tables = 'preferably' +--echo # + +set use_stat_tables = 'preferably'; + +CREATE TABLE t1 (id int PRIMARY KEY) ENGINE=MyISAM; +INSERT INTO t1 VALUES (1),(2); +ANALYZE TABLE t1; + +CREATE TABLE t2 (name char(3)) ENGINE=MyISAM; +ANALYZE TABLE t2; +INSERT INTO t2 VALUES ('USA'),('AUS'); + +SELECT * FROM t1 STRAIGHT_JOIN t2 WHERE name IN ( 'AUS','YEM' ) AND id = 1; +EXPLAIN +SELECT * FROM t1 STRAIGHT_JOIN t2 WHERE name IN ( 'AUS','YEM' ) AND id = 1; + +ANALYZE TABLE t2; + +SELECT * FROM t1 STRAIGHT_JOIN t2 WHERE name IN ( 'AUS','YEM' ) AND id = 1; +EXPLAIN +SELECT * FROM t1 STRAIGHT_JOIN t2 WHERE name IN ( 'AUS','YEM' ) AND id = 1; + +DROP TABLE t1,t2; + +set use_stat_tables=@save_use_stat_tables; diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 3da62e2cc89..77f79783db0 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -3691,7 +3691,7 @@ make_join_statistics(JOIN *join, List &tables_list, // All dep. must be constants if (s->dependent & ~(found_const_table_map)) continue; - if (table->stat_records() <= 1L && + if (table->file->stats.records <= 1L && (table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) && !table->pos_in_table_list->embedding && !((outer_join & table->map) &&