摘要:在业务功能实现时,经常会用到视图简化查询SQL。但有时候会因为视图降低查询效率,本文主要分析在业务需求满足的情况下,将有效的过滤条件传递到基表,减少运算过程中数据库需要处理的数据量,提升SQL执行效率。
本文分享自华为云社区《GaussDB(DWS)业务视图优化-过滤条件传递》,作者:卫小毛 。
在业务功能实现时,经常会用到视图简化查询SQL。但有时候会因为视图降低查询效率,本文主要分析在业务需求满足的情况下,将有效的过滤条件传递到基表,减少运算过程中数据库需要处理的数据量,提升SQL执行效率。
SQL举例
SELECT
count(1) AS have_done_num,
t1.task_def_key_ AS menuguid
FROM
vw_pay_voucher_bill t2
LEFT JOIN xact_hi_taskinst t1 ON t1.business_key_ = t2.id
AND t1.proc_def_key_ = 'pay_voucher_bill'
AND t1.operation_flag_ IN ('NORMAL', 'WITHDRAW')
AND t1.suspension_state_ = 1
AND t1.org_code_ = t2.mof_div_code
AND delete_reason_ = 'completed'
AND ext1_ IS NULL
WHERE
t2.is_deleted = '2'
AND t2.fiscal_year = '2022'
AND t2.mof_div_code = 'xxxxxxxx0'
AND (
agency_id = '5A1xxxxxxxxxxxxxxxxxxx4T5'
)
GROUP BY
t1.task_def_key_
HAVING
t1.task_def_key_ IS NOT NULL;