Abfrage, die in Stapeljobs > 100 Minuten benötigt, um abgeschlossen zu werden. Wie kann ich die Zeit verkürzen?
–Explain
SELECT DISTINCT
INV_NO_001 “Invoice Number”, Chr(9),
INVOICE.INV_ID_002 “Invoice ID”, Chr(9),
CASE
WHEN INV_TYPE =‘B’ THEN ‘Billed Invoice’
WHEN INV_TYPE =‘M’ THEN ‘Miscellaneous’
END
AS “Document Type”,Chr(9),
CASE WHEN INVOICE.INV_PAID_FLAG = ‘P’ THEN INVOICE.INV_CLOSE_DATE ELSE NULL END AS “Invoice Close Date”, Chr(9),
trim(inv_curr3) “Billed Currency”, Chr(9),
ROUND((INV_AMT / 100), 2) “Invoice Amount”, Chr(9),
ROUND(((INV_AMT - INV_AMT_PAID) / 100),2) “Outstanding Amount”, Chr(9),
ROUND((INV_AMT *EXCH_RATE2 / 100), 2) “Invoice Amount (USD)”, Chr(9),
ROUND(((INV_AMT - INV_AMT_PAID) *EXCH_RATE2 / 100),2) “Outstanding Amount (USD)”, Chr(9),
PERS_ID “Touch By OHR ID”, Chr(9),
PERS_NAME_ “Touch By Name”, Chr(9),
FROM (
SELECT DISTINCT PERS_SSO_ID ,PERS_NAME, INV_ID_002 INVOICE_ID,
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(TRIM(Substring(Scr_Line_Desc, 1, 1000)),Chr(10),’ ‘),Chr(13),’ ‘),
Chr(9),’ ‘),Chr(126),’-‘),Chr(44),’ ‘),’"‘,’') Sp_Comments,
IC_NO,IC_NAME
FROM
test.INVESTMENT_CODE INNER JOIN test.INVOICE ON IC_NO_702 = INV_IC_NO_720
INNER JOIN test.INVOICE_SCRATCH_PAD ON (SCR_CUST_NO_722 = INV_CUST_NO_720 AND SCR_FINDER_NO_722 = INV_FINDER_NO_720)
LEFT JOIN test.PERSONNEL ON SCR_USER_ID_722 = PERS_CODE_796 INNER JOIN test.CUSTOMER ON INV_CUST_NO_720 = CUST_NO_717
AND SCR_LINE_DESC_722 IS NOT NULL
and IC_H2_702=‘HCG’ AND IC_H3_702=‘LSC’
AND DATE_TRUNC(‘DAY’,SCR_DATE_722::DATE) >= date_trunc(‘week’, current_timestamp::timestamp(0))
AND DATE_TRUNC(‘DAY’,SCR_DATE_722) <= (current_date + 7 - (extract(dow from current_timestamp::timestamp(0)) || ’ days’)::interval)
AND DATE_TRUNC(‘DAY’,INVOICE_SCRATCH_PAD.TIMESTAMP::DATE) >= date_trunc(‘week’, current_timestamp::timestamp(0))
AND DATE_TRUNC(‘DAY’,INVOICE_SCRATCH_PAD.TIMESTAMP::DATE) <=
(current_date + 7 - (extract(dow from current_date) || ’ days’)::interval)
)A LEFT OUTER JOIN
(
SELECT DISTINCT INV_ID_002 INVOICE_ID, SCR_DATE_722 SP_DATE, SCR_CONTACT_TYPE_722 CONTACT_TYPE,
SCR_OPEN_ITEM_STAT1_722 SP_CODE,
INVOICE_SCRATCH_PAD.TIMESTAMP,
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(TRIM(OTHER_TABLE_NAME_794), CHR(10), ’ '), CHR(13), ’ '), CHR(9), ’ '),
CHR(126), ‘-’), CHR(44), ’ '), ‘“”’, ‘’) SP_DESCRIPTION
FROM
test.INVESTMENT_CODE INNER JOIN test.INVOICE ON IC_NO_702 = INV_IC_NO_720
INNER JOIN test.INVOICE_SCRATCH_PAD ON (SCR_CUST_NO_722 = INV_CUST_NO_720
AND SCR_FINDER_NO_722 = INV_FINDER_NO_720)
INNER JOIN test.OTHER_TABLE ON ( OTHER_TABLE_KEY_794 = SCR_OPEN_ITEM_STAT1_722
AND OTHER_TABLE_TYPE_794 = ‘SP’ AND OTHER_TABLE_LANG_794 = ‘E’
AND SCR_LINE_DESC_722 IS NULL
and IC_H2_702=‘HCG’ AND IC_H3_702=‘LSC’
AND DATE_TRUNC(‘DAY’,INVOICE_SCRATCH_PAD.TIMESTAMP::DATE) >= date_trunc(‘week’, current_timestamp::timestamp(0))
AND DATE_TRUNC(‘DAY’,INVOICE_SCRATCH_PAD.TIMESTAMP) <= (current_date + 7 -
(extract(dow from current_timestamp::timestamp(0)) || ’ days’)::interval))
) B ON A.INVOICE_ID = B.INVOICE_ID
AND DATE_TRUNC(‘minute’,A.TIMESTAMP::DATE) = DATE_TRUNC(‘minute’,B.TIMESTAMP::DATE)
AND COALESCE(A.CONTACT_TYPE,‘na’) = COALESCE(B.CONTACT_TYPE, ‘na’)
INNER JOIN test.INVOICE ON A.INVOICE_ID = INVOICE.INV_ID_002
LEFT OUTER JOIN test.EXCHANGE_RATE2 ON test.INVOICE.INV_CURR3_720 = EXCH_CURR3_FROM_792
AND EXCH_RATE_TYPE_792=‘CM’ AND EXCH_CURR3_TO_792=‘USD’
ORDER BY INVOICE.INV_ID_002 ,A.SP_DATE ASC;
– Unique (cost=788268.15…788268.29 rows=2 width=1303)
----- Explain Details:
“Unique (cost=788268.15…788268.29 rows=2 width=1303)”
" → Sort (cost=788268.15…788268.16 rows=2 width=1303)"
" Sort Key: invoice.inv_id_720, a.sp_date, invoice.inv_no_720, a.business_alias, invoice.inv_cust_no_720, invoice.inv_cust_short_name_720, invoice.inv_date_720, invoice.inv_due_date_720, invoice.inv_system_dt_720, a.cust_cc_no_717, a.ic_no_702, a.ic_name_702, (CASE WHEN (invoice.inv_type_720 = ‘B’::bpchar) THEN ‘Billed Invoice’::text WHEN (invoice.inv_type_720 = ‘C’::bpchar) THEN ‘Billed Credit Note’::text WHEN (invoice.inv_type_720 = ‘N’::bpchar) THEN ‘Memo Invoice’::text WHEN (invoice.inv_type_720 = ‘U’::bpchar) THEN ‘Unapplied Cash’::text WHEN (invoice.inv_type_720 = ‘V’::bpchar) THEN ‘Variance (Cash Appln)’::text WHEN (invoice.inv_type_720 = ‘W’::bpchar) THEN ‘Variance (Zero Appln)’::text WHEN (invoice.inv_type_720 = ‘X’::bpchar) THEN ‘Reverse Variance (Cash)’::text WHEN (invoice.inv_type_720 = ‘Y’::bpchar) THEN ‘REVERSE VARIANCE(ZERO)’::text WHEN (invoice.inv_type_720 = ‘E’::bpchar) THEN ‘Charge Entry’::text WHEN (invoice.inv_type_720 = ‘I’::bpchar) THEN ‘Interest Invoice’::text WHEN (invoice.inv_type_720 = ‘M’::bpchar) THEN ‘Miscellaneous’::text ELSE NULL::text END), (CASE WHEN (invoice.inv_paid_flag_720 = ‘P’::bpchar) THEN ‘Closed’::text ELSE ‘Open’::text END), (CASE WHEN (invoice.inv_paid_flag_720 = ‘P’::bpchar) THEN invoice.inv_close_date_720 ELSE NULL::timestamp without time zone END), (TRIM(BOTH FROM invoice.inv_curr3_720)), (round((invoice.inv_amt_720 / ‘100’::numeric), 2)), (round(((invoice.inv_amt_720 - invoice.inv_amt_paid_720) / ‘100’::numeric), 2)), (round(((invoice.inv_amt_720 * exchange_rate2.exch_rate2_792) / ‘100’::numeric), 2)), (round((((invoice.inv_amt_720 - invoice.inv_amt_paid_720) * exchange_rate2.exch_rate2_792) / ‘100’::numeric), 2)), a.pers_sso_id_796, a.pers_name_796, a.contact_type, a.contact_result, b.sp_description, a.sp_comments"
" → Nested Loop Left Join (cost=788250.57…788268.14 rows=2 width=1303)"
" → Nested Loop (cost=788250.28…788267.10 rows=2 width=267)"
" → Merge Left Join (cost=788249.84…788250.18 rows=2 width=182)"
" Merge Cond: ((a.invoice_id = b.invoice_id) AND ((date_trunc(‘minute’::text, ((a.““timestamp””)::date)::timestamp with time zone)) = (date_trunc(‘minute’::text, ((b.““timestamp””)::date)::timestamp with time zone))) AND (((COALESCE(a.contact_type, ‘na’::character varying))::text) = ((COALESCE(b.contact_type, ‘na’::character varying))::text)))"
" → Sort (cost=394264.20…394264.21 rows=2 width=158)"
" Sort Key: a.invoice_id, (date_trunc(‘minute’::text, ((a.““timestamp””)::date)::timestamp with time zone)), ((COALESCE(a.contact_type, ‘na’::character varying))::text)"
" → Subquery Scan on a (cost=394264.09…394264.19 rows=2 width=158)"
" → Unique (cost=394264.09…394264.17 rows=2 width=200)"
" → Sort (cost=394264.09…394264.09 rows=2 width=200)"
" Sort Key: personnel.pers_sso_id_796,