From 1c30ec097027ebef052992ac7accfcdf8a19f227 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 9 Dec 2003 23:52:58 +0300 Subject: [PATCH 1/2] Fix for bug #1335: filesort is missing in EXPLAIN if ORDER BY NULL is used mysql-test/r/group_by.result: test results fixed mysql-test/t/group_by.test: added few explicit tests for bug #1335 --- mysql-test/r/group_by.result | 17 ++++++++++++++++- mysql-test/t/group_by.test | 28 ++++++++++++++++++++++++++++ sql/sql_select.cc | 14 ++++++++++++-- 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/mysql-test/r/group_by.result b/mysql-test/r/group_by.result index f89cb8a6792..861f0f009cd 100644 --- a/mysql-test/r/group_by.result +++ b/mysql-test/r/group_by.result @@ -287,7 +287,7 @@ table type possible_keys key key_len ref rows Extra t1 ALL NULL NULL NULL NULL 8 Using filesort explain select sql_big_result spid,sum(userid) from t1 group by spid desc order by null; table type possible_keys key key_len ref rows Extra -t1 ALL NULL NULL NULL NULL 8 +t1 ALL NULL NULL NULL NULL 8 Using filesort select sql_big_result spid,sum(userid) from t1 group by spid desc; spid sum(userid) 7 3 @@ -597,3 +597,18 @@ count(*) category 1 3 1 4 drop table t1; +userid count(*) +3 5 +2 1 +1 2 +EXPLAIN SELECT userid,count(*) FROM t1 GROUP BY userid DESC; +table type possible_keys key key_len ref rows Extra +t1 ALL NULL NULL NULL NULL 8 Using temporary; Using filesort +i COUNT(DISTINCT(i)) +1 1 +2 1 +4 4 +explain SELECT i, COUNT(DISTINCT(i)) FROM t1 GROUP BY j ORDER BY NULL; +table type possible_keys key key_len ref rows Extra +t1 ALL NULL NULL NULL NULL 6 Using filesort +DROP TABLE t1; diff --git a/mysql-test/t/group_by.test b/mysql-test/t/group_by.test index df99bc2a9dc..d28eeb27c15 100644 --- a/mysql-test/t/group_by.test +++ b/mysql-test/t/group_by.test @@ -423,3 +423,31 @@ select id, sum(qty) as sqty, count(qty) as cqty from t1 group by id having sum(q select count(*), case interval(qty,2,3,4,5,6,7,8) when -1 then NULL when 0 then "zero" when 1 then "one" when 2 then "two" end as category from t1 group by category; select count(*), interval(qty,2,3,4,5,6,7,8) as category from t1 group by category; drop table t1; +# +# Tests for bug #1355: 'Using filesort' is missing in EXPLAIN when ORDER BY +# NULL is used. +# +--disable_query_log +CREATE TABLE t1 ( + userid int(10) unsigned, + score smallint(5) unsigned, + key (score) +); +INSERT INTO t1 VALUES (1,1),(2,2),(1,1),(3,3),(3,3),(3,3),(3,3),(3,3); +# Here we select unordered GROUP BY into a temporary talbe, +# and then sort it with filesort (GROUP BY in MySQL +# implies sorted order of results) +SELECT userid,count(*) FROM t1 GROUP BY userid DESC; +--enable_query_log +EXPLAIN SELECT userid,count(*) FROM t1 GROUP BY userid DESC; +--disable_query_log +DROP TABLE t1; +CREATE TABLE t1 ( + i int(11) default NULL, + j int(11) default NULL +); +INSERT INTO t1 VALUES (1,2),(2,3),(4,5),(3,5),(1,5),(23,5); +SELECT i, COUNT(DISTINCT(i)) FROM t1 GROUP BY j ORDER BY NULL; +--enable_query_log +explain SELECT i, COUNT(DISTINCT(i)) FROM t1 GROUP BY j ORDER BY NULL; +DROP TABLE t1; diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 7f8dfd219d0..df4b0226ff6 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -696,8 +696,18 @@ mysql_select(THD *thd,TABLE_LIST *tables,List &fields,COND *conds, if (select_options & SELECT_DESCRIBE) { - if (!order && !no_order) - order=group; + /* + Check if we managed to optimize ORDER BY away and don't use temporary + table to resolve ORDER BY: in that case, we only may need to do + filesort for GROUP BY. + */ + if (!order && !no_order && (!skip_sort_order || !need_tmp)) + { + /* Reset 'order' to 'group' and reinit variables describing 'order' */ + order= group; + simple_order= simple_group; + skip_sort_order= 0; + } if (order && (join.const_tables == join.tables || ((simple_order || skip_sort_order) && From b0a9299efee6db1be9340d02f73739f679ddd6a5 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 10 Dec 2003 01:08:10 +0300 Subject: [PATCH 2/2] bug #1335 tests followup: --disable_query_log was not a good idea, Serg says mysql-test/r/group_by.result: bug #1335 tests followup: --disable_query_log was not a good idea, Serg says tests results updated --- mysql-test/r/group_by.result | 14 ++++++++++++++ mysql-test/t/group_by.test | 4 ---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/mysql-test/r/group_by.result b/mysql-test/r/group_by.result index 861f0f009cd..dba95614052 100644 --- a/mysql-test/r/group_by.result +++ b/mysql-test/r/group_by.result @@ -597,6 +597,13 @@ count(*) category 1 3 1 4 drop table t1; +CREATE TABLE t1 ( +userid int(10) unsigned, +score smallint(5) unsigned, +key (score) +); +INSERT INTO t1 VALUES (1,1),(2,2),(1,1),(3,3),(3,3),(3,3),(3,3),(3,3); +SELECT userid,count(*) FROM t1 GROUP BY userid DESC; userid count(*) 3 5 2 1 @@ -604,6 +611,13 @@ userid count(*) EXPLAIN SELECT userid,count(*) FROM t1 GROUP BY userid DESC; table type possible_keys key key_len ref rows Extra t1 ALL NULL NULL NULL NULL 8 Using temporary; Using filesort +DROP TABLE t1; +CREATE TABLE t1 ( +i int(11) default NULL, +j int(11) default NULL +); +INSERT INTO t1 VALUES (1,2),(2,3),(4,5),(3,5),(1,5),(23,5); +SELECT i, COUNT(DISTINCT(i)) FROM t1 GROUP BY j ORDER BY NULL; i COUNT(DISTINCT(i)) 1 1 2 1 diff --git a/mysql-test/t/group_by.test b/mysql-test/t/group_by.test index d28eeb27c15..58bb4b3e268 100644 --- a/mysql-test/t/group_by.test +++ b/mysql-test/t/group_by.test @@ -427,7 +427,6 @@ drop table t1; # Tests for bug #1355: 'Using filesort' is missing in EXPLAIN when ORDER BY # NULL is used. # ---disable_query_log CREATE TABLE t1 ( userid int(10) unsigned, score smallint(5) unsigned, @@ -438,9 +437,7 @@ INSERT INTO t1 VALUES (1,1),(2,2),(1,1),(3,3),(3,3),(3,3),(3,3),(3,3); # and then sort it with filesort (GROUP BY in MySQL # implies sorted order of results) SELECT userid,count(*) FROM t1 GROUP BY userid DESC; ---enable_query_log EXPLAIN SELECT userid,count(*) FROM t1 GROUP BY userid DESC; ---disable_query_log DROP TABLE t1; CREATE TABLE t1 ( i int(11) default NULL, @@ -448,6 +445,5 @@ CREATE TABLE t1 ( ); INSERT INTO t1 VALUES (1,2),(2,3),(4,5),(3,5),(1,5),(23,5); SELECT i, COUNT(DISTINCT(i)) FROM t1 GROUP BY j ORDER BY NULL; ---enable_query_log explain SELECT i, COUNT(DISTINCT(i)) FROM t1 GROUP BY j ORDER BY NULL; DROP TABLE t1;