PostgreSQL慢速查询
OpenVAS(由PostgreSQL支持)实例在打开“任务”选项卡时速度较慢 以下查询在PostgreSQL中运行22秒。有没有关于如何优化的建议PostgreSQL慢速查询,postgresql,openvas,Postgresql,Openvas,OpenVAS(由PostgreSQL支持)实例在打开“任务”选项卡时速度较慢 以下查询在PostgreSQL中运行22秒。有没有关于如何优化的建议 SELECT id, host, iso_time (start_time), iso_time (end_time), current_port, max_port, report, (SELECT uuid FROM reports WHERE id = report), (SELECT
SELECT id, host,
iso_time (start_time), iso_time (end_time),
current_port, max_port, report,
(SELECT uuid FROM reports WHERE id = report),
(SELECT uuid FROM hosts
WHERE id = (SELECT host FROM host_identifiers
WHERE source_type = 'Report Host'
AND name = 'ip'
AND source_id = (SELECT uuid FROM reports
WHERE id = report)
AND value = report_hosts.host
LIMIT 1)
)
FROM report_hosts
WHERE report = 702;
计划是
Index Scan using report_hosts_by_report on report_hosts (cost=0.42..1975570.99 rows=447 width=38) (actual time=50.042..22979.257 rows=1206 loops=1)
Index Cond: (report = 702)
SubPlan 1
-> Index Scan using reports_pkey on reports (cost=0.28..2.49 rows=1 width=37) (actual time=0.004..0.004 rows=1 loops=1206)
Index Cond: (id = report_hosts.report)
SubPlan 4
-> Index Scan using hosts_pkey on hosts (cost=4414.37..4416.59 rows=1 width=37) (actual time=0.001..0.001 rows=0 loops=1206)
Index Cond: (id = $4)
InitPlan 3 (returns $4)
-> Limit (cost=2.49..4414.09 rows=1 width=4) (actual time=18.998..18.998 rows=0 loops=1206)
InitPlan 2 (returns $2)
-> Index Scan using reports_pkey on reports reports_1 (cost=0.28..2.49 rows=1 width=37) (actual time=0.001..0.001 rows=1 loops=1206)
Index Cond: (id = report_hosts.report)
-> Seq Scan on host_identifiers (cost=0.00..4411.60 rows=1 width=4) (actual time=18.997..18.997 rows=0 loops=1206)
Filter: ((source_type = 'Report Host'::text) AND (name = 'ip'::text) AND (source_id = $2) AND (value = report_hosts.host))
Rows Removed by Filter: 99459
Planning time: 0.531 ms
Execution time: 22979.575 ms
所有时间都花在1206次主机标识符的顺序扫描上 尝试用联接替换子查询:
SELECT rh.id, rh.host,
iso_time(rh.start_time), iso_time(rh.end_time),
rh.current_port, rh.max_port, rh.report,
r.uuid,
h.uuid
FROM report_hosts AS rh
LEFT JOIN reports AS r
ON rh.report = r.id
LEFT JOIN host_identifiers AS hi
ON hi.source_id = r.uuid
AND hi.value = rh.host
AND hi.source_type = 'Report Host'
AND hi.name = 'ip'
LEFT JOIN hosts AS h
ON h.id = hi.host
WHERE rh.report = 702;
这是不完全相同的,因为它没有考虑到限制1
,如果没有的顺序,这几乎没有意义,但它应该接近事实
适当的索引将使其快速(如果它们还不存在):
- 一个在
报告(id)
- 主机标识符(源id、值)
- 主机上的一个(id)
您的查询很难读取,因为您没有用表名限定列。所有时间都花在对主机标识符进行1206次顺序扫描上 尝试用联接替换子查询:
SELECT rh.id, rh.host,
iso_time(rh.start_time), iso_time(rh.end_time),
rh.current_port, rh.max_port, rh.report,
r.uuid,
h.uuid
FROM report_hosts AS rh
LEFT JOIN reports AS r
ON rh.report = r.id
LEFT JOIN host_identifiers AS hi
ON hi.source_id = r.uuid
AND hi.value = rh.host
AND hi.source_type = 'Report Host'
AND hi.name = 'ip'
LEFT JOIN hosts AS h
ON h.id = hi.host
WHERE rh.report = 702;
这是不完全相同的,因为它没有考虑到限制1
,如果没有的顺序,这几乎没有意义,但它应该接近事实
适当的索引将使其快速(如果它们还不存在):
- 一个在
报告(id)
- 主机标识符(源id、值)
- 主机上的一个(id)
您的查询很难读取,因为您没有用表名限定列。哇!添加索引
主机标识符(源id、值)
正是我想要的:
create INDEX host_identifiers_source_id_value on host_identifiers(source_id, value);
“任务”选项卡的页面加载时间从70秒减少到仅13秒
谢谢 哇!添加索引
主机标识符(源id、值)
正是我想要的:
create INDEX host_identifiers_source_id_value on host_identifiers(source_id, value);
“任务”选项卡的页面加载时间从70秒减少到仅13秒
谢谢 标量子查询,为什么?(包括没有ORDER BY的LIMIT 1;为什么?)。还怀疑我不能更改查询本身,但可以优化数据库端(更智能的索引?)。我对这台机器(32cores、SSD、32G RAM)进行了一般postgresql调优,但它仍然很慢。在计划中,从底部开始的第三行是对主机标识符进行顺序扫描,这是否意味着它缺少索引?标量子查询,为什么?(包括没有ORDER BY的LIMIT 1;为什么?)。还怀疑我不能更改查询本身,但可以优化数据库端(更智能的索引?)。我对这台机器(32cores、SSD、32G RAM)进行了一般的postgresql调优,但它仍然很慢。在计划中,从底部开始的第三行是对主机标识符进行顺序扫描,这是否意味着它缺少索引?