hopefully somebody can help me with this query. I need to list all 200 records from ngr_titles below, joined with their most recent related record from ngr_monitordata. The foreign key in ngr_monitordata is called titleid and the most recent should be the higher when_posted value for each title.
The problem is that ngr_monitordata contains millions of records and is increasing every second.
This is the query I used but it takes 14 seconds to execute:
SELECT * FROM ngr_titles
LEFT JOIN
(SELECT titleid, MAX(when_posted) FROM ngr_monitordata GROUP BY titleid) tmp1
ON ngr_titles.id= tmp1.titleid;
What would be the best approach?
Tables are:
CREATE TABLE `ngr_titles` (
`id` int(11) NOT NULL auto_increment,
`title` varchar(255) default NULL,
`drop_min` int(11) NOT NULL default '50',
`warn_pcnt` float(5,1) NOT NULL default '15.0',
`crit_pcnt` float(5,1) NOT NULL default '25.0',
`deviations` float(5,1) NOT NULL default '2.0',
`addlobby` int(2) NOT NULL default '0',
`url` varchar(255) default NULL,
`graph_url` varchar(255) default NULL,
`lastchecked` int(11) NOT NULL default '0',
`last_exception` int(11) default NULL,
`state` enum('ok','warning','critical') default NULL,
`trend` int(11) NOT NULL default '0',
`short_name` varchar(45) NOT NULL,
`historical` varchar(45) NOT NULL,
`cacti_url` varchar(255) default NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `title` (`title`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `ngr_monitordata` (
`id` int(11) NOT NULL auto_increment,
`titleid` int(11) default NULL,
`when_posted` int(11) default NULL,
`games_completed` int(11) default NULL,
`games_created` int(11) default NULL,
`games_in_progress` int(11) default NULL,
`games_in_progress_gps` int(11) default NULL,
`users_in_games` int(11) default NULL,
`users_in_games_gps` int(11) default NULL,
`users_in_lobby` int(11) default NULL,
`users_in_passive` int(11) default NULL,
`users_in_rooms` int(11) default NULL,
`rooms` int(11) default NULL,
`state` enum('ok','warning','critical','unknown') NOT NULL default 'ok',
`user_drop` int(11) NOT NULL default '0',
`retries` int(8) default NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `whowhenidx` (`titleid`,`when_posted`),
KEY `whenidx` (`when_posted`),
KEY `titleidx` (`titleid`),
KEY `stateidx` (`state`)
) ENGINE=MyISAM AUTO_INCREMENT=120512615 DEFAULT CHARSET=utf8;
Quick solution, it should work although I wrote it in haste. If it were me, I’d normalize the data slightly to reduce the overhead from aggregate function and subquery.