-- ============================================================
-- 동기화 현황 조회 성능 개선 인덱스
-- 대상 스키마: std_snp_data
--
-- 쿼리 패턴:
--   SELECT batch_flag, COUNT(*), MAX(CASE WHEN batch_flag='S' THEN crt_dt END)
--   FROM table a
--   INNER JOIN batch_job_execution b ON a.job_execution_id = b.job_execution_id AND b.status = 'COMPLETED'
--   WHERE a.batch_flag IN ('N', 'P', 'S')
--   GROUP BY a.batch_flag
--
-- 인덱스 전략:
--   1. 각 소스 테이블: (batch_flag, job_execution_id) 복합 인덱스
--      → batch_flag 필터링 + JOIN 조건을 인덱스만으로 처리
--      → crt_dt INCLUDE로 커버링 인덱스 (Index Only Scan 가능)
--   2. batch_job_execution: (job_execution_id, status) 복합 인덱스
--      → JOIN 조건을 인덱스만으로 처리
-- ============================================================

-- batch_job_execution 테이블 (1회만 실행)
CREATE INDEX IF NOT EXISTS idx_batch_job_execution_status
    ON std_snp_data.batch_job_execution (job_execution_id, status);

-- Ship 테이블
CREATE INDEX IF NOT EXISTS idx_ship_info_mst_sync ON std_snp_data.tb_ship_info_mst (batch_flag, job_execution_id) INCLUDE (crt_dt);
CREATE INDEX IF NOT EXISTS idx_ship_add_info_sync ON std_snp_data.tb_ship_add_info (batch_flag, job_execution_id) INCLUDE (crt_dt);
CREATE INDEX IF NOT EXISTS idx_ship_bbctr_hstry_sync ON std_snp_data.tb_ship_bbctr_hstry (batch_flag, job_execution_id) INCLUDE (crt_dt);
CREATE INDEX IF NOT EXISTS idx_ship_idntf_info_hstry_sync ON std_snp_data.tb_ship_idntf_info_hstry (batch_flag, job_execution_id) INCLUDE (crt_dt);
CREATE INDEX IF NOT EXISTS idx_ship_clfic_hstry_sync ON std_snp_data.tb_ship_clfic_hstry (batch_flag, job_execution_id) INCLUDE (crt_dt);
CREATE INDEX IF NOT EXISTS idx_ship_company_rel_sync ON std_snp_data.tb_ship_company_rel (batch_flag, job_execution_id) INCLUDE (crt_dt);
CREATE INDEX IF NOT EXISTS idx_ship_crew_list_sync ON std_snp_data.tb_ship_crew_list (batch_flag, job_execution_id) INCLUDE (crt_dt);
CREATE INDEX IF NOT EXISTS idx_ship_dark_actv_idnty_sync ON std_snp_data.tb_ship_dark_actv_idnty (batch_flag, job_execution_id) INCLUDE (crt_dt);
CREATE INDEX IF NOT EXISTS idx_ship_country_hstry_sync ON std_snp_data.tb_ship_country_hstry (batch_flag, job_execution_id) INCLUDE (crt_dt);
CREATE INDEX IF NOT EXISTS idx_ship_group_revn_ownr_hstry_sync ON std_snp_data.tb_ship_group_revn_ownr_hstry (batch_flag, job_execution_id) INCLUDE (crt_dt);
CREATE INDEX IF NOT EXISTS idx_ship_ice_grd_sync ON std_snp_data.tb_ship_ice_grd (batch_flag, job_execution_id) INCLUDE (crt_dt);
CREATE INDEX IF NOT EXISTS idx_ship_nm_chg_hstry_sync ON std_snp_data.tb_ship_nm_chg_hstry (batch_flag, job_execution_id) INCLUDE (crt_dt);
CREATE INDEX IF NOT EXISTS idx_ship_operator_hstry_sync ON std_snp_data.tb_ship_operator_hstry (batch_flag, job_execution_id) INCLUDE (crt_dt);
CREATE INDEX IF NOT EXISTS idx_ship_ownr_hstry_sync ON std_snp_data.tb_ship_ownr_hstry (batch_flag, job_execution_id) INCLUDE (crt_dt);
CREATE INDEX IF NOT EXISTS idx_ship_prtc_rpn_hstry_sync ON std_snp_data.tb_ship_prtc_rpn_hstry (batch_flag, job_execution_id) INCLUDE (crt_dt);
CREATE INDEX IF NOT EXISTS idx_ship_sfty_mng_evdc_hstry_sync ON std_snp_data.tb_ship_sfty_mng_evdc_hstry (batch_flag, job_execution_id) INCLUDE (crt_dt);
CREATE INDEX IF NOT EXISTS idx_ship_mng_company_hstry_sync ON std_snp_data.tb_ship_mng_company_hstry (batch_flag, job_execution_id) INCLUDE (crt_dt);
CREATE INDEX IF NOT EXISTS idx_ship_sstrvsl_rel_sync ON std_snp_data.tb_ship_sstrvsl_rel (batch_flag, job_execution_id) INCLUDE (crt_dt);
CREATE INDEX IF NOT EXISTS idx_ship_spc_fetr_sync ON std_snp_data.tb_ship_spc_fetr (batch_flag, job_execution_id) INCLUDE (crt_dt);
CREATE INDEX IF NOT EXISTS idx_ship_status_hstry_sync ON std_snp_data.tb_ship_status_hstry (batch_flag, job_execution_id) INCLUDE (crt_dt);
CREATE INDEX IF NOT EXISTS idx_ship_cargo_capacity_sync ON std_snp_data.tb_ship_cargo_capacity (batch_flag, job_execution_id) INCLUDE (crt_dt);
CREATE INDEX IF NOT EXISTS idx_ship_inspection_ymd_sync ON std_snp_data.tb_ship_inspection_ymd (batch_flag, job_execution_id) INCLUDE (crt_dt);
CREATE INDEX IF NOT EXISTS idx_ship_inspection_ymd_hstry_sync ON std_snp_data.tb_ship_inspection_ymd_hstry (batch_flag, job_execution_id) INCLUDE (crt_dt);
CREATE INDEX IF NOT EXISTS idx_ship_tech_mng_company_hstry_sync ON std_snp_data.tb_ship_tech_mng_company_hstry (batch_flag, job_execution_id) INCLUDE (crt_dt);
CREATE INDEX IF NOT EXISTS idx_ship_thrstr_info_sync ON std_snp_data.tb_ship_thrstr_info (batch_flag, job_execution_id) INCLUDE (crt_dt);

-- Company
CREATE INDEX IF NOT EXISTS idx_company_dtl_info_sync ON std_snp_data.tb_company_dtl_info (batch_flag, job_execution_id) INCLUDE (crt_dt);

-- Event
CREATE INDEX IF NOT EXISTS idx_event_mst_sync ON std_snp_data.tb_event_mst (batch_flag, job_execution_id) INCLUDE (crt_dt);
CREATE INDEX IF NOT EXISTS idx_event_cargo_sync ON std_snp_data.tb_event_cargo (batch_flag, job_execution_id) INCLUDE (crt_dt);
CREATE INDEX IF NOT EXISTS idx_event_humn_acdnt_sync ON std_snp_data.tb_event_humn_acdnt (batch_flag, job_execution_id) INCLUDE (crt_dt);
CREATE INDEX IF NOT EXISTS idx_event_rel_sync ON std_snp_data.tb_event_rel (batch_flag, job_execution_id) INCLUDE (crt_dt);

-- Facility
CREATE INDEX IF NOT EXISTS idx_port_facility_info_sync ON std_snp_data.tb_port_facility_info (batch_flag, job_execution_id) INCLUDE (crt_dt);

-- PSC
CREATE INDEX IF NOT EXISTS idx_psc_mst_sync ON std_snp_data.tb_psc_mst (batch_flag, job_execution_id) INCLUDE (crt_dt);
CREATE INDEX IF NOT EXISTS idx_psc_defect_sync ON std_snp_data.tb_psc_defect (batch_flag, job_execution_id) INCLUDE (crt_dt);
CREATE INDEX IF NOT EXISTS idx_psc_oa_certf_sync ON std_snp_data.tb_psc_oa_certf (batch_flag, job_execution_id) INCLUDE (crt_dt);

-- Movements
CREATE INDEX IF NOT EXISTS idx_ship_anchrgcall_hstry_sync ON std_snp_data.tb_ship_anchrgcall_hstry (batch_flag, job_execution_id) INCLUDE (crt_dt);
CREATE INDEX IF NOT EXISTS idx_ship_berthcall_hstry_sync ON std_snp_data.tb_ship_berthcall_hstry (batch_flag, job_execution_id) INCLUDE (crt_dt);
CREATE INDEX IF NOT EXISTS idx_ship_now_status_hstry_sync ON std_snp_data.tb_ship_now_status_hstry (batch_flag, job_execution_id) INCLUDE (crt_dt);
CREATE INDEX IF NOT EXISTS idx_ship_dest_hstry_sync ON std_snp_data.tb_ship_dest_hstry (batch_flag, job_execution_id) INCLUDE (crt_dt);
CREATE INDEX IF NOT EXISTS idx_ship_prtcll_hstry_sync ON std_snp_data.tb_ship_prtcll_hstry (batch_flag, job_execution_id) INCLUDE (crt_dt);
CREATE INDEX IF NOT EXISTS idx_ship_sts_opert_hstry_sync ON std_snp_data.tb_ship_sts_opert_hstry (batch_flag, job_execution_id) INCLUDE (crt_dt);
CREATE INDEX IF NOT EXISTS idx_ship_teminalcall_hstry_sync ON std_snp_data.tb_ship_teminalcall_hstry (batch_flag, job_execution_id) INCLUDE (crt_dt);
CREATE INDEX IF NOT EXISTS idx_ship_trnst_hstry_sync ON std_snp_data.tb_ship_trnst_hstry (batch_flag, job_execution_id) INCLUDE (crt_dt);

-- Code
CREATE INDEX IF NOT EXISTS idx_ship_type_cd_sync ON std_snp_data.tb_ship_type_cd (batch_flag, job_execution_id) INCLUDE (crt_dt);
CREATE INDEX IF NOT EXISTS idx_ship_country_cd_sync ON std_snp_data.tb_ship_country_cd (batch_flag, job_execution_id) INCLUDE (crt_dt);

-- Risk & Compliance
CREATE INDEX IF NOT EXISTS idx_ship_risk_info_sync ON std_snp_data.tb_ship_risk_info (batch_flag, job_execution_id) INCLUDE (crt_dt);
CREATE INDEX IF NOT EXISTS idx_ship_compliance_info_sync ON std_snp_data.tb_ship_compliance_info (batch_flag, job_execution_id) INCLUDE (crt_dt);
CREATE INDEX IF NOT EXISTS idx_company_compliance_info_sync ON std_snp_data.tb_company_compliance_info (batch_flag, job_execution_id) INCLUDE (crt_dt);

-- ============================================================
-- std_snp_svc (타겟 스키마) - 데이터 미리보기 조회 성능 개선
--
-- 쿼리 패턴:
--   SELECT * FROM std_snp_svc.table ORDER BY crt_dt DESC NULLS LAST LIMIT 20
--
-- 인덱스 전략:
--   crt_dt DESC 인덱스 → ORDER BY + LIMIT을 Index Scan으로 처리
-- ============================================================

-- Ship
CREATE INDEX IF NOT EXISTS idx_svc_ship_info_mst_crt ON std_snp_svc.tb_ship_info_mst (crt_dt DESC);
CREATE INDEX IF NOT EXISTS idx_svc_ship_add_info_crt ON std_snp_svc.tb_ship_add_info (crt_dt DESC);
CREATE INDEX IF NOT EXISTS idx_svc_ship_bbctr_hstry_crt ON std_snp_svc.tb_ship_bbctr_hstry (crt_dt DESC);
CREATE INDEX IF NOT EXISTS idx_svc_ship_idntf_info_hstry_crt ON std_snp_svc.tb_ship_idntf_info_hstry (crt_dt DESC);
CREATE INDEX IF NOT EXISTS idx_svc_ship_clfic_hstry_crt ON std_snp_svc.tb_ship_clfic_hstry (crt_dt DESC);
CREATE INDEX IF NOT EXISTS idx_svc_ship_company_rel_crt ON std_snp_svc.tb_ship_company_rel (crt_dt DESC);
CREATE INDEX IF NOT EXISTS idx_svc_ship_crew_list_crt ON std_snp_svc.tb_ship_crew_list (crt_dt DESC);
CREATE INDEX IF NOT EXISTS idx_svc_ship_dark_actv_idnty_crt ON std_snp_svc.tb_ship_dark_actv_idnty (crt_dt DESC);
CREATE INDEX IF NOT EXISTS idx_svc_ship_country_hstry_crt ON std_snp_svc.tb_ship_country_hstry (crt_dt DESC);
CREATE INDEX IF NOT EXISTS idx_svc_ship_group_revn_ownr_hstry_crt ON std_snp_svc.tb_ship_group_revn_ownr_hstry (crt_dt DESC);
CREATE INDEX IF NOT EXISTS idx_svc_ship_ice_grd_crt ON std_snp_svc.tb_ship_ice_grd (crt_dt DESC);
CREATE INDEX IF NOT EXISTS idx_svc_ship_nm_chg_hstry_crt ON std_snp_svc.tb_ship_nm_chg_hstry (crt_dt DESC);
CREATE INDEX IF NOT EXISTS idx_svc_ship_operator_hstry_crt ON std_snp_svc.tb_ship_operator_hstry (crt_dt DESC);
CREATE INDEX IF NOT EXISTS idx_svc_ship_ownr_hstry_crt ON std_snp_svc.tb_ship_ownr_hstry (crt_dt DESC);
CREATE INDEX IF NOT EXISTS idx_svc_ship_prtc_rpn_hstry_crt ON std_snp_svc.tb_ship_prtc_rpn_hstry (crt_dt DESC);
CREATE INDEX IF NOT EXISTS idx_svc_ship_sfty_mng_evdc_hstry_crt ON std_snp_svc.tb_ship_sfty_mng_evdc_hstry (crt_dt DESC);
CREATE INDEX IF NOT EXISTS idx_svc_ship_mng_company_hstry_crt ON std_snp_svc.tb_ship_mng_company_hstry (crt_dt DESC);
CREATE INDEX IF NOT EXISTS idx_svc_ship_sstrvsl_rel_crt ON std_snp_svc.tb_ship_sstrvsl_rel (crt_dt DESC);
CREATE INDEX IF NOT EXISTS idx_svc_ship_spc_fetr_crt ON std_snp_svc.tb_ship_spc_fetr (crt_dt DESC);
CREATE INDEX IF NOT EXISTS idx_svc_ship_status_hstry_crt ON std_snp_svc.tb_ship_status_hstry (crt_dt DESC);
CREATE INDEX IF NOT EXISTS idx_svc_ship_cargo_capacity_crt ON std_snp_svc.tb_ship_cargo_capacity (crt_dt DESC);
CREATE INDEX IF NOT EXISTS idx_svc_ship_inspection_ymd_crt ON std_snp_svc.tb_ship_inspection_ymd (crt_dt DESC);
CREATE INDEX IF NOT EXISTS idx_svc_ship_inspection_ymd_hstry_crt ON std_snp_svc.tb_ship_inspection_ymd_hstry (crt_dt DESC);
CREATE INDEX IF NOT EXISTS idx_svc_ship_tech_mng_company_hstry_crt ON std_snp_svc.tb_ship_tech_mng_company_hstry (crt_dt DESC);
CREATE INDEX IF NOT EXISTS idx_svc_ship_thrstr_info_crt ON std_snp_svc.tb_ship_thrstr_info (crt_dt DESC);

-- Company
CREATE INDEX IF NOT EXISTS idx_svc_company_dtl_info_crt ON std_snp_svc.tb_company_dtl_info (crt_dt DESC);

-- Event
CREATE INDEX IF NOT EXISTS idx_svc_event_mst_crt ON std_snp_svc.tb_event_mst (crt_dt DESC);
CREATE INDEX IF NOT EXISTS idx_svc_event_cargo_crt ON std_snp_svc.tb_event_cargo (crt_dt DESC);
CREATE INDEX IF NOT EXISTS idx_svc_event_humn_acdnt_crt ON std_snp_svc.tb_event_humn_acdnt (crt_dt DESC);
CREATE INDEX IF NOT EXISTS idx_svc_event_rel_crt ON std_snp_svc.tb_event_rel (crt_dt DESC);

-- Facility
CREATE INDEX IF NOT EXISTS idx_svc_port_facility_info_crt ON std_snp_svc.tb_port_facility_info (crt_dt DESC);

-- PSC
CREATE INDEX IF NOT EXISTS idx_svc_psc_mst_crt ON std_snp_svc.tb_psc_mst (crt_dt DESC);
CREATE INDEX IF NOT EXISTS idx_svc_psc_defect_crt ON std_snp_svc.tb_psc_defect (crt_dt DESC);
CREATE INDEX IF NOT EXISTS idx_svc_psc_oa_certf_crt ON std_snp_svc.tb_psc_oa_certf (crt_dt DESC);

-- Movements
CREATE INDEX IF NOT EXISTS idx_svc_ship_anchrgcall_hstry_crt ON std_snp_svc.tb_ship_anchrgcall_hstry (crt_dt DESC);
CREATE INDEX IF NOT EXISTS idx_svc_ship_berthcall_hstry_crt ON std_snp_svc.tb_ship_berthcall_hstry (crt_dt DESC);
CREATE INDEX IF NOT EXISTS idx_svc_ship_now_status_hstry_crt ON std_snp_svc.tb_ship_now_status_hstry (crt_dt DESC);
CREATE INDEX IF NOT EXISTS idx_svc_ship_dest_hstry_crt ON std_snp_svc.tb_ship_dest_hstry (crt_dt DESC);
CREATE INDEX IF NOT EXISTS idx_svc_ship_prtcll_hstry_crt ON std_snp_svc.tb_ship_prtcll_hstry (crt_dt DESC);
CREATE INDEX IF NOT EXISTS idx_svc_ship_sts_opert_hstry_crt ON std_snp_svc.tb_ship_sts_opert_hstry (crt_dt DESC);
CREATE INDEX IF NOT EXISTS idx_svc_ship_teminalcall_hstry_crt ON std_snp_svc.tb_ship_teminalcall_hstry (crt_dt DESC);
CREATE INDEX IF NOT EXISTS idx_svc_ship_trnst_hstry_crt ON std_snp_svc.tb_ship_trnst_hstry (crt_dt DESC);

-- Code
CREATE INDEX IF NOT EXISTS idx_svc_ship_type_cd_crt ON std_snp_svc.tb_ship_type_cd (crt_dt DESC);
CREATE INDEX IF NOT EXISTS idx_svc_ship_country_cd_crt ON std_snp_svc.tb_ship_country_cd (crt_dt DESC);

-- Risk & Compliance
CREATE INDEX IF NOT EXISTS idx_svc_ship_risk_info_crt ON std_snp_svc.tb_ship_risk_info (crt_dt DESC);
CREATE INDEX IF NOT EXISTS idx_svc_ship_risk_hstry_crt ON std_snp_svc.tb_ship_risk_hstry (crt_dt DESC);
CREATE INDEX IF NOT EXISTS idx_svc_ship_compliance_info_crt ON std_snp_svc.tb_ship_compliance_info (crt_dt DESC);
CREATE INDEX IF NOT EXISTS idx_svc_ship_compliance_hstry_crt ON std_snp_svc.tb_ship_compliance_hstry (crt_dt DESC);
CREATE INDEX IF NOT EXISTS idx_svc_ship_compliance_info_hstry_crt ON std_snp_svc.tb_ship_compliance_info_hstry (crt_dt DESC);
CREATE INDEX IF NOT EXISTS idx_svc_company_compliance_info_crt ON std_snp_svc.tb_company_compliance_info (crt_dt DESC);
CREATE INDEX IF NOT EXISTS idx_svc_company_compliance_hstry_crt ON std_snp_svc.tb_company_compliance_hstry (crt_dt DESC);
CREATE INDEX IF NOT EXISTS idx_svc_company_compliance_info_hstry_crt ON std_snp_svc.tb_company_compliance_info_hstry (crt_dt DESC);
