Details
-
Bug
-
Status: Closed (View Workflow)
-
Major
-
Resolution: Fixed
-
10.2(EOL), 10.3(EOL), 10.4(EOL)
-
None
Description
CREATE TABLE tt (i int) ; |
INSERT INTO `tt` VALUES (1),(0),(1),(2),(0),(NULL),(1),(2),(NULL),(1),(2); |
|
SELECT DISTINCT COUNT(*) OVER () FROM tt GROUP BY i ; |
SELECT DISTINCT COUNT(*) OVER (), MOD(MIN(i),2) FROM tt GROUP BY i ; |
MariaDB [test]> SELECT DISTINCT COUNT(*) OVER () FROM tt GROUP BY i ;
|
+------------------+
|
| COUNT(*) OVER () |
|
+------------------+
|
| 4 |
|
+------------------+
|
1 row in set (0.001 sec)
|
|
MariaDB [test]> SELECT DISTINCT COUNT(*) OVER (), MOD(MIN(i),2) FROM tt GROUP BY i ;
|
+------------------+---------------+
|
| COUNT(*) OVER () | MOD(MIN(i),2) |
|
+------------------+---------------+
|
| 0 | NULL |
|
| 0 | 0 |
|
| 0 | 1 |
|
+------------------+---------------+
|
3 rows in set (0.001 sec)
|
in Mysql 8.0.11
mysql> SELECT DISTINCT COUNT(*) OVER (), MOD(MIN(i),2) FROM tt GROUP BY i ;
|
+------------------+---------------+
|
| COUNT(*) OVER () | MOD(MIN(i),2) |
|
+------------------+---------------+
|
| 4 | 1 |
|
| 4 | 0 |
|
| 4 | NULL |
|
+------------------+---------------+
|
3 rows in set (0.00 sec)
|
The query is: SELECT DISTINCT COUNT(*) OVER (), MOD(MIN(i),2) FROM tt GROUP BY i ;
This query requires two temp tables for execution, lets say these are tmp_table_1 and tmp_table_2
tmp_table_1 fields => i, MIN(i), COUNT(*) OVER ()
tmp_table_2 fields => i, MIN(i), COUNT(*) OVER (), MOD(MIN(i),2)
So the execution is like
1)Read data from table t1
a) Perform grouping on the fly
b) update MIN(i)
c) write the new value to temporary table tmp_table1
2) Read the records from temp table tmp_table1
3) Evaluate MOD(MIN(i),2) and write the new record in the tmp_table2
4) After all records are written, then compute the window function COUNT(*) OVER () on tmp_table2
5) Perform Distinct on tmp_table2