如何在Informix上使用函数和索引来加速查询

时间:2018-09-19 13:32:34

标签: sql function indexing informix

在Informix Dbms下,我需要加快此查询的速度:

set isolation to dirty read;
SELECT NVL(hr_dati1, '---'), NVL(cm_t_stoc, -1) as type,
LPAD(cm_mod,3, 0)||LPAD(cm_col,3, 0) as coord,
NVL(cm_segmento, 0) as seg, NVL(cm_lres, 0) 
FROM informix.artind, informix.coordman
LEFT OUTER JOIN informix.hmdescr ON cm_t_stoc = hr_key_soc AND 
hr_key_pref = 'MGZ' AND hr_key_suff = 'STOCC'
WHERE cm_magaz = '5' AND ad_code = '2065498' AND 
((cm_code = '2065498') OR
((cm_code = ad_coor_r) AND ( SUBSTR(NVL(ad_coor_r, ' '), 1, 2) = 'MU' )
AND
( SUBSTR(NVL(cm_code, ' '), 1, 2) = 'MU' ) ))
ORDER BY type, seg, coord;

当我用作 仅过滤

(cm_code = '2065498')

代替

((cm_code = '2065498') OR
((cm_code = ad_coor_r) AND ( SUBSTR(NVL(ad_coor_r, ' '), 1, 2) = 'MU')      
AND
( SUBSTR(NVL(cm_code, ' '), 1, 2) = 'MU' ) ))

但是现在我需要添加这个新条件。

在寻求优化的过程中,我认为运行缓慢是由于我创建了此函数的substr:

create function informix.substr2_ad_coor_r(coor_r char(8))
  returning char(8) with (NOT VARIANT);
  return substr(coor_r, 1, 2);
end function;

和这些索引:

create index informix.artind_idx_sub_coor_r on informix.artind
 ( informix.substr2_ad_coor_r(ad_coor_r));

create index informix.coordman_idx_sub_codifa on informix.coordman
 ( informix.substr2_ad_coor_r(cm_codifa));

但是我没有足够提高速度,同样奇怪的是,如果在过滤条件下使用substr例如(SUBSTR(NVL(ad_coor_r, ' '), 1, 2) = 'MU')而不是substr2_ad_coor_r,则查询看起来更快。 substr2_ad_coor_r(ad_coor_r) = 'MU'

1 个答案:

答案 0 :(得分:0)

我通过使用联合将查询分为2个来加快查询速度:

set isolation to dirty read;
SELECT NVL(hr_dati1, '---'), NVL(cm_t_stoc, -1) as type,
LPAD(cm_mod,3, 0)||LPAD(cm_col,3, 0) as coord,
NVL(cm_segmento, 0) as seg, NVL(cm_lres, 0)
FROM informix.artind, informix.coordman
LEFT OUTER JOIN informix.hmdescr ON cm_t_stoc = hr_key_soc AND
hr_key_pref = 'MGZ' AND hr_key_suff = 'STOCC'
WHERE cm_magaz = '5' AND ad_code = '2065498' AND
(cm_code = '2065498')
UNION
SELECT NVL(hr_dati1, '---'), NVL(cm_t_stoc, -1) as type,
LPAD(cm_mod,3, 0)||LPAD(cm_col,3, 0) as coord,
NVL(cm_segmento, 0) as seg, NVL(cm_lres, 0)
FROM informix.artind, informix.coordman
LEFT OUTER JOIN informix.hmdescr ON cm_t_stoc = hr_key_soc AND
hr_key_pref = 'MGZ' AND hr_key_suff = 'STOCC'
WHERE cm_magaz = '5' AND ad_code = '2065498' AND
((cm_code = ad_coor_r) AND ( SUBSTR(NVL(ad_coor_r, ' '), 1, 2) = 'MU' )
AND
( SUBSTR(NVL(cm_code, ' '), 1, 2) = 'MU' ) )
ORDER BY 1, 3, 2;