postgresql中没有松散的索引扫描,但是this stack overflow page描述了一种使用递归CTE扫描进行自我滚动的好方法。我的问题是:如何选择其他列(distinct
子句中未使用)?
这是我当前正在运行的代码:SELECT distinct on (HLH_EVNT_SK, DX_CD) HLH_EVNT_SK, DX_CD, DX_VRSN_ID, DX_TP_CD from med_clm_dx_final;
可以,但是速度很慢。
(HLH_EVNT_SK,DX_CD)在表med_clm_dx_final上定义一个索引;
为了加快速度,我可以像这样实现递归CTE
WITH RECURSIVE
t AS (
select HLH_EVNT_SK, DX_CD from (select HLH_EVNT_SK, DX_CD from med_clm_dx_final order by HLH_EVNT_SK, DX_CD limit 1) asdf union all
select (select HLH_EVNT_SK from med_clm_dx_final where (HLH_EVNT_SK, DX_CD) > (t.HLH_EVNT_SK,t.DX_CD) order by HLH_EVNT_SK, DX_CD limit 1),
(select DX_CD from med_clm_dx_final where (HLH_EVNT_SK, DX_CD) > (t.HLH_EVNT_SK,t.DX_CD) order by HLH_EVNT_SK, DX_CD limit 1)
from t where t.HLH_EVNT_SK is not null)
select * from t where t.HLH_EVNT_SK is not null
但是我不知道如何选择不包含在选择中的其他列,即DX_VRSN_ID和DX_TP_CD。谢谢!