Ich habe eine SQL-Anfrage, die 100% meiner VM-CPU übernehmen, während es funktioniert. Ich möchte wissen, wie es zu optimieren:SQL-Anfrage Optimierung
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 g.hostgroup_id = p.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
Das Problem ist in diesem Teil:
(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
Die Tabellenprotokolle sind wirklich riesig und einige Freunde sagten mir, eine Puffertabelle/Datenbank zu verwenden, aber ich ziemlich wusste diese Dinge und ich weiß nicht, wie es geht.
Es wird eine EXPLAIN der Abfrage VERLÄNGERT:
Es scheint, dass er nur 2 Reihe der Tabellenprotokolle untersucht werden, so, warum es so viel Zeit in Anspruch nimmt? (In den Tabellenprotokollen befindet sich eine 560000 Zeile). Hier
ist alle Indizes dieser Tabellen:
centstorage.acknowledgements:
centstorage.hosts:
centstorage.services:
centstorage.hosts_hostgroups:
centstorage.hostgroups:
centreon.hostgroup_relation:
centreon.acl_resources_hg_relations:
centreon.acl_resources:
Welches dbms-Produkt? Tabellen- und Indexdefinitionen usw. – jarlh
Überprüfen Sie zuerst den Ausführungsplan und vergewissern Sie sich, dass Ihnen ein Index fehlt. – Galma88
Ich benutze MySQL. –