|
Thank you! I repeated on 10.3-10.6:
-- source include/have_innodb.inc
|
|
CREATE TABLE t1 (usernum bigint(20), id bigint(20), KEY id (id)) engine=innodb;
|
insert into t1 values (1,1),(1,2);
|
|
CREATE TABLE t2 (id bigint(20), parent bigint(20), PRIMARY KEY (id)) engine=innodb;
|
insert into t2 values (1,0),(2,0);
|
|
CREATE TABLE t3 (num bigint(20), id bigint(20), KEY id (id)) engine=innodb;
|
insert into t3 values (11,1),(12,1),(13,1),(14,1),(15,1),(16,1),(17,1),(18,1),(19,1),(20,2),(21,2),(22,2),(23,2),(24,2),(25,2),(26,2),(27,2),(28,2),(29,2);
|
|
|
CREATE TABLE t4 ( num bigint(20), a varchar(20), dt datetime, st tinyint(3) unsigned,
|
PRIMARY KEY (num), KEY st (st)) engine=innodb;
|
INSERT INTO t4 VALUES (11,'','2021-10-05 15:43:04',2),(12,'','2021-10-05 15:43:04',0),(13,'','2021-10-05 15:43:04',2),(14,'','2021-10-05 15:43:04',0),(15,'','2021-10-05 15:43:04',2),(16,'','2021-10-05 15:43:04',0),(17,'','2021-10-05 15:43:04',2),(18,'','2021-10-05 15:43:04',0),(19,'','2021-10-05 15:43:04',2),(20,'','2021-10-05 15:43:04',0),(21,'','2021-10-05 15:43:04',2),(22,'','2021-10-05 15:43:04',0),(23,'','2021-10-05 15:43:04',2),(24,'','2021-10-05 15:43:04',0),(25,'','2021-10-05 15:43:04',2),(26,'','2021-10-05 15:43:04',0),(27,'','2021-10-05 15:43:04',2),(28,'','2021-10-05 15:43:04',0),(29,'','2021-10-05 15:43:04',2);
|
|
|
set session optimizer_switch='split_materialized=on';
|
|
select coalesce(dt.count,0)
|
from t2
|
left join t1 on t1.id = t2.id
|
left join t2 t2a on t2a.id = t2.parent
|
left join (
|
select t3.id, count(*) 'count'
|
from t3 join t4 on t4.num = t3.num
|
and not(coalesce(t4.a,'') != '' and t4.dt > now())
|
where t4.st = 0 group by t3.id
|
) dt on dt.id = t2.id
|
where t1.usernum = 1;
|
|
set session optimizer_switch='split_materialized=off';
|
|
select coalesce(dt.count,0)
|
from t2
|
left join t1 on t1.id = t2.id
|
left join t2 t2a on t2a.id = t2.parent
|
left join (
|
select t3.id, count(*) 'count'
|
from t3 join t4 on t4.num = t3.num
|
and not(coalesce(t4.a,'') != '' and t4.dt > now())
|
where t4.st = 0 group by t3.id
|
) dt on dt.id = t2.id
|
where t1.usernum = 1;
|
|
drop table t1,t2,t3,t4;
|
MariaDB [test]> select coalesce(dt.count,0) from t2 left join t1 on t1.id = t2.id left join t2 t2a on t2a.id = t2.parent left join ( select t3.id, count(*) 'count' from t3 join t4 on t4.num = t3.num and not(coalesce(t4.a,'') != '' and t4.dt > now()) where t4.st = 0 group by t3.id ) dt on dt.id = t2.id where t1.usernum = 1;
|
+----------------------+
|
| coalesce(dt.count,0) |
|
+----------------------+
|
| 9 |
|
| 10 |
|
+----------------------+
|
2 rows in set (0.019 sec)
|
|
MariaDB [test]> explain extended select coalesce(dt.count,0) from t2 left join t1 on t1.id = t2.id left join t2 t2a on t2a.id = t2.parent left join ( select t3.id, count(*) 'count' from t3 join t4 on t4.num = t3.num and not(coalesce(t4.a,'') != '' and t4.dt > now()) where t4.st = 0 group by t3.id ) dt on dt.id = t2.id where t1.usernum = 1;
|
+------+-----------------+------------+--------+---------------+---------+---------+-------------+------+----------+-------------------------------------------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
|
+------+-----------------+------------+--------+---------------+---------+---------+-------------+------+----------+-------------------------------------------------+
|
| 1 | PRIMARY | t2 | ALL | PRIMARY | NULL | NULL | NULL | 2 | 100.00 | |
|
| 1 | PRIMARY | t1 | ALL | id | NULL | NULL | NULL | 2 | 100.00 | Using where; Using join buffer (flat, BNL join) |
|
| 1 | PRIMARY | <derived2> | ref | key0 | key0 | 9 | test.t2.id | 2 | 100.00 | |
|
| 2 | LATERAL DERIVED | t3 | ref | id | id | 9 | test.t1.id | 1 | 100.00 | Using where |
|
| 2 | LATERAL DERIVED | t4 | eq_ref | PRIMARY,st | PRIMARY | 8 | test.t3.num | 1 | 47.37 | Using where |
|
+------+-----------------+------------+--------+---------------+---------+---------+-------------+------+----------+-------------------------------------------------+
|
5 rows in set, 1 warning (0.008 sec)
|
|
Note (Code 1003): /* select#1 */ select coalesce(`dt`.`count`,0) AS `coalesce(dt.count,0)` from `test`.`t2` join `test`.`t1` left join (/* select#2 */ select `test`.`t3`.`id` AS `id`,count(0) AS `count` from `test`.`t3` join `test`.`t4` where (coalesce(`test`.`t4`.`a`,'') = '' or `test`.`t4`.`dt` <= <cache>(current_timestamp())) and `test`.`t3`.`id` = `test`.`t1`.`id` and `test`.`t3`.`id` = `test`.`t2`.`id` and `test`.`t4`.`st` = 0 and `test`.`t4`.`num` = `test`.`t3`.`num` group by `test`.`t3`.`id`) `dt` on(`dt`.`id` = `test`.`t2`.`id`) where `test`.`t1`.`usernum` = 1 and `test`.`t1`.`id` = `test`.`t2`.`id`
|
|
MariaDB [test]> set session optimizer_switch='split_materialized=off';
|
Query OK, 0 rows affected (0.001 sec)
|
|
MariaDB [test]> select coalesce(dt.count,0) from t2 left join t1 on t1.id = t2.id left join t2 t2a on t2a.id = t2.parent left join ( select t3.id, count(*) 'count' from t3 join t4 on t4.num = t3.num and not(coalesce(t4.a,'') != '' and t4.dt > now()) where t4.st = 0 group by t3.id ) dt on dt.id = t2.id where t1.usernum = 1;
|
+----------------------+
|
| coalesce(dt.count,0) |
|
+----------------------+
|
| 4 |
|
| 5 |
|
+----------------------+
|
2 rows in set (0.016 sec)
|
|
MariaDB [test]> explain extended select coalesce(dt.count,0) from t2 left join t1 on t1.id = t2.id left join t2 t2a on t2a.id = t2.parent left join ( select t3.id, count(*) 'count' from t3 join t4 on t4.num = t3.num and not(coalesce(t4.a,'') != '' and t4.dt > now()) where t4.st = 0 group by t3.id ) dt on dt.id = t2.id where t1.usernum = 1;
|
+------+-------------+------------+--------+---------------+---------+---------+-------------+------+----------+-------------------------------------------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
|
+------+-------------+------------+--------+---------------+---------+---------+-------------+------+----------+-------------------------------------------------+
|
| 1 | PRIMARY | t2 | ALL | PRIMARY | NULL | NULL | NULL | 2 | 100.00 | |
|
| 1 | PRIMARY | <derived2> | ref | key0 | key0 | 9 | test.t2.id | 2 | 100.00 | |
|
| 1 | PRIMARY | t1 | ALL | id | NULL | NULL | NULL | 2 | 100.00 | Using where; Using join buffer (flat, BNL join) |
|
| 2 | DERIVED | t3 | ALL | NULL | NULL | NULL | NULL | 19 | 100.00 | Using where; Using temporary; Using filesort |
|
| 2 | DERIVED | t4 | eq_ref | PRIMARY,st | PRIMARY | 8 | test.t3.num | 1 | 47.37 | Using where |
|
+------+-------------+------------+--------+---------------+---------+---------+-------------+------+----------+-------------------------------------------------+
|
5 rows in set, 1 warning (0.008 sec)
|
|
Note (Code 1003): /* select#1 */ select coalesce(`dt`.`count`,0) AS `coalesce(dt.count,0)` from `test`.`t2` join `test`.`t1` left join (/* select#2 */ select `test`.`t3`.`id` AS `id`,count(0) AS `count` from `test`.`t3` join `test`.`t4` where `test`.`t4`.`st` = 0 and `test`.`t4`.`num` = `test`.`t3`.`num` and (coalesce(`test`.`t4`.`a`,'') = '' or `test`.`t4`.`dt` <= <cache>(current_timestamp())) group by `test`.`t3`.`id`) `dt` on(`dt`.`id` = `test`.`t2`.`id`) where `test`.`t1`.`usernum` = 1 and `test`.`t1`.`id` = `test`.`t2`.`id`
|
|