Ho una richiesta SQL che prende il 100% della mia CPU VM mentre funziona. Voglio sapere come ottimizzare esso:Ottimizzazione richiesta SQL
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
Il problema è in questa parte:
(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
I registri della tabella è davvero enorme e alcuni amici mi ha detto di utilizzare un buffer tavolo/base di dati ma ho abbastanza sapevo a queste cose e non so come farlo.
C'è un spiegare estesa del query:
Sembra che sarà esaminato solo il 2 fila dei registri della tabella e allora perché ci vuole così tanto tempo? (C'è una riga 560000 nei registri della tabella).
Ecco tutti gli indici di tali tabelle:
centstorage.acknowledgements:
centstorage.hosts:
centstorage.services:
centstorage.hosts_hostgroups:
centstorage.hostgroups:
centreon.hostgroup_relation:
centreon.acl_resources_hg_relations:
centreon.acl_resources:
Quale prodotto dbms? Definizioni di tabelle e indici, ecc. – jarlh
Prima di tutto controlla il piano di esecuzione e verifica se ti manca un indice. – Galma88
Sto usando MySQL. –