In case you have your own slow SQL query, you can optimize it automatically here.
For the query above, the following recommendations will be helpful as part of the SQL tuning process.
You'll find 3 sections below:
ALTER TABLE `es_temp1` ADD INDEX `es_temp1_idx_visitor_id` (`visitor_id`);
ALTER TABLE `hits` ADD INDEX `hits_idx_visitor_id_id` (`visitor_id`,`id`);
ALTER TABLE `hits` ADD INDEX `hits_idx_id` (`id`);
ALTER TABLE `hits` ADD INDEX `hits_idx_visitor_id_id_2` (`visitor_id`,`id`);
SELECT
visitors.id,
visitors.ip,
visitors.hash,
(SELECT
hits.time
FROM
hits
WHERE
hits.visitor_id = visitors.id
ORDER BY
hits.id ASC LIMIT 1) AS first_hit,
(SELECT
hits.time
FROM
hits
WHERE
hits.visitor_id = visitors.id
ORDER BY
hits.id DESC LIMIT 1) AS last_hit,
(SELECT
hits.host
FROM
hits
WHERE
hits.visitor_id = visitors.id
ORDER BY
hits.id DESC LIMIT 1) AS last_host,
(SELECT
hits.location
FROM
hits
WHERE
hits.visitor_id = visitors.id
ORDER BY
hits.id DESC LIMIT 1) AS last_location,
es_temp1.total_hits,
(SELECT
strftime('%s',
'now') - hits.time
FROM
hits
WHERE
hits.visitor_id = visitors.id
ORDER BY
hits.id DESC LIMIT 1) AS idle_since
FROM
visitors
LEFT JOIN
es_temp1
ON es_temp1.visitor_id = visitors.id
WHERE
idle_since < 30
ORDER BY
last_hit DESC