This join makes me sad.
SELECT
w.name AS worker,
w.id AS worker_id,
worked.pool_name AS active_pool,
worked.latest - INTERVAL 5 HOUR AS active_time,
submitted.pool_name AS last_accepted_pool,
submitted.latest - INTERVAL 5 HOUR AS last_accepted_time,
w.last_reboot AS last_reboot
FROM worker w
LEFT OUTER JOIN (
SELECT
wd.worker_id AS worker_id,
wd.time_requested AS latest,
p.name AS pool_name
FROM work_data wd
INNER JOIN (
SELECT
worker_id,
MAX(time_requested) AS latest
FROM work_data
GROUP BY worker_id
) wd2
ON wd.worker_id = wd2.worker_id
AND wd.time_requested = wd2.latest
INNER JOIN pool p
ON p.id = wd.pool_id
GROUP BY wd.worker_id
) worked
ON worked.worker_id = w.id
LEFT OUTER JOIN (
SELECT
sw.worker_id AS worker_id,
sw.time AS latest,
p.name AS pool_name
FROM submitted_work sw
INNER JOIN (
SELECT
worker_id,
MAX(time) AS latest
FROM submitted_work
WHERE result = 1
GROUP BY worker_id
) sw2
ON sw.worker_id = sw2.worker_id
AND sw.result = 1
AND sw.time = sw2.latest
INNER JOIN pool p
ON p.id = sw.pool_id
GROUP BY sw.worker_id
) submitted
ON submitted.worker_id = w.id
ORDER BY w.name
Solution?
ALTER TABLE work_data DROP PRIMARY KEY;
ALTER TABLE work_data ADD id INT NOT NULL PRIMARY KEY AUTO_INCREMENT FIRST;
ALTER TABLE work_data ADD KEY (`worker_id`, `data`);
CREATE TABLE `work_data_history` (
`id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
`worker_id` int(11) NOT NULL,
`pool_id` int(11) NOT NULL,
`data` char(152) character set ascii collate ascii_bin NOT NULL,
`time_requested` datetime NOT NULL,
KEY (`worker_id`,`data`),
KEY `worker_time` (`worker_id`,`time_requested`)
);
CREATE TABLE `submitted_work_history` (
`id` int(11) NOT NULL auto_increment,
`worker_id` int(11) NOT NULL,
`pool_id` int(11) NOT NULL,
`result` tinyint(1) NOT NULL,
`time` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `dashboard_status_index` (`worker_id`,`result`,`time`)
);
$con=mysql_connect("__DB_HOST__","__DB_USERNAME__","__DB_PASSWORD__");
mysql_select_db("__DB_DATABASE__",$con);
$q="SELECT MAX(id) AS mid FROM work_data_history";
$r=mysql_query($q);
if(mysql_errno()!=0)echo mysql_error()."\n";
$row=mysql_fetch_assoc($r);
$mid=$row['mid'];
$q="INSERT INTO work_data_history SELECT * FROM work_data WHERE id>".$mid;
mysql_query($q);
if(mysql_errno()!=0)echo mysql_error()."\n";
$q="DELETE FROM work_data WHERE time_requested < (NOW() - INTERVAL 12 HOUR)";
mysql_query($q);
if(mysql_errno()!=0)echo mysql_error()."\n";
$q="SELECT MAX(id) AS mid FROM submitted_work_history";
$r=mysql_query($q);
if(mysql_errno()!=0)echo mysql_error()."\n";
$row=mysql_fetch_assoc($r);
$mid=$row['mid'];
$q="INSERT INTO submitted_work_history SELECT * FROM submitted_work WHERE id>".$mid;
mysql_query($q);
if(mysql_errno()!=0)echo mysql_error()."\n";
$q="DELETE FROM submitted_work WHERE time < (NOW() - INTERVAL 12 HOUR)";
mysql_query($q);
if(mysql_errno()!=0)echo mysql_error()."\n";
?>