如何解决SQL 查询不会完成
WITH
etl AS
(
SELECT
clm.patient_id,clm.npi,clm.provider,clm.start::DATE AS start_dt
FROM rev
JOIN data clm
ON rev.patient_id = clm.patient_id AND rev.claim = clm.CLaim
),i AS
(
SELECT org_id,etl.npi,pid,ROW_NUMBER() OVER(PARTITION BY org_id ORDER BY type,id) AS order
FROM etl
JOIN org_identifier
ON etl.npi = org_identifier.npi
AND etl.provider = org_identifier.pid
AND type ILIKE 'CARE%'
)
SELECT *
FROM etl
JOIN i
ON
i.npi = etl.npi AND i.pid = etl.provider AND i.order = 1
nested Loop (cost=437.29..437.35 rows=1 width=206)
Join Filter: ((etl.npi = i.npi) AND (etl.provider = (i.pid)::text))
CTE etl
-> nested Loop (cost=0.43..434.60 rows=1 width=30)
-> Seq Scan on rev (cost=0.00..11.60 rows=160 width=12)
-> Index Scan using data_unique on data clm (cost=0.43..2.63 rows=1 width=34)
Index Cond: ((patient_id = rev.patient_id) AND (claim = rev.claim))
CTE ooi
-> WindowAgg (cost=2.67..2.69 rows=1 width=64)
-> Sort (cost=2.67..2.67 rows=1 width=56)
Sort Key: org_identifier.org_id,org_identifier.type,org_identifier.id
-> nested Loop (cost=0.41..2.66 rows=1 width=56)
-> CTE Scan on etl etl_1 (cost=0.00..0.02 rows=1 width=64)
-> Index Scan using org_identifier_pid_npi on org_identifier (cost=0.41..2.64 rows=1 width=35)
Index Cond: (((pid)::text = etl_1.provider) AND ((npi)::text = etl_1.npi))
Filter: ((type)::text ~~* 'CARE%'::text)
-> CTE Scan on etl (cost=0.00..0.02 rows=1 width=76)
-> CTE Scan on i (cost=0.00..0.02 rows=1 width=130)
Filter: (order = 1)
如何更改或优化此查询以使其完成运行。它目前已经运行了2个小时。我有 VACUUM ANALYZE
数据表。我检查了后端的锁,但没有。也没有其他进程更新此查询正在使用的任何表。
更新
nested Loop (cost=437.29..437.35 rows=1 width=8) (actual
time=0.037..0.037 rows=0 loops=1)
Output: i.org_id
Join Filter: ((etl.npi = i.npi) AND (etl.ccn = (i.pid)::text))
Buffers: shared hit=43
CTE etl
-> nested Loop (cost=0.43..434.60 rows=1 width=30) (actual time=0.037..0.037 rows=0 loops=1)
Output: clm.patient_id,clm.ccn,clm.start
Buffers: shared hit=43
-> Seq Scan on public.rev (cost=0.00..11.60 rows=160 width=12) (actual time=0.006..0.008 rows=14 loops=1)
Output: rev.patient_id,rev.claim,...other columns...
Buffers: shared hit=1
-> Index Scan using data_unique on public.data clm (cost=0.43..2.63 rows=1 width=34) (actual time=0.002..0.002 rows=0 loops=14)
Output: clm.claim,clm.patient_id,clm.start,...other columns...
Index Cond: ((clm.patient_id = rev.patient_id) AND (clm.claim = rev.claim))
Buffers: shared hit=42
CTE i
-> WindowAgg (cost=2.67..2.69 rows=1 width=64) (never executed)
Output: org_identifier.org_id,etl_1.npi,org_identifier.pid,row_number() OVER (?),org_identifier.id
-> Sort (cost=2.67..2.67 rows=1 width=56) (never executed)
Output: org_identifier.org_id,org_identifier.id,org_identifier.pid
Sort Key: org_identifier.org_id,org_identifier.id
-> nested Loop (cost=0.41..2.66 rows=1 width=56) (never executed)
Output: org_identifier.org_id,org_identifier.pid
Inner Unique: true
-> CTE Scan on etl etl_1 (cost=0.00..0.02 rows=1 width=64) (never executed)
Output: etl_1.patient_id,etl_1.ccn,etl_1.start
-> Index Scan using org_identifier_pid_npi on public.org_identifier (cost=0.41..2.64 rows=1 width=35) (never executed)
Output: org_identifier.id,org_identifier.org_id,org_identifier.pid_ca,org_identifier.pid_state,org_identifier.npi
Index Cond: (((org_identifier.pid)::text = etl_1.ccn) AND ((org_identifier.npi)::text = etl_1.npi))
Filter: ((org_identifier.type)::text ~~* 'CARE%'::text)
-> CTE Scan on etl (cost=0.00..0.02 rows=1 width=64) (actual time=0.037..0.037 rows=0 loops=1)
Output: etl.patient_id,etl.ccn,etl.start
Buffers: shared hit=43
-> CTE Scan on i (cost=0.00..0.02 rows=1 width=122) (never executed)
Output: i.org_id,i.npi,i.pid,i.pid_order
Filter: (i.pid_order = 1)
Planning time: 0.403 ms
Execution time: 0.085 ms
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。