From 80a38b92d41ed034598c4064669fcd6f4d28f3ee Mon Sep 17 00:00:00 2001 From: Igor Babaev Date: Tue, 11 Feb 2014 19:22:17 -0800 Subject: [PATCH] Fixed bug mdev-5630. The function calculate_cond_selectivity_for_table() must consider the case when the key range tree returned by the call of get_mm_tree() is of the type SEL_TREE::ALWAYS. --- mysql-test/r/selectivity.result | 18 ++++++++++++++++++ mysql-test/r/selectivity_innodb.result | 18 ++++++++++++++++++ mysql-test/t/selectivity.test | 22 ++++++++++++++++++++++ sql/opt_range.cc | 5 +++++ 4 files changed, 63 insertions(+) diff --git a/mysql-test/r/selectivity.result b/mysql-test/r/selectivity.result index 3059431c2e5..92395a6c9f6 100644 --- a/mysql-test/r/selectivity.result +++ b/mysql-test/r/selectivity.result @@ -1166,3 +1166,21 @@ set optimizer_use_condition_selectivity = 3; SELECT * FROM INFORMATION_SCHEMA.TRIGGERS WHERE SQL_MODE != ''; TRIGGER_CATALOG TRIGGER_SCHEMA TRIGGER_NAME EVENT_MANIPULATION EVENT_OBJECT_CATALOG EVENT_OBJECT_SCHEMA EVENT_OBJECT_TABLE ACTION_ORDER ACTION_CONDITION ACTION_STATEMENT ACTION_ORIENTATION ACTION_TIMING ACTION_REFERENCE_OLD_TABLE ACTION_REFERENCE_NEW_TABLE ACTION_REFERENCE_OLD_ROW ACTION_REFERENCE_NEW_ROW CREATED SQL_MODE DEFINER CHARACTER_SET_CLIENT COLLATION_CONNECTION DATABASE_COLLATION set optimizer_use_condition_selectivity=@save_optimizer_use_condition_selectivity; +# +# Bug mdev-5630: always true conjunctive condition +# when optimizer_use_condition_selectivity=3 +# +set use_stat_tables = 'preferably'; +set optimizer_use_condition_selectivity = 3; +CREATE TABLE t1 (a int) ENGINE=MyISAM; +INSERT INTO t1 VALUES (10); +CREATE TABLE t2 (id int, flag char(1), INDEX(id)) ENGINE=MyISAM; +INSERT INTO t2 VALUES (100,'0'),(101,'1'); +ANALYZE TABLE t1, t2; +Table Op Msg_type Msg_text +test.t1 analyze status OK +test.t2 analyze status OK +SELECT * FROM t1, t2 WHERE id = a AND ( a = 16 OR flag AND a != 6 ); +a id flag +DROP TABLE t1,t2; +set use_stat_tables=@save_use_stat_tables; diff --git a/mysql-test/r/selectivity_innodb.result b/mysql-test/r/selectivity_innodb.result index f4485cb0f73..a776da61d7f 100644 --- a/mysql-test/r/selectivity_innodb.result +++ b/mysql-test/r/selectivity_innodb.result @@ -1174,5 +1174,23 @@ set optimizer_use_condition_selectivity = 3; SELECT * FROM INFORMATION_SCHEMA.TRIGGERS WHERE SQL_MODE != ''; TRIGGER_CATALOG TRIGGER_SCHEMA TRIGGER_NAME EVENT_MANIPULATION EVENT_OBJECT_CATALOG EVENT_OBJECT_SCHEMA EVENT_OBJECT_TABLE ACTION_ORDER ACTION_CONDITION ACTION_STATEMENT ACTION_ORIENTATION ACTION_TIMING ACTION_REFERENCE_OLD_TABLE ACTION_REFERENCE_NEW_TABLE ACTION_REFERENCE_OLD_ROW ACTION_REFERENCE_NEW_ROW CREATED SQL_MODE DEFINER CHARACTER_SET_CLIENT COLLATION_CONNECTION DATABASE_COLLATION set optimizer_use_condition_selectivity=@save_optimizer_use_condition_selectivity; +# +# Bug mdev-5630: always true conjunctive condition +# when optimizer_use_condition_selectivity=3 +# +set use_stat_tables = 'preferably'; +set optimizer_use_condition_selectivity = 3; +CREATE TABLE t1 (a int) ENGINE=MyISAM; +INSERT INTO t1 VALUES (10); +CREATE TABLE t2 (id int, flag char(1), INDEX(id)) ENGINE=MyISAM; +INSERT INTO t2 VALUES (100,'0'),(101,'1'); +ANALYZE TABLE t1, t2; +Table Op Msg_type Msg_text +test.t1 analyze status OK +test.t2 analyze status OK +SELECT * FROM t1, t2 WHERE id = a AND ( a = 16 OR flag AND a != 6 ); +a id flag +DROP TABLE t1,t2; +set use_stat_tables=@save_use_stat_tables; set optimizer_switch=@save_optimizer_switch_for_selectivity_test; SET SESSION STORAGE_ENGINE=DEFAULT; diff --git a/mysql-test/t/selectivity.test b/mysql-test/t/selectivity.test index 82baf7cf1af..fe804cf459f 100644 --- a/mysql-test/t/selectivity.test +++ b/mysql-test/t/selectivity.test @@ -750,3 +750,25 @@ SELECT * FROM INFORMATION_SCHEMA.TRIGGERS WHERE SQL_MODE != ''; set optimizer_use_condition_selectivity=@save_optimizer_use_condition_selectivity; +--echo # +--echo # Bug mdev-5630: always true conjunctive condition +--echo # when optimizer_use_condition_selectivity=3 +--echo # + +set use_stat_tables = 'preferably'; +set optimizer_use_condition_selectivity = 3; + +CREATE TABLE t1 (a int) ENGINE=MyISAM; +INSERT INTO t1 VALUES (10); + +CREATE TABLE t2 (id int, flag char(1), INDEX(id)) ENGINE=MyISAM; +INSERT INTO t2 VALUES (100,'0'),(101,'1'); + +ANALYZE TABLE t1, t2; + +SELECT * FROM t1, t2 WHERE id = a AND ( a = 16 OR flag AND a != 6 ); + +DROP TABLE t1,t2; + +set use_stat_tables=@save_use_stat_tables; + diff --git a/sql/opt_range.cc b/sql/opt_range.cc index d10a512cad6..0387ea5f4db 100644 --- a/sql/opt_range.cc +++ b/sql/opt_range.cc @@ -3460,6 +3460,11 @@ bool calculate_cond_selectivity_for_table(THD *thd, TABLE *table, Item *cond) table->reginfo.impossible_range= 1; goto free_alloc; } + else if (tree->type == SEL_TREE::ALWAYS) + { + rows= table_records; + goto free_alloc; + } else if (tree->type == SEL_TREE::MAYBE) { rows= table_records;