BLE*_*tor 5 mysql sql optimization
我有一个SQL请求,它在工作时占用了我100%的VM CPU.我想知道如何优化它:
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
Run Code Online (Sandbox Code Playgroud)
问题出在这一部分:
(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
Run Code Online (Sandbox Code Playgroud)
表日志真的很大,一些朋友告诉我使用缓冲表/数据库,但我很清楚这些事情,我不知道该怎么做.
有一个EXPLAIN EXTENDED查询: 
看来他只会检查2行表日志,为什么需要这么多时间呢?(表日志中有560000行).
以下是这些表的所有索引:
centstorage.acknowledgements:
centstorage.hosts:
centstorage.services:
centstorage.hosts_hostgroups:
centstorage.hostgroups:
centreon.hostgroup_relation:
centreon.acl_resources_hg_relations:
centreon.acl_resources:

因为SQL Server可以使用以下命令定义查询的最大并行度MAXDOP
例如,您可以在查询末尾定义
option (maxdop 2)
Run Code Online (Sandbox Code Playgroud)
我很确定 中有一个等效的东西MySql。
如果执行时间不相关,您可以尝试处理这种情况。
| 归档时间: |
|
| 查看次数: |
331 次 |
| 最近记录: |