I have the following query -
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
This is the query plan of the query -
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)
How can I change or optimize this query so it finishes running. It has currently been running for 2 hours. I have VACUUM ANALYZE the data table. I have checked for locks on the backend and there are none. There are also no other processes updating any of the tables this query is using.
UPDATE
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.npi, 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.type, org_identifier.id
-> Sort (cost=2.67..2.67 rows=1 width=56) (never executed)
Output: org_identifier.org_id, org_identifier.type, org_identifier.id, etl_1.npi, org_identifier.pid
Sort Key: org_identifier.org_id, org_identifier.type, org_identifier.id
-> Nested Loop (cost=0.41..2.66 rows=1 width=56) (never executed)
Output: org_identifier.org_id, org_identifier.type, org_identifier.id, etl_1.npi, 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.npi, 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, org_identifier.type, 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.npi, 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