|
Thank you very much, now I could repeat it, with InnoDB/Aria/Myisam/ on 10.3-10.10
If I drop the index, then results are correct.
CREATE TABLE `test2` (
|
`timestamp` timestamp NOT NULL DEFAULT current_timestamp(),
|
`flat` tinyint(4) unsigned NOT NULL,
|
`rssi` tinyint(4) NOT NULL,
|
`type` enum('WATER_COLD','WATER_HOT') NOT NULL,
|
`value` decimal(9,3) unsigned NOT NULL,
|
`error_flags` smallint(5) unsigned NOT NULL,
|
PRIMARY KEY (`timestamp`,`flat`,`type`)
|
) ;
|
|
INSERT INTO `test2` VALUES
|
('2022-08-10 00:02:42', 20, -77, 'WATER_COLD', 287.211, 0),
|
('2022-08-10 00:04:30', 20, -68, 'WATER_HOT', 158.653, 0),
|
('2022-08-10 00:07:44', 20, -79, 'WATER_COLD', 287.211, 0),
|
('2022-08-10 00:09:28', 20, -68, 'WATER_HOT', 158.653, 0),
|
('2022-08-10 00:12:45', 20, -79, 'WATER_COLD', 287.211, 0),
|
('2022-08-11 11:02:55', 20, -65, 'WATER_HOT', 158.794, 0),
|
('2022-08-11 11:07:57', 20, -67, 'WATER_COLD', 287.474, 0),
|
('2022-08-11 11:08:05', 20, -65, 'WATER_HOT', 158.795, 0),
|
('2022-08-11 11:13:04', 20, -65, 'WATER_COLD', 287.474, 0),
|
('2022-08-11 23:59:16', 20, -71, 'WATER_COLD', 287.641, 0);
|
|
|
with
|
twater_daily_raw as (
|
select date_format(`timestamp`, '%y-%m-%d') 'date',
|
extract(year from `timestamp`) 'year',
|
extract(month from `timestamp`) 'month',
|
extract(day from `timestamp`) 'day',
|
`flat`, `type`,
|
(select `value` from test2 sw where sw.`timestamp` = max(w.`timestamp`) and sw.`flat` = w.`flat` and sw.`type` = w.`type`) 'total',
|
max(timestamp) 'last'
|
from `test2` w
|
group by `date`, `flat`, `type`
|
order by `date`, `flat`, `type`
|
),
|
|
twater_daily_usage as (
|
select *,
|
(`total` - ifnull(lag(`total`, 1) over (partition by `type`, `flat` order by `type`, `flat`, `date`),0)) 'usage'
|
from `twater_daily_raw`
|
order by `date`, `flat`, `type`
|
),
|
|
water_daily_combined as (
|
select wh.`usage` 'hot',
|
wh.total 'hot_total'
|
from twater_daily_usage wc, twater_daily_usage wh
|
where wc.`date` = wh.`date` and wc.`flat` = wh.`flat` and wh.`type` = 'water_hot' and wc.`type` = 'water_cold'
|
|
)
|
|
select * from water_daily_combined wdc;
|
MariaDB [test]> CREATE TABLE `test2` (
|
-> `timestamp` timestamp NOT NULL DEFAULT current_timestamp(),
|
-> `flat` tinyint(4) unsigned NOT NULL,
|
-> `rssi` tinyint(4) NOT NULL,
|
-> `type` enum('WATER_COLD','WATER_HOT') NOT NULL,
|
-> `value` decimal(9,3) unsigned NOT NULL,
|
-> `error_flags` smallint(5) unsigned NOT NULL,
|
-> PRIMARY KEY (`timestamp`,`flat`,`type`)
|
-> ) ENGINE=Aria;
|
Query OK, 0 rows affected (0.044 sec)
|
|
MariaDB [test]> INSERT INTO `test2` VALUES
|
-> ('2022-08-10 00:02:42', 20, -77, 'WATER_COLD', 287.211, 0),
|
-> ('2022-08-10 00:04:30', 20, -68, 'WATER_HOT', 158.653, 0),
|
-> ('2022-08-10 00:07:44', 20, -79, 'WATER_COLD', 287.211, 0),
|
-> ('2022-08-10 00:09:28', 20, -68, 'WATER_HOT', 158.653, 0),
|
-> ('2022-08-10 00:12:45', 20, -79, 'WATER_COLD', 287.211, 0),
|
-> ('2022-08-11 11:02:55', 20, -65, 'WATER_HOT', 158.794, 0),
|
-> ('2022-08-11 11:07:57', 20, -67, 'WATER_COLD', 287.474, 0),
|
-> ('2022-08-11 11:08:05', 20, -65, 'WATER_HOT', 158.795, 0),
|
-> ('2022-08-11 11:13:04', 20, -65, 'WATER_COLD', 287.474, 0),
|
-> ('2022-08-11 23:59:16', 20, -71, 'WATER_COLD', 287.641, 0);
|
Query OK, 10 rows affected (0.012 sec)
|
Records: 10 Duplicates: 0 Warnings: 0
|
|
MariaDB [test]> with
|
-> twater_daily_raw as (
|
-> select date_format(`timestamp`, '%y-%m-%d') 'date',
|
-> extract(year from `timestamp`) 'year',
|
-> extract(month from `timestamp`) 'month',
|
-> extract(day from `timestamp`) 'day',
|
-> `flat`, `type`,
|
-> (select `value` from test2 sw where sw.`timestamp` = max(w.`timestamp`) and sw.`flat` = w.`flat` and sw.`type` = w.`type`) 'total',
|
-> max(timestamp) 'last'
|
-> from `test2` w
|
-> group by `date`, `flat`, `type`
|
-> order by `date`, `flat`, `type`
|
-> ),
|
->
|
-> twater_daily_usage as (
|
-> select *,
|
-> (`total` - ifnull(lag(`total`, 1) over (partition by `type`, `flat` order by `type`, `flat`, `date`),0)) 'usage'
|
-> from `twater_daily_raw`
|
-> order by `date`, `flat`, `type`
|
-> ),
|
->
|
-> water_daily_combined as (
|
-> select wh.`usage` 'hot',
|
-> wh.total 'hot_total'
|
-> from twater_daily_usage wc, twater_daily_usage wh
|
-> where wc.`date` = wh.`date` and wc.`flat` = wh.`flat` and wh.`type` = 'water_hot' and wc.`type` = 'water_cold'
|
->
|
-> )
|
->
|
-> select * from water_daily_combined wdc;
|
+------+-----------+
|
| hot | hot_total |
|
+------+-----------+
|
| NULL | NULL |
|
| NULL | NULL |
|
+------+-----------+
|
2 rows in set (0.011 sec)
|
|
MariaDB [test]> explain extended with twater_daily_raw as ( select date_format(`timestamp`, '%y-%m-%d') 'date', extract(year from `timestamp`) 'year', extract(month from `timestamp`) 'month', extract(day from `timestamp`) 'day', `flat`, `type`, (select `value` from test2 sw where sw.`timestamp` = max(w.`timestamp`) and sw.`flat` = w.`flat` and sw.`type` = w.`type`) 'total', max(timestamp) 'last' from `test2` w group by `date`, `flat`, `type` order by `date`, `flat`, `type` ), twater_daily_usage as ( select *, (`total` - ifnull(lag(`total`, 1) over (partition by `type`, `flat` order by `type`, `flat`, `date`),0)) 'usage' from `twater_daily_raw` order by `date`, `flat`, `type` ), water_daily_combined as ( select wh.`usage` 'hot', wh.total 'hot_total' from twater_daily_usage wc, twater_daily_usage wh where wc.`date` = wh.`date` and wc.`flat` = wh.`flat` and wh.`type` = 'water_hot' and wc.`type` = 'water_cold' ) select * from water_daily_combined wdc;
|
+------+--------------------+------------+--------+---------------+---------+---------+------------------------------+------+----------+-----------------------------------------------------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
|
+------+--------------------+------------+--------+---------------+---------+---------+------------------------------+------+----------+-----------------------------------------------------------+
|
| 1 | PRIMARY | <derived4> | ALL | NULL | NULL | NULL | NULL | 10 | 100.00 | Using where |
|
| 1 | PRIMARY | <derived6> | ref | key0 | key0 | 12 | wc.date,wc.flat | 2 | 100.00 | Using where |
|
| 6 | DERIVED | <derived7> | ALL | NULL | NULL | NULL | NULL | 10 | 100.00 | Using where; Using temporary; Using filesort |
|
| 7 | DERIVED | w | index | NULL | PRIMARY | 6 | NULL | 10 | 100.00 | Using where; Using index; Using temporary; Using filesort |
|
| 8 | DEPENDENT SUBQUERY | sw | eq_ref | PRIMARY | PRIMARY | 6 | func,test.w.flat,test.w.type | 1 | 100.00 | Using index condition |
|
| 4 | DERIVED | <derived2> | ALL | NULL | NULL | NULL | NULL | 10 | 100.00 | Using where; Using temporary; Using filesort |
|
| 2 | DERIVED | w | index | NULL | PRIMARY | 6 | NULL | 10 | 100.00 | Using where; Using index; Using temporary; Using filesort |
|
| 3 | DEPENDENT SUBQUERY | sw | eq_ref | PRIMARY | PRIMARY | 6 | func,func,func | 1 | 100.00 | Using index condition |
|
+------+--------------------+------------+--------+---------------+---------+---------+------------------------------+------+----------+-----------------------------------------------------------+
|
8 rows in set, 9 warnings (0.001 sec)
|
|
Note (Code 1276): Field or reference 'test.w.timestamp' of SELECT #3 was resolved in SELECT #2
|
Note (Code 1981): Aggregate function 'max()' of SELECT #3 belongs to SELECT #2
|
Note (Code 1276): Field or reference 'test.w.flat' of SELECT #3 was resolved in SELECT #2
|
Note (Code 1276): Field or reference 'test.w.type' of SELECT #3 was resolved in SELECT #2
|
Note (Code 1276): Field or reference 'test.w.timestamp' of SELECT #8 was resolved in SELECT #7
|
Note (Code 1981): Aggregate function 'max()' of SELECT #8 belongs to SELECT #7
|
Note (Code 1276): Field or reference 'test.w.flat' of SELECT #8 was resolved in SELECT #7
|
Note (Code 1276): Field or reference 'test.w.type' of SELECT #8 was resolved in SELECT #7
|
Note (Code 1003): with twater_daily_raw as (/* select#2 */ select date_format(`test`.`w`.`timestamp`,'%y-%m-%d') AS `date`,extract(year from `test`.`w`.`timestamp`) AS `year`,extract(month from `test`.`w`.`timestamp`) AS `month`,extract(day from `test`.`w`.`timestamp`) AS `day`,`test`.`w`.`flat` AS `flat`,`test`.`w`.`type` AS `type`,<expr_cache><`test`.`w`.`timestamp`,max(`test`.`w`.`timestamp`),max(`test`.`w`.`timestamp`),`test`.`w`.`flat`,`test`.`w`.`type`>((/* select#3 */ select `test`.`sw`.`value` from `test`.`test2` `sw` where `test`.`sw`.`timestamp` = max(`test`.`w`.`timestamp`) and `test`.`sw`.`flat` = `test`.`w`.`flat` and `test`.`sw`.`type` = `test`.`w`.`type`)) AS `total`,max(`test`.`w`.`timestamp`) AS `last` from `test`.`test2` `w` where `test`.`w`.`type` = 'water_cold' group by date_format(`test`.`w`.`timestamp`,'%y-%m-%d'),`test`.`w`.`flat` order by date_format(`test`.`w`.`timestamp`,'%y-%m-%d'),`test`.`w`.`flat`), twater_daily_usage as (/* select#4 */ select `twater_daily_raw`.`date` AS `date`,`twater_daily_raw`.`year` AS `year`,`twater_daily_raw`.`month` AS `month`,`twater_daily_raw`.`day` AS `day`,`twater_daily_raw`.`flat` AS `flat`,`twater_daily_raw`.`type` AS `type`,`twater_daily_raw`.`total` AS `total`,`twater_daily_raw`.`last` AS `last`,`twater_daily_raw`.`total` - ifnull(lag(`twater_daily_raw`.`total`,1) over ( partition by `twater_daily_raw`.`type`,`twater_daily_raw`.`flat` order by `twater_daily_raw`.`type`,`twater_daily_raw`.`flat`,`twater_daily_raw`.`date`),0) AS `usage` from `twater_daily_raw` where `twater_daily_raw`.`type` = 'water_cold' order by `twater_daily_raw`.`date`,`twater_daily_raw`.`flat`), water_daily_combined as (/* select#5 */ select `wh`.`usage` AS `hot`,`wh`.`total` AS `hot_total` from `twater_daily_usage` `wc` join `twater_daily_usage` `wh` where `wh`.`date` = `wc`.`date` and `wh`.`flat` = `wc`.`flat` and `wh`.`type` = 'water_hot' and `wc`.`type` = 'water_cold')/* select#1 */ select `wh`.`usage` AS `hot`,`wh`.`total` AS `hot_total` from `twater_daily_usage` `wc` join `twater_daily_usage` `wh` where `wh`.`date` = `wc`.`date` and `wh`.`flat` = `wc`.`flat` and `wh`.`type` = 'water_hot' and `wc`.`type` = 'water_cold'
|
|
MariaDB [test]> alter table test2 drop index `primary`;
|
Query OK, 10 rows affected (0.125 sec)
|
Records: 10 Duplicates: 0 Warnings: 0
|
|
MariaDB [test]> with twater_daily_raw as ( select date_format(`timestamp`, '%y-%m-%d') 'date', extract(year from `timestamp`) 'year', extract(month from `timestamp`) 'month', extract(day from `timestamp`) 'day', `flat`, `type`, (select `value` from test2 sw where sw.`timestamp` = max(w.`timestamp`) and sw.`flat` = w.`flat` and sw.`type` = w.`type`) 'total', max(timestamp) 'last' from `test2` w group by `date`, `flat`, `type` order by `date`, `flat`, `type` ), twater_daily_usage as ( select *, (`total` - ifnull(lag(`total`, 1) over (partition by `type`, `flat` order by `type`, `flat`, `date`),0)) 'usage' from `twater_daily_raw` order by `date`, `flat`, `type` ), water_daily_combined as ( select wh.`usage` 'hot', wh.total 'hot_total' from twater_daily_usage wc, twater_daily_usage wh where wc.`date` = wh.`date` and wc.`flat` = wh.`flat` and wh.`type` = 'water_hot' and wc.`type` = 'water_cold' ) select * from water_daily_combined wdc;
|
+---------+-----------+
|
| hot | hot_total |
|
+---------+-----------+
|
| 158.653 | 158.653 |
|
| 0.142 | 158.795 |
|
+---------+-----------+
|
2 rows in set (0.001 sec)
|
|
|
MariaDB [test]> explain extended with twater_daily_raw as ( select date_format(`timestamp`, '%y-%m-%d') 'date', extract(year from `timestamp`) 'year', extract(month from `timestamp`) 'month', extract(day from `timestamp`) 'day', `flat`, `type`, (select `value` from test2 sw where sw.`timestamp` = max(w.`timestamp`) and sw.`flat` = w.`flat` and sw.`type` = w.`type`) 'total', max(timestamp) 'last' from `test2` w group by `date`, `flat`, `type` order by `date`, `flat`, `type` ), twater_daily_usage as ( select *, (`total` - ifnull(lag(`total`, 1) over (partition by `type`, `flat` order by `type`, `flat`, `date`),0)) 'usage' from `twater_daily_raw` order by `date`, `flat`, `type` ), water_daily_combined as ( select wh.`usage` 'hot', wh.total 'hot_total' from twater_daily_usage wc, twater_daily_usage wh where wc.`date` = wh.`date` and wc.`flat` = wh.`flat` and wh.`type` = 'water_hot' and wc.`type` = 'water_cold' ) select * from water_daily_combined wdc;
|
+------+--------------------+------------+------+---------------+------+---------+-----------------+------+----------+----------------------------------------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
|
+------+--------------------+------------+------+---------------+------+---------+-----------------+------+----------+----------------------------------------------+
|
| 1 | PRIMARY | <derived4> | ALL | NULL | NULL | NULL | NULL | 10 | 100.00 | Using where |
|
| 1 | PRIMARY | <derived6> | ref | key0 | key0 | 12 | wc.date,wc.flat | 2 | 100.00 | Using where |
|
| 6 | DERIVED | <derived7> | ALL | NULL | NULL | NULL | NULL | 10 | 100.00 | Using where; Using temporary; Using filesort |
|
| 7 | DERIVED | w | ALL | NULL | NULL | NULL | NULL | 10 | 100.00 | Using where; Using temporary; Using filesort |
|
| 8 | DEPENDENT SUBQUERY | sw | ALL | NULL | NULL | NULL | NULL | 10 | 100.00 | Using where |
|
| 4 | DERIVED | <derived2> | ALL | NULL | NULL | NULL | NULL | 10 | 100.00 | Using where; Using temporary; Using filesort |
|
| 2 | DERIVED | w | ALL | NULL | NULL | NULL | NULL | 10 | 100.00 | Using where; Using temporary; Using filesort |
|
| 3 | DEPENDENT SUBQUERY | sw | ALL | NULL | NULL | NULL | NULL | 10 | 100.00 | Using where |
|
+------+--------------------+------------+------+---------------+------+---------+-----------------+------+----------+----------------------------------------------+
|
8 rows in set, 9 warnings (0.001 sec)
|
|
Note (Code 1276): Field or reference 'test.w.timestamp' of SELECT #3 was resolved in SELECT #2
|
Note (Code 1981): Aggregate function 'max()' of SELECT #3 belongs to SELECT #2
|
Note (Code 1276): Field or reference 'test.w.flat' of SELECT #3 was resolved in SELECT #2
|
Note (Code 1276): Field or reference 'test.w.type' of SELECT #3 was resolved in SELECT #2
|
Note (Code 1276): Field or reference 'test.w.timestamp' of SELECT #8 was resolved in SELECT #7
|
Note (Code 1981): Aggregate function 'max()' of SELECT #8 belongs to SELECT #7
|
Note (Code 1276): Field or reference 'test.w.flat' of SELECT #8 was resolved in SELECT #7
|
Note (Code 1276): Field or reference 'test.w.type' of SELECT #8 was resolved in SELECT #7
|
Note (Code 1003): with twater_daily_raw as (/* select#2 */ select date_format(`test`.`w`.`timestamp`,'%y-%m-%d') AS `date`,extract(year from `test`.`w`.`timestamp`) AS `year`,extract(month from `test`.`w`.`timestamp`) AS `month`,extract(day from `test`.`w`.`timestamp`) AS `day`,`test`.`w`.`flat` AS `flat`,`test`.`w`.`type` AS `type`,<expr_cache><`test`.`w`.`timestamp`,max(`test`.`w`.`timestamp`),max(`test`.`w`.`timestamp`),`test`.`w`.`flat`,`test`.`w`.`type`>((/* select#3 */ select `test`.`sw`.`value` from `test`.`test2` `sw` where `test`.`sw`.`timestamp` = max(`test`.`w`.`timestamp`) and `test`.`sw`.`flat` = `test`.`w`.`flat` and `test`.`sw`.`type` = `test`.`w`.`type`)) AS `total`,max(`test`.`w`.`timestamp`) AS `last` from `test`.`test2` `w` where `test`.`w`.`type` = 'water_cold' group by date_format(`test`.`w`.`timestamp`,'%y-%m-%d'),`test`.`w`.`flat` order by date_format(`test`.`w`.`timestamp`,'%y-%m-%d'),`test`.`w`.`flat`), twater_daily_usage as (/* select#4 */ select `twater_daily_raw`.`date` AS `date`,`twater_daily_raw`.`year` AS `year`,`twater_daily_raw`.`month` AS `month`,`twater_daily_raw`.`day` AS `day`,`twater_daily_raw`.`flat` AS `flat`,`twater_daily_raw`.`type` AS `type`,`twater_daily_raw`.`total` AS `total`,`twater_daily_raw`.`last` AS `last`,`twater_daily_raw`.`total` - ifnull(lag(`twater_daily_raw`.`total`,1) over ( partition by `twater_daily_raw`.`type`,`twater_daily_raw`.`flat` order by `twater_daily_raw`.`type`,`twater_daily_raw`.`flat`,`twater_daily_raw`.`date`),0) AS `usage` from `twater_daily_raw` where `twater_daily_raw`.`type` = 'water_cold' order by `twater_daily_raw`.`date`,`twater_daily_raw`.`flat`), water_daily_combined as (/* select#5 */ select `wh`.`usage` AS `hot`,`wh`.`total` AS `hot_total` from `twater_daily_usage` `wc` join `twater_daily_usage` `wh` where `wh`.`date` = `wc`.`date` and `wh`.`flat` = `wc`.`flat` and `wh`.`type` = 'water_hot' and `wc`.`type` = 'water_cold')/* select#1 */ select `wh`.`usage` AS `hot`,`wh`.`total` AS `hot_total` from `twater_daily_usage` `wc` join `twater_daily_usage` `wh` where `wh`.`date` = `wc`.`date` and `wh`.`flat` = `wc`.`flat` and `wh`.`type` = 'water_hot' and `wc`.`type` = 'water_cold'
|
|