PostgreSQL优化-exists改关联查询、with里面的where条件放到最终查询join的on里面


PostgreSQL的exists改关联查询

一、exists改关联查询

二、with里面的where条件放到最终查询join的on里面

三、示例SQL

-- drop table tzq_t4 cascade;
-- create table tzq_t3 as 
-- explain(analyze true,verbose true,costs true,buffers true,timing true,format text)
-- 
-- create table tzq_t4 as 
WITH t AS (
	select t
		.FUND_TRANSFER_APPLY_ID,
		t.proc_inst_id AS procInstId,
		(
		select
			fmt.message 
		from
			fnd.tpl_fd_message_t fmt 
		where
			fmt.app_name = 'etreasury' 
			and fmt.message_key = t.workflow_status 
			and fmt.language = 'en_US' 
			limit 1 
		) as status,
		coalesce (
			(
			select
				( select u.lname from fnd.tpl_user_t u where u.user_id = ta.assignee_ :: numeric ) 
			from
				ewf.act_ru_task AS ta 
			where
				ta.proc_inst_id_ = t.receive_procinst_id 
			),
			(
			SELECT
				string_agg ( ( SELECT u.lname FROM fnd.tpl_user_t u WHERE u.user_id = twhi.user_id_ ), ',' ) 
			FROM
				ewf.tpl_wf_hi_identitylink_t AS twhi,
				ewf.act_hi_taskinst AS art 
			WHERE
				twhi.task_id_ = art.id_ 
				AND art.proc_inst_id_ :: VARCHAR = t.receive_procinst_id 
				AND ( art.delete_reason_ != 'completed' OR art.delete_reason_ IS NULL ) 
				AND t.receive_wf_status = art.description_ 
				AND art.description_ != 'etreasury.deal.receivefund.flow.completed' 
			) 
		) AS currentHandler,
		t.deal_no as dealNumber,
		t.ticket_no as ticketNumber,
		( select im.instrument_name from ets.ets_bas_instrument_t im where im.bas_instrument_id = t.bas_instrument_id limit 1 ) as instrument,
		( select currency_code from ets.ets_mdm_rm_currency_info_t ct where ct.currencies_id = t.payment_ccy_id limit 1 ) as paymentCurrency,
		round( t.payment_amount, 2 ) as paymentAmount,
		to_char( t.payment_date, 'yyyy-MM-dd' ) as paymentDate,
		round( t.equivalent_usd_amount, 2 ) as equivalentUSDAmount,
	CASE
			
			WHEN t.urgent_flag = 1 THEN
			'Yes' 
			WHEN t.urgent_flag = 0 THEN
			'No' 
		END AS urgent,
		round( t.wht_amount, 2 ) as wHTAmount,
		( select et.management_entity_en_name from ets.ets_bas_management_entity_t et where et.bas_management_entity_id = t.back_to_back_subsidiary_id limit 1 ) as b2B3rdParty,
		t.back_to_back_deal_no as b2BDealNo,
		( select et.management_entity_en_name from ets.ets_bas_management_entity_t et where et.bas_management_entity_id = t.source_mgt_entity_id limit 1 ) as sourceEntity,
		( select ct.company_code from ets.ets_company_t ct where ct.company_id = t.source_company_id limit 1 ) as sourceCompanyCode,
		( select bt.bank_account_no from ets_bbam.finbas_bank_account_t bt where bt.bank_account_id = t.source_bank_account_id limit 1 ) as sourceBankAccountNo,
		( select currency_code from ets.ets_mdm_rm_currency_info_t ct where ct.currencies_id = t.source_acc_ccy_id limit 1 ) as sourceAccountCurrency,
		round( t.source_amount, 2 ) as sourceAmount,
		(
		select
			bnt.account_nature 
		from
			(
			SELECT
				n.bas_account_nature_id,
				n.account_nature_name,
			CASE
					
					WHEN coalesce ( n.parent_id :: VARCHAR, '' ) = '' THEN
					n.account_nature_name ELSE n.parent_name 
				END AS account_nature 
			FROM
				(
				SELECT t
					.bas_account_nature_id,
					t.parent_id,
					( SELECT p.account_nature_name FROM ets_bbam.ets_bas_account_nature_t p WHERE p.bas_account_nature_id = t.parent_id ) parent_name,
					t.account_nature_name,
					t.account_nature_name_cn,
					t.status,
					t.account_nature_type 
				FROM
					ets_bbam.ets_bas_account_nature_t t 
				) AS n 
			) bnt 
		where
			bnt.bas_account_nature_id = t.source_account_nature 
		) as sourceAccountNature,
		t.source_bank_name as sourceBankName,
		(
		SELECT
			fbt.cparty_short_name 
		FROM
			ets_bbam.finbas_bank_account_t bat,
			ets_bbam.finbas_bankacc_currency_t bct,
			ets_bbam.finbas_cparty_t fbt 
		WHERE
			bct.owner_id = bat.bank_account_id 
			AND bat.bank_id = fbt.cparty_id 
			AND bct.currency_id = t.source_acc_ccy_id 
			AND bat.bank_account_id = t.source_bank_account_id 
			AND bct.owner_type = 1 
			LIMIT 1 
		) as sourceBankShortName,
		t.source_bank_branch_name as sourceBankBranchName,
		t.source_convert_rate as sourceFXconvertRate,
		( select et.management_entity_en_name from ets.ets_bas_management_entity_t et where et.bas_management_entity_id = t.destination_mgt_entity_id  limit 1 ) as destinationEntity,
		( select ct.company_code from ets.ets_company_t ct where ct.company_id = t.destination_company_id  limit 1 ) as destinationCompanyCode,
		( select bt.bank_account_no from ets_bbam.finbas_bank_account_t bt where bt.bank_account_id = t.destination_bank_account_id  limit 1 ) as destinationBankAccountNo,
		( select currency_code from ets.ets_mdm_rm_currency_info_t ct where ct.currencies_id = t.destination_acc_ccy_id limit 1 ) destinationAccountCurrency,
		round( t.destination_amount, 2 ) as destinationAmount,
		(
		select
			bnt.account_nature 
		from
			(
			SELECT
				n.bas_account_nature_id,
				n.account_nature_name,
			CASE
					
					WHEN coalesce ( n.parent_id :: VARCHAR, '' ) = '' THEN
					n.account_nature_name ELSE n.parent_name 
				END AS account_nature 
			FROM
				(
				SELECT t
					.bas_account_nature_id,
					t.parent_id,
					( SELECT p.account_nature_name FROM ets_bbam.ets_bas_account_nature_t p WHERE p.bas_account_nature_id = t.parent_id ) parent_name,
					t.account_nature_name,
					t.account_nature_name_cn,
					t.status,
					t.account_nature_type 
				FROM
					ets_bbam.ets_bas_account_nature_t t 
				) AS n 
			) bnt 
		where
			bnt.bas_account_nature_id = t.destination_account_nature 
		) as destinationAccountNature,
		t.destination_bank_name as destinationBankName,
		(
		SELECT
			fbt.cparty_short_name 
		FROM
			ets_bbam.finbas_bank_account_t bat,
			ets_bbam.finbas_bankacc_currency_t bct,
			ets_bbam.finbas_cparty_t fbt 
		WHERE
			bct.owner_id = bat.bank_account_id 
			AND bat.bank_id = fbt.cparty_id 
			AND bct.currency_id = t.destination_acc_ccy_id 
			AND bat.bank_account_id = t.destination_bank_account_id 
			AND bct.owner_type = 1 
			LIMIT 1 
		) as destinationBankShortName,
		t.destination_bank_branch_name as destinationBankBranchName,
		t.destination_convert_rate as destinationFXconvertRate,
		( select u.lname from fnd.tpl_user_t u where u.user_id = t.created_by limit 1 ) as dealer,
		to_char( t.deal_date, 'yyyy-MM-dd' ) as dealDate,
		to_char( t.creation_date, 'yyyy-MM-dd hh24:mi:ss' ) as createTime,
		t.payment_method as paymentMethod,
		t.document_no as documentNo,
		t.deal_date,
	CASE
			
			WHEN t.fund_transfer_reason = 0 THEN
			'Drawdown' 
			WHEN t.fund_transfer_reason = 1 THEN
			'Repayment' 
			WHEN t.fund_transfer_reason = 2 THEN
			'' 
		END AS fundTransferReason,
	CASE
			
			WHEN t.print_Status = '0' THEN
			'Unprinted' 
			WHEN t.print_Status = '1' THEN
			'Printed' ELSE'' 
		END AS printStatus,
		t.reprint_reason reprintReason
		,t.source_mgt_entity_id AS source_mgt_entity_id
	from
		ets.ets_deal_fund_transfer_apply_t t,
		ets.ets_bas_management_entity_t me 
		-- 新增关联表
		,(SELECT distinct lit.item_name
			 FROM fnd.tpl_lookup_item_t lit 
			WHERE 1=1 
				AND lit.classify_code = 'RECEIVE_I18NSTATUS' 
				AND lit.app_name = 'etreasury' 
				) lit
	where
	
		me.bas_management_entity_id = t.source_mgt_entity_id
		-- 关联查询条件
		and t.receive_wf_status = lit.item_name 
-- 逻辑修改,改成关联查询
-- 		and exists ( SELECT 1
-- 		               FROM fnd.tpl_lookup_item_t lit 
-- 		              WHERE t.receive_wf_status = lit.item_name 
-- 									  AND lit.classify_code = 'RECEIVE_I18NSTATUS' 
-- 										AND lit.app_name = 'etreasury' 
-- 										) 
										
		and t.source_id != 2 
		and t.channel_delete_flag = 0 

-- 之前的快的		
-- 		me.bas_management_entity_id = t.source_mgt_entity_id 
-- 			and exists ( SELECT 1 FROM fnd.tpl_lookup_item_t lit 
-- 			              WHERE t.workflow_status = lit.item_name 
-- 										AND lit.classify_code = 'FUNDTRANSFER_I18NSTATUS' 
-- 										AND app_name = 'etreasury' ) 
-- 			and t.source_id != 2 
-- 			and t.channel_delete_flag = 0 

-- 逻辑修改,往下面放
-- 		and t.source_mgt_entity_id not in (
-- 						'229',
-- 						'247',
-- 						'351',
-- 						'230',
-- 						'506',
-- 						'112',
-- 						'224',
-- 						'231',
-- 						'233',
-- 						'221',
-- 						'1845',
-- 						'294',
-- 						'2145',
-- 						'204' 
-- 					) 

	)

,ttmp AS (

	SELECT
		TA.* 
	FROM
		(
		
		SELECT
			TWL.ASSIGNEE,
			
			(
			SELECT coalesce
				( MAX ( TFM.MESSAGE ), TWL.TASK_DEF_NAME ) 
			FROM
				fnd.TPL_FD_MESSAGE_T TFM 
			WHERE
				TFM.MESSAGE_KEY = TWL.TASK_DEF_NAME 
				AND TFM.LANGUAGE = 'en_US' 
				AND TFM.APP_NAME = 'etreasury' 
			) TASK_NAME,
			
			PROCESS_INSTANCE_ID AS PROC_INST_ID_,
			TWL.CREATION_DATE,
			ROW_NUMBER ( ) OVER ( PARTITION BY TWL.PROCESS_INSTANCE_ID, TWL.TASK_DEF_NAME ORDER BY TWL.CREATION_DATE DESC ) RN 
		FROM
			EWF.TPL_WF_LOG_T TWL 
		WHERE
			TWL.TYPE = 0 
			AND TWL.PROCESS_INSTANCE_ID IN ( SELECT t.procInstId FROM t ) 
		) TA 
	WHERE
		RN = 1 
	)
	
	, ttmp2 AS (
	
	select
		ttmp.PROC_INST_ID_,
	CASE
			
			WHEN ttmp.TASK_NAME = 'Supervisor' THEN
			( ttmp.ASSIGNEE || ',' || TO_CHAR( ttmp.CREATION_DATE, 'yyyy-mm-dd hh24:mi:ss' ) ) ELSE'' 
		END as supervisor,
	CASE
			
			WHEN ttmp.TASK_NAME = 'Approver' THEN
			( ttmp.ASSIGNEE || ',' || TO_CHAR( ttmp.CREATION_DATE, 'yyyy-mm-dd hh24:mi:ss' ) ) ELSE'' 
		END as Approver,
	CASE
			
			WHEN ttmp.TASK_NAME = 'Accountant' THEN
			( ttmp.ASSIGNEE || ',' || TO_CHAR( ttmp.CREATION_DATE, 'yyyy-mm-dd hh24:mi:ss' ) ) ELSE'' 
		END AS ACCOUNTANT,
	CASE
			
			WHEN ttmp.TASK_NAME = 'Accountant Reviewer' THEN
			( ttmp.ASSIGNEE || ',' || TO_CHAR( ttmp.CREATION_DATE, 'yyyy-mm-dd hh24:mi:ss' ) ) ELSE'' 
		END as accountantReviewer,
	CASE
			
			WHEN ttmp.TASK_NAME = 'Actual Pay Cashier' THEN
			( ttmp.ASSIGNEE || ',' || TO_CHAR( ttmp.CREATION_DATE, 'yyyy-mm-dd hh24:mi:ss' ) ) ELSE'' 
		END as actualPayCashier,
	CASE
			
			WHEN ttmp.TASK_NAME = 'Local Cashier' THEN
			( ttmp.ASSIGNEE || ',' || TO_CHAR( ttmp.CREATION_DATE, 'yyyy-mm-dd hh24:mi:ss' ) ) ELSE'' 
		END as localCashier,
	CASE
			
			WHEN ttmp.TASK_NAME = 'eBanking Group C' THEN
			( ttmp.ASSIGNEE || ',' || TO_CHAR( ttmp.CREATION_DATE, 'yyyy-mm-dd hh24:mi:ss' ) ) ELSE'' 
		END as eBankingGroupC,
	CASE
			
			WHEN ttmp.TASK_NAME = 'eBanking Group B' THEN
			( ttmp.ASSIGNEE || ',' || TO_CHAR( ttmp.CREATION_DATE, 'yyyy-mm-dd hh24:mi:ss' ) ) ELSE'' 
		END as eBankingGroupB,
	CASE
			
			WHEN ttmp.TASK_NAME = 'eBanking Group A' THEN
			( ttmp.ASSIGNEE || ',' || TO_CHAR( ttmp.CREATION_DATE, 'yyyy-mm-dd hh24:mi:ss' ) ) ELSE'' 
		END as eBankingGroupA,
	CASE
			
			WHEN ttmp.TASK_NAME = 'Bill Group B' THEN
			( ttmp.ASSIGNEE || ',' || TO_CHAR( ttmp.CREATION_DATE, 'yyyy-mm-dd hh24:mi:ss' ) ) ELSE'' 
		END AS billGroupB,
	CASE
			
			WHEN ttmp.TASK_NAME = 'Bill Group A' THEN
			( ttmp.ASSIGNEE || ',' || TO_CHAR( ttmp.CREATION_DATE, 'yyyy-mm-dd hh24:mi:ss' ) ) ELSE'' 
		END as billGroupA 
	from
		ttmp 
	)

	,t2 AS (
	
	SELECT
		proc_inst_id_,
		( regexp_match ( approver :: text, '[^,]+' ) ) [ 1 ] AS approver,
		( SELECT ( regexp_matches( approver :: text, '[^,]+', 'g' ) ) [ 1 ] LIMIT 1 OFFSET 2-1 ) AS approverTime,
		( regexp_match ( accountant :: text, '[^,]+' ) ) [ 1 ] AS accountant,
		( SELECT ( regexp_matches( accountant :: text, '[^,]+', 'g' ) ) [ 1 ] LIMIT 1 OFFSET 2-1 ) AS accountantSignTime,
		( regexp_match ( accountantReviewer :: text, '[^,]+' ) ) [ 1 ] AS reviewer,
		( SELECT ( regexp_matches( accountantReviewer :: text, '[^,]+', 'g' ) ) [ 1 ] LIMIT 1 OFFSET 2-1 ) AS reviewerSignTime,
		( regexp_match ( actualPayCashier :: text, '[^,]+' ) ) [ 1 ] AS cashier,
		( SELECT ( regexp_matches( actualPayCashier :: text, '[^,]+', 'g' ) ) [ 1 ] LIMIT 1 OFFSET 2-1 ) AS cashierSignTime,
		( regexp_match ( localCashier :: text, '[^,]+' ) ) [ 1 ] AS localCashier,
		( SELECT ( regexp_matches( localCashier :: text, '[^,]+', 'g' ) ) [ 1 ] LIMIT 1 OFFSET 2-1 ) AS localCashierSignTime,
		( regexp_match ( eBankingGroupC :: text, '[^,]+' ) ) [ 1 ] AS eBankingGroupC,
		( SELECT ( regexp_matches( eBankingGroupC :: text, '[^,]+', 'g' ) ) [ 1 ] LIMIT 1 OFFSET 2-1 ) AS eBankingGroupCSignTime,
		( regexp_match ( eBankingGroupB :: text, '[^,]+' ) ) [ 1 ] AS eBankingGroupB,
		( SELECT ( regexp_matches( eBankingGroupB :: text, '[^,]+', 'g' ) ) [ 1 ] LIMIT 1 OFFSET 2-1 ) AS eBankingGroupBSignTime,
		( regexp_match ( eBankingGroupA :: text, '[^,]+' ) ) [ 1 ] AS eBankingGroupA,
		( SELECT ( regexp_matches( eBankingGroupA :: text, '[^,]+', 'g' ) ) [ 1 ] LIMIT 1 OFFSET 2-1 ) AS ebankingGroupASignTime,
		( regexp_match ( billGroupB :: text, '[^,]+' ) ) [ 1 ] AS billGroupB,
		( SELECT ( regexp_matches( billGroupB :: text, '[^,]+', 'g' ) ) [ 1 ] LIMIT 1 OFFSET 2-1 ) AS billGroupBSignTime,
		( regexp_match ( billGroupA :: text, '[^,]+' ) ) [ 1 ] AS billGroupA,
		( SELECT ( regexp_matches( billGroupA :: text, '[^,]+', 'g' ) ) [ 1 ] LIMIT 1 OFFSET 2-1 ) AS billGroupASignTime 
	FROM
		(
		SELECT
			TA.* 
		FROM
			(
			SELECT
				PROC_INST_ID_,
				MAX ( supervisor ) AS supervisor,
				MAX ( Approver ) AS Approver,
				MAX ( ACCOUNTANT ) AS ACCOUNTANT,
				MAX ( accountantReviewer ) AS accountantReviewer,
				MAX ( actualPayCashier ) AS actualPayCashier,
				MAX ( localCashier ) AS localCashier,
				MAX ( eBankingGroupC ) AS eBankingGroupC,
				MAX ( eBankingGroupB ) AS eBankingGroupB,
				MAX ( eBankingGroupA ) AS eBankingGroupA,
				MAX ( billGroupB ) AS billGroupB,
				MAX ( billGroupA ) AS billGroupA 
			FROM
				ttmp2 
			group by
				PROC_INST_ID_ 
			) TA 
		) tt 
	) 

	SELECT t
	.FUND_TRANSFER_APPLY_ID,
	t.procInstId,
	t.status,
	t.currentHandler,
	t.dealNumber,
	t.ticketNumber,
	t.instrument,
	t.paymentCurrency,
	t.paymentAmount,
	t.paymentDate,
	t.equivalentUSDAmount,
	t.urgent,
	t.wHTAmount,
	t.b2B3rdParty,
	t.b2BDealNo,
	t.sourceEntity,
	t.sourceCompanyCode,
	t.sourceBankAccountNo,
	t.sourceAccountCurrency,
	t.sourceAmount,
	t.sourceAccountNature,
	t.sourceBankName,
	t.sourceBankShortName,
	t.sourceBankBranchName,
	t.sourceFXconvertRate,
	t.destinationEntity,
	t.destinationCompanyCode,
	t.destinationBankAccountNo,
	t.destinationAccountCurrency,
	t.destinationAmount,
	t.destinationAccountNature,
	t.destinationBankName,
	t.destinationBankShortName,
	t.destinationBankBranchName,
	t.destinationFXconvertRate,
	t.dealer,
	t.dealDate,
	t.createTime,
	t2.approver,
	t2.approverTime,
	t2.accountant,
	t2.accountantSignTime,
	t2.reviewer,
	t2.reviewerSignTime,
	t2.cashier,
	t.paymentMethod,
	t.documentNo,
	t2.cashierSignTime,
	t2.localCashier,
	t2.localCashierSignTime,
	t2.eBankingGroupC,
	t2.eBankingGroupCSignTime,
	t2.eBankingGroupB,
	t2.eBankingGroupBSignTime,
	t2.eBankingGroupA,
	t2.ebankingGroupASignTime,
	t2.billGroupB,
	t2.billGroupBSignTime,
	t2.billGroupA,
	t2.billGroupASignTime,
	t.fundTransferReason,
	t.printStatus,
	t.reprintReason
	
FROM t inner join t2 
  on t.procInstId = t2.proc_inst_id_ 
 and t.source_mgt_entity_id not in (
						'229',
						'247',
						'351',
						'230',
						'506',
						'112',
						'224',
						'231',
						'233',
						'221',
						'1845',
						'294',
						'2145',
						'204' 
					) 

order by
	t.dealDate desc,
	t.FUND_TRANSFER_APPLY_ID desc

四、PostgreSQL分页,查出结果后,用with当成临时表,再进行limit 15 offset 0。直接搞得话会很卡。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tzq@2018

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值