informix 中默认值的优化查询

optimised query for default value in informix

需求:创建一个视图,其中记录来自4个表 第一个字段来自 scr,secount 来自 mxr,第三个字段默认来自 mrt..

第五个是desc,是基于语言的..条件如下 mxr 中有一个字段 lng w 需要在 data_el_nm 和 lng(of mxr) 的帮助下读取 rpr 如果没有找到记录然后在 data_el_nm 和 lng(of scr) 的帮助下读取 rpr 如果not found read with data_el_nm and default lng(which is en).. UNTILL this is handled if all three is not found 使该字段带有空格。我通过在 rpr 上放置外部来做到这一点,但这需要很多时间。任何人都可以提出优化的方法来做到这一点吗?谢谢你

CREATE VIEW "informix".abc(
cmpy_id,
lgn_id,
fld_nm,
data_el_nm,
desc
)

AS

SELECT

s.cmpy_id,
u.lgn_id,
"RTONM",
r.rto_nm,
rcs_0.long_cptn_var

FROM

scr s,
mxr u,
mrt r,
outer(rpr rcs_0)

WHERE

rcs_0.data_el_nm = r.rto_nm AND
(rcs_0.lng = u.lng OR (NOT EXISTS(select * from rpr rcs_1
where rcs_1.rc_id = rcs_0.rc_id AND rcs_1.lng = u.lng)
AND rcs_0.lng = s.bas_lng) OR (NOT EXISTS(select * from rpr rcs_2
where rcs_2.rc_id = rcs_0.rc_id AND rcs_2.lng = u.lng)
AND NOT EXISTS(select * from rpr rcs_3 where
rcs_3.rc_id = rcs_0.rc_id AND rcs_3.lng = s.bas_lng)
AND rcs_0.lng = 'en'))

UNION ALL
SELECT

s.cmpy_id,
u.lgn_id,
"RTONM",
r.rto_nm,
rcs_0.long_cptn_var

FROM

scr s,
mxr u,
mrt r,
outer(rpr rcs_0)

WHERE

rcs_0.data_el_nm = r.rto_nm AND
(rcs_0.lng = u.lng OR (NOT EXISTS(select * from rpr rcs_1
where rcs_1.rc_id = rcs_0.rc_id AND rcs_1.lng = u.lng)
AND rcs_0.lng = s.bas_lng) OR (NOT EXISTS(select * from rpr rcs_2
where rcs_2.rc_id = rcs_0.rc_id AND rcs_2.lng = u.lng)
AND NOT EXISTS(select * from rpr rcs_3 where
rcs_3.rc_id = rcs_0.rc_id AND rcs_3.lng = s.bas_lng)
AND rcs_0.lng = 'en'))

很难阅读您的查询或它们附带的解释性文本,而且我看不出 UNION ALL 的目的是什么,因为两边的查询看起来完全相同。在任何情况下,我建议您可能想探索使用嵌套 NVL()CASEDECODE() 语句的常规连接。

CREATE VIEW "informix".abc
(
    cmpy_id,
    lgn_id,
    fld_nm,
    data_el_nm,
    desc
)

AS
SELECT
    s.cmpy_id,
    u.lgn_id,
    "RTONM",
    r.rto_nm,
    NVL(mxr_lng.long_cptn_var, NVL(scr_lng.long_cptn_var, NVL(def_lng.long_cptn_var, ' ')))
FROM
    scr AS s,
    mxr AS u,
    mrt AS r,
    outer rpr AS mxr_lng,
    outer rpr AS scr_lng,
    outer rpr AS def_lng
WHERE mxr_lng.data_el_nm = r.rto_nm AND mxr_lng.lng = u.lng
  AND scr_lng.data_el_nm = r.rto_nm AND scr_lng.lng = s.bas_lng
  AND def_lng.data_el_nm = r.rto_nm AND def_lng.lng = 'en'

嵌套的 NVL() 调用将首先从 mxr 中获取 long_cptn_var,但如果它为 NULL,则为 scr,如果为 NULL,则为默认值,否则为 space.

显然我无法测试这段代码,但它应该能为您提供大致的思路,即使它在语法上并不完美。