I've a single query to get 10 Maximum value from MySQL database. It's returned a correct value, but the problem is, it took long time to get the value
This value is CPU data from a device. So, from a single device have about 6 Module Type. From each Module Type have about 120 Module Number. From each Module Number have 2 Slot (active and standby). Module Number places in a subrack. I need to get 10 Module Number which has highest value.
I've tried using my own query, it returned correct value, but the slot is incorrect. Then I found a query from stack overflow (MySQL query, MAX() + GROUP BY)
This is my table structure :
Create Table: CREATE TABLE `Router_Modul_CPU` (
`date_id` date NOT NULL,
`hour_id` time NOT NULL,
`NE` varchar(50) NOT NULL,
`modul_number` int(11) NOT NULL,
`modul_type` varchar(50) NOT NULL,
`slot` int(11) NOT NULL,
`subrack` int(11) DEFAULT NULL,
`mean_memory` float DEFAULT NULL,
`peak_memory` float DEFAULT NULL,
PRIMARY KEY (`date_id`,`hour_id`,`NE`,`modul_number`,`modul_type`,`slot`),
KEY `index_key` (`date_id`,`hour_id`,`NE`,`modul_number`,`modul_type`,`slot`,`subrack`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
This is my query :
select
Router_Modul_CPU.NE,
Router_Modul_CPU.modul_number,
Router_Modul_CPU.slot,
Router_Modul_CPU.subrack
from Router_Modul_CPU
inner join
(
select modul_number, max(peak_memory) as maks
from `Router_Modul_CPU`
group by modul_number, NE, subrack
) maxt on
(Router_Modul_CPU.modul_number = maxt.modul_number and
Router_Modul_CPU.peak_memory = maxt.maks)
where modul_type='SPU' and NE='R-D5-SBT' and date_id='2019-02-14'
limit 10
It took about 40-50 seconds to get the data, and those query only for one device
All this data were inserted every 5 minutes to this table. Now this table have about 25 million row.
Is there any tuning to my table or any suggestion for my query to make more efficient?
Thanks