Mysql implements rownum and mysqlrownum
Create table 'total _ freq_ctrl '(
- 'Time' int (10) unsigned not null,
- 'Machine 'char (64) not null,
- 'Module' char (32) not null,
- 'Total _ flow' int (10) unsigned not null,
- 'Deny _ flow' int (10) unsigned not null,
- Primary key ('module ', 'machine', 'time ')
- ) ENGINE = InnoDB default charset = utf8
Original SQL
- SELECT machine, deny_flow, total_flow, time FROM total_freq_ctrl a where 1> (select count (machine) FROM total_freq_ctrl WHERE machine =. machine AND time>. time) and. module = 'all' order by. time desc;
Change 1 to N to get the first N records of each group. Because I do not like subqueries very much, I will try to rename the join method.
However, we need to sort all the data to determine the first N entries in each group. Therefore, we need to perform a full table scan for the Best optimization.
First, I want to sort the data in the table and introduce a variable @ row for rownumber.
- Set @ row = 0; set @ mid = ''; SELECT module, machine, time, @ row: = @ row + 1 rownum FROM total_freq_ctrl order by module, machine, time desc limit 10;
- Query OK, 0 rows affected (0.00 sec)
- Query OK, 0 rows affected (0.00 sec)
- + -------- + --------------- + ------------ + -------- +
- | Module | machine | time | rownum |
- + -------- + --------------- + ------------ + -------- +
- | All | 10.201.20.181 | 1409640060 | 1 |
- | All | 10.201.20.181 | 1409640000 | 2 |
- | All | 10.201.20.181 | 1409639940 | 3 |
- | All | 10.201.20.181 | 1409639880 | 4 |
- | All | 10.201.000097 | 1409640060 | 5 |
- | All | 10.201.000097 | 1409640000 | 6 |
- | All | 10.201.000097 | 1409639940 | 7 |
- | All | 10.201.000097 | 1409639880 | 8 |
- | All | 10.201.000098 | 1409640060 | 9 |
- | All | 10.201.000098 | 1409640000 | 10 |
- + -------- + --------------- + ------------ + -------- +
Rownumber already exists. Add @ mid to group
- Set @ row = 0; set @ mid = ''; SELECT module, machine, time, case when @ mid = machine then @ row: = @ row + 1 else @ row: = 1 end rownum, @ mid: = machine FROM total_freq_ctrl order by module, machine, time desc limit 20;
- Query OK, 0 rows affected (0.00 sec)
- Query OK, 0 rows affected (0.00 sec)
- + -------- + --------------- + ------------ + -------- + --------------- +
- | Module | machine | time | rownum | @ mid: = machine |
- + -------- + --------------- + ------------ + -------- + --------------- +
- | All | 10.201.20.181 | 1409640180 | 1 | 10.201.20.181 |
- | All | 10.201.20.181 | 1409640120 | 2 | 10.201.20.181 |
- | All | 10.201.20.181 | 1409640060 | 3 | 10.201.20.181 |
- | All | 10.201.20.181 | 1409640000 | 4 | 10.201.20.181 |
- | All | 10.201.20.181 | 1409639940 | 5 | 10.201.20.181 |
- | All | 10.201.20.181 | 1409639880 | 6 | 10.201.20.181 |
- | All | 10.201.20.97 | 1409640180 | 1 | 10.201.000097 |
- | All | 10.201.20.97 | 1409640120 | 2 | 10.201.000097 |
- | All | 10.201.20.97 | 1409640060 | 3 | 10.201.20.97 |
- | All | 10.201.20.97 | 1409640000 | 4 | 10.201.000097 |
- | All | 10.201.20.97 | 1409639940 | 5 | 10.201.000097 |
- | All | 10.201.20.97 | 1409639880 | 6 | 10.201.000097 |
- | All | 10.201.20.98 | 1409640180 | 1 | 10.201.000098 |
- | All | 10.201.20.98 | 1409640120 | 2 | 10.201.000098 |
- | All | 10.201.000098 | 1409640060 | 3 | 10.201.000098 |
- | All | 10.201.20.98 | 1409640000 | 4 | 10.201.000098 |
- | All | 10.201.20.98 | 1409639940 | 5 | 10.201.000098 |
- | All | 10.201.000098 | 1409639880 | 6 | 10.201.000098 |
- + -------- + --------------- + ------------ + -------- + --------------- +
Now, add an inner join to the table and then limit rownumber to get the target data.
- Set @ row = 0; set @ mid = ''; select. *, B. rownum from total_freq_ctrl a inner join (SELECT module, machine, time, case when @ mid = machine then @ row: = @ row + 1 else @ row: = 1 end rownum, @ mid: = machine mid FROM total_freq_ctrl order by module, machine, time desc) B on B. module =. module and B. machine =. machine and B. time =. time where B. rownum <5;
- Query OK, 0 rows affected (0.00 sec)
- Query OK, 0 rows affected (0.00 sec)
- + ------------ + --------------- + -------- + ------------ + ----------- + -------- +
- | Time | machine | module | total_flow | deny_flow | rownum |
- + ------------ + --------------- + -------- + ------------ + ----------- + -------- +
- | 1409640360 | 10.201.20.181 | all | 53937 | 6058 | 1 |
- | 1409640300 | 10.201.20.181 | all | 52588 | 5701 | 2 |
- | 1409640240 | 10.201.20.181 | all | 54254 | 5608 | 3 |
- | 1409640180 | 10.201.20.181 | all | 54684 | 5811 | 4 |
- | 1409640360 | 10.201.000097 | all | 50679 | 5307 | 1 |
- | 1409640300 | 10.201.000097 | all | 50472 | 5239 | 2 |
- | 1409640240 | 10.201.000097 | all | 51586 | 5509 | 3 |
- | 1409640180 | 10.201.000097 | all | 50794 | 5378 | 4 |
- | 1409640360 | 10.201.000098 | all | 84747 | 5652 | 1 |
- | 1409640300 | 10.201.000098 | all | 84506 | 5696 | 2 |
- | 1409640240 | 10.201.000098 | all | 84982 | 5513 | 3 |
- | 1409640180 | 10.201.000098 | all | 83997 | 5623 | 4 |
- + ------------ + --------------- + -------- + ------------ + ----------- + -------- +