У меня есть этот запрос, содержащий ссылку dblink, так как мне нужно подключиться к другой базе данных, и он кажется таким медленным. Есть ли способ сделать это быстро? В другом запросе dblink, выполняемом в Mse c, но один и тот же запрос будет использоваться на другом сервере, тогда его выполнение занимает слишком много времени. Индекс уже создан на сервере ссылок БД. Есть ли какие-либо проблемы в запросе.
Ниже запрос используется для получения последних счетов.
Ниже приведен код:
WITH recursive derived AS
( SELECT inv_num, new_inv_num, 0 AS LEVEL
FROM "CBF".cbf_inv_gen_trans@prod_lbt_temp_cluster123 o ,
"CBF".bill_master@prod_lbt_temp_cluster123 bm
WHERE bm.invoice_no = o.new_inv_num
AND BM.id::number = o.batch_id::number
AND o.inv_num = '061810G10024409'
AND o.new_inv_num NOT LIKE '%XX%'
AND o.new_inv_num IS NOT NULL
AND o.attribute_id IS NOT NULL
AND o.attr_val IS NOT NULL
AND bm.bill_date is not null
UNION
SELECT distinct o.inv_num, cr.new_inv_num, 0 AS LEVEL
FROM "CBF".cbf_inv_gen_trans@prod_lbt_temp_cluster123 o , "CBF".cbf_inv_gen_trans@prod_lbt_temp_cluster123 CR,
"CBF".bill_master@prod_lbt_temp_cluster123 bm
WHERE o.new_inv_num = cr.inv_num
and bm.invoice_no = cr.new_inv_num
AND BM.id::number = cr.batch_id::number
AND o.inv_num = '061810G10024409'
and o.new_inv_num ilike '%XX%'
AND o.attribute_id IS NOT NULL
and cr.new_inv_num not ilike '%XX%'
and cr.attribute_id ='38'
AND bm.bill_date is not null
UNION
select ad.invoice_no,ad.new_inv_num, 0 AS LEVEL
from "CBF".cbf_custom_attr_value_details@prod_lbt_temp_cluster123 ad ,"CBF".bill_master@prod_lbt_temp_cluster123 bm
WHERE bm.invoice_no = ad.new_inv_num
AND BM.id::number = ad.batch_id::number
AND attribute_id = 129 and
new_inv_num is not null
and new_inv_num not like '%XX%'
and ad.invoice_no = '061810G10024409'
AND bm.bill_date is not null
), MERGE as (
SELECT *
FROM derived
union all
SELECT e.inv_num, e.new_inv_num, LEVEL + 1
FROM "CBF".cbf_inv_gen_trans@prod_lbt_temp_cluster123 e
inner join MERGE s ON s.new_inv_num = e.inv_num
inner JOIN "CBF".BILL_MASTER@prod_lbt_temp_cluster123 bm
ON BM.INVOICE_NO = e.NEW_INV_NUM and BM.id::number = e.batch_id::number
WHERE e.new_inv_num NOT LIKE '%XX%'
AND e.new_inv_num IS NOT NULL
AND e.attribute_id IS NOT NULL
AND e.attr_val IS NOT NULL
AND BM.BILL_DATE IS NOT NULL
),
RECREATE AS (***
SELECT *
FROM derived
union all
select ad.invoice_no,ad.new_inv_num,LEVEL + 1
from "CBF".cbf_custom_attr_value_details@prod_lbt_temp_cluster123 ad
inner join RECREATE s ON s.new_inv_num = ad.invoice_no
inner JOIN "CBF".BILL_MASTER@prod_lbt_temp_cluster123 bm ON
BM.INVOICE_NO = ad.NEW_INV_NUM and BM.id::number = ad.batch_id::number
where attribute_id = 129
AND ad.new_inv_num is not null
and ad.new_inv_num not like '%XX%'
AND BM.BILL_DATE IS NOT NULL
)
Select new_inv_num INTO v_new_inv_num from (SELECT * from MERGE
union
select * from RECREATE
)
WHERE LEVEL in (select MAX(LVL) from (
SELECT max(LEVEL) LVL FROM MERGE
UNION
SELECT max(LEVEL) LVL FROM RECREATE
UNION
SELECT max(LEVEL) LVL FROM derived )
) limit 1;