[MDEV-11034] Setting aggregate result to local variable can change query plan Created: 2016-10-12  Updated: 2020-12-01

Status: Confirmed
Project: MariaDB Server
Component/s: Optimizer
Affects Version/s: 10.0.26, 10.1.18
Fix Version/s: 10.2

Type: Bug Priority: Major
Reporter: VAROQUI Stephane Assignee: Sergei Petrunia
Resolution: Unresolved Votes: 1
Labels: None


 Description   

MariaDB [RHE_MANAGER]> explain SELECT MAX(`CT_ID`) FROM `CONTRATS`;
+------+-------------+-------+------+---------------+------+---------+------+------+------------------------------+
| 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 |
+------+-------------+-------+------+---------------+------+---------+------+------+------------------------------+
1 row in set (0.00 sec)
 
  
explain SELECT @max:=MAX(`CT_ID`) FROM `CONTRATS`;
+------+-------------+----------+-------+---------------+--------------+---------+------+---------+-------------+
| id   | select_type | table    | type  | possible_keys | key          | key_len | ref  | rows    | Extra       |
+------+-------------+----------+-------+---------------+--------------+---------+------+---------+-------------+
|    1 | SIMPLE      | CONTRATS | index | NULL          | CT_EXPORTRHI | 2       | NULL | 2679710 | Using index |
+------+-------------+----------+-------+---------------+--------------+---------+------+---------+-------------+



 Comments   
Comment by Valerii Kravchuk [ 2016-10-12 ]

Here is a simple test case to get this, confirmed on 10.1.18:

drop table if exists tdummy;
create table tdummy(id int auto_increment primary key, c1 int, key(c1)) engine=InnoDB;
insert into tdummy(c1) values(1);
insert into tdummy(c1) select 2 from tdummy;
insert into tdummy(c1) select 2 from tdummy;
insert into tdummy(c1) select 2 from tdummy;
insert into tdummy(c1) select 2 from tdummy;
insert into tdummy(c1) select 2 from tdummy;
insert into tdummy(c1) select 2 from tdummy;
insert into tdummy(c1) select 2 from tdummy;
insert into tdummy(c1) select 2 from tdummy;
insert into tdummy(c1) select 2 from tdummy;
insert into tdummy(c1) select 2 from tdummy;
insert into tdummy(c1) select 2 from tdummy;
insert into tdummy(c1) select 2 from tdummy;
insert into tdummy(c1) select 2 from tdummy;
insert into tdummy(c1) select 2 from tdummy;
insert into tdummy(c1) select 2 from tdummy;
insert into tdummy(c1) select 2 from tdummy;
insert into tdummy(c1) select 2 from tdummy;
insert into tdummy(c1) select 2 from tdummy;
insert into tdummy(c1) select 2 from tdummy;
insert into tdummy(c1) select 2 from tdummy;
alter table tdummy add column c2 char(200) default 'a';
select version();
 
explain select max(c1) from tdummy;
explain select @max:=max(c1) from tdummy;
select max(c1) from tdummy;
select @max:=max(c1) from tdummy;

I get the following:

...
MariaDB [test]> insert into tdummy(c1) select 2 from tdummy;
Query OK, 524288 rows affected (4.88 sec)
Records: 524288  Duplicates: 0  Warnings: 0
 
MariaDB [test]> alter table tdummy add column c2 char(200) default 'a';
Query OK, 0 rows affected (45.03 sec)
Records: 0  Duplicates: 0  Warnings: 0
 
MariaDB [test]> select version();
+-----------------+
| version()       |
+-----------------+
| 10.1.18-MariaDB |
+-----------------+
1 row in set (0.00 sec)
 
MariaDB [test]> explain select max(c1) from tdummy;
+------+-------------+-------+------+---------------+------+---------+------+------+------------------------------+
| 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 |
+------+-------------+-------+------+---------------+------+---------+------+------+------------------------------+
1 row in set (0.02 sec)
 
MariaDB [test]> explain select @max:=max(c1) from tdummy;
+------+-------------+--------+-------+---------------+------+---------+------+---------+-------------+
| id   | select_type | table  | type  | possible_keys | key  | key_len | ref  | rows    | Extra       |
+------+-------------+--------+-------+---------------+------+---------+------+---------+-------------+
|    1 | SIMPLE      | tdummy | index | NULL          | c1   | 5       | NULL | 1032785 | Using index |
+------+-------------+--------+-------+---------------+------+---------+------+---------+-------------+
1 row in set (0.00 sec)
 
MariaDB [test]> select max(c1) from tdummy;
+---------+
| max(c1) |
+---------+
|       2 |
+---------+
1 row in set (0.00 sec)
 
MariaDB [test]> select @max:=max(c1) from tdummy;
+---------------+
| @max:=max(c1) |
+---------------+
|             2 |
+---------------+
1 row in set (0.24 sec)

Generated at Thu Feb 08 07:46:48 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.