【发布时间】:2021-01-20 19:28:06
【问题描述】:
因此,正如我之前提到的,我不处理通常需要帮助增强此查询的原始 SQL。目前,大约需要 3 秒。我想知道他们是否有办法让它变得更好。我删除了一些选择以使查询更小。
select row_number() over () as id,
scope.id as sow_id,
scope.*,
task.*,
po.entity_id as group_name
from t_scope_of_work as scope,
(select * from audittaskimpl) as task
Left join peopleassignments_potowners po ON task.taskid = po.task_id and po.entity_id like '%/%'
Where task.processinstanceid IN
(select distinct processinstanceid from taskvariableimpl where value = scope.sownumber)
order by task.processinstanceid desc, task.createdon desc;
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|QUERY PLAN |
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|Sort (cost=3037398.97..3037728.28 rows=131726 width=1981) (actual time=1714.744..1714.812 rows=648 loops=1) |
| Sort Key: task.processinstanceid DESC, task.createdon DESC |
| Sort Method: quicksort Memory: 884kB |
| Buffers: shared hit=930703 |
| -> WindowAgg (cost=0.55..2800174.53 rows=131726 width=1981) (actual time=6.919..1713.339 rows=648 loops=1) |
| Buffers: shared hit=930703 |
| -> Merge Right Join (cost=0.55..2798527.96 rows=131726 width=1965) (actual time=6.905..1712.235 rows=648 loops=1) |
| Merge Cond: (po.task_id = task.taskid) |
| Buffers: shared hit=930703 |
| -> Index Only Scan using idx_paspot_taskentity on peopleassignments_potowners po (cost=0.27..32.45 rows=462 width=28) (actual time=0.101..0.378 rows=446 loops=1)|
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
【问题讨论】:
-
您好,首先我会对其进行解释分析,以便您对数据库实际使用它的操作有一个合理的估计。
-
通过将 WHERE ... IN 替换为 INNER JOIN 是否会提高性能?
-
dba.stackexchange.com 上的人可能也能提供帮助
-
不会有什么不同,但是:
(select * from audittaskimpl) as task可以简化为audittaskimpl as task。并且请不要在 WHERE 子句和显式JOIN运算符中混合使用旧的、古老的和脆弱的隐式连接。对所有连接使用显式JOIN -
请edit您的问题并添加使用
explain (analyze, buffers, format text)生成的execution plan(不是只是一个“简单”解释)为formatted text,并确保保留计划的缩进。粘贴文本,然后将```放在计划前一行和计划后一行。还请包括所有索引的完整create index语句。
标签: postgresql query-optimization