I accidentally reformatted the query for my readability reference and better saw the relationship between the tables ... otherwise I will ignore this part.
SELECT g.name AS hostgroup, h.name AS hostname, a.host_id, s.display_name AS servicename, a.service_id, a.entry_time AS ack_time, ( SELECT ctime FROM logs WHERE logs.host_id = a.host_id AND logs.service_id = a.service_id AND logs.ctime < a.entry_time AND logs.status IN (1, 2, 3) AND logs.type = 1 ORDER BY logs.log_id DESC LIMIT 1) AS start_time, ar.acl_res_name AS timeperiod, a.state AS state, a.author, a.acknowledgement_id AS ack_id FROM centstorage.acknowledgements a LEFT JOIN centstorage.hosts h ON a.host_id = h.host_id LEFT JOIN centstorage.services s ON a.service_id = s.service_id LEFT JOIN centstorage.hosts_hostgroups p ON a.host_id = p.host_id LEFT JOIN centstorage.hostgroups g ON p.hostgroup_id = g.hostgroup_id LEFT JOIN centreon.hostgroup_relation hg ON a.host_id = hg.host_host_id LEFT JOIN centreon.acl_resources_hg_relations hh ON hg.hostgroup_hg_id = hh.hg_hg_id LEFT JOIN centreon.acl_resources ar ON hh.acl_res_id = ar.acl_res_id WHERE ar.acl_res_name != 'All Resources' AND YEAR(FROM_UNIXTIME( a.entry_time )) = YEAR(CURDATE()) AND MONTH(FROM_UNIXTIME( a.entry_time )) = MONTH(CURDATE()) AND a.service_id is not null ORDER BY a.acknowledgement_id ASC
First, I recommend starting with your βconfirmationβ table and having an index at least (entry_time, confirmment_id). Then update the WHERE clause. Since you use the function to convert the unix timestamp to a date and capture YEAR (and month), respectively, I donβt think it uses an index because it has to calculate this for each row. To enhance this, the unix timestamp is nothing more than a number representing seconds from a specific point in time. If you are looking for a specific month, then pre-calculate the start and end times of unix and run for this range. Sort of...
and a.entry_time> = UNIX_TIMESTAMP ('2015-10-01') and a.entry_time <UNIX_TIMESTAMP ('2015-11-01')
Thus, it is all seconds for a month until 11:59:59 on October 31, shortly before November 1.
Then, without glasses, to see all the images more clearly and for a shorter time this morning, I would ensure that you have at least the following indexes on each table, respectively
table index logs ( host_id, service_id, type, status, ctime, log_id ) acknowledgements ( entry_time, acknowledgement_id, host_id, service_id ) hosts ( host_id, name ) services ( service_id, display_name ) hosts_hostgroups ( host_id, hostgroup_id ) hostgroups ( hostgroup_id, name ) hostgroup_relation ( host_host_id, hostgroup_hg_id ) acl_resources_hg_relations ( hh_hg_id, acl_res_id ) acl_resources ar ( acl_res_id, acl_res_name )
Finally, your correlated subquery field will be a killer as it is processed for each row, but hopefully other ideas for optimizing the index will help performance.