0

现有查询:

SELECT   
 SUBSTR(B.C_TYP, 1, 4)                        
,SUBSTR(B.X_DESC, 1, 25)                      
,SUBSTR(A.C_TYP, 5, 4)                        
,SUBSTR(A.X_DESC, 1, 25)                      
,SUBSTR(A.X_DESC, 82, 11) FROM     M.MASCTAB A, M.MASCTAB B WHERE    
       A.I_PLT = ?
AND    A.N_TBL = ?                                
AND    A.C_TYP LIKE ?                          
AND    A.I_PLT = B.I_PLT                            
AND    SUBSTR(A.C_TYP, 1, 4) = SUBSTR(B.C_TYP, 1, 4)
AND    SUBSTR(A.X_DESC, 81, 1) = 'W'                
AND    B.N_TBL = 'DEPTC'                            
ORDER    BY A.C_TYP

修改后的查询:

SELECT 

(SELECT SUBSTR(C_TYP, 1, 4)
FROM M.MASCTAB
WHERE I_PLT = A.I_PLT
AND N_TBL = 'DEPTC'
AND LEFT(C_TYP, 4) = LEFT(A.C_TYP, 4) 
)
,(SELECT SUBSTR(X_DESC, 1, 25) 
FROM M.MASCTAB 
WHERE I_PLT = A.I_PLT 
AND N_TBL = 'DEPTC' 
AND LEFT(C_TYP, 4) = LEFT(A.C_TYP, 4) 
),SUBSTR(A.C_TYP, 5, 4) 
,SUBSTR(A.X_DESC,1, 25) 
,SUBSTR(A.X_DESC, 82, 11) 
FROM M.MASCTAB A
WHERE A.I_PLT = ? 
AND A.N_TBL = ? 
AND A.C_TYP LIKE ? 
AND SUBSTR(A.X_DESC, 81, 1) = 'W' 
ORDER BY A.C_TYP 
WITH UR

优化查询后,具有准确的结果集,部署代码后面临问题。错误 _jspService 错误类 java.lang.NullPointerException

会有什么问题?请帮忙。

4

2 回答 2

0

我认为你的select (select ...), (select ...), .... From ....). 只要您的两个子查询准确返回 1 或 0 结果,查询才会成功。

查看您的初始查询,它看起来非常简单或无辜。不过,您可能可以优化一些事情。

  1. A.C_TYP LIKE ?- 类似的运算符很危险。这可能会降低查询的性能。从您提供该查询的程序中,尽量避免使用 '%' 位于字符串开头的构造。
  2. 你使用索引吗?- 为 I_PLT、N_TBL 创建一个(包含两个值的索引)
  3. 考虑在表中创建包含 SUBSTR(C_TYP, 1, 4) 和 SUBSTR(X_DESC, 81, 1) = 'W' 值的新列。您将存储冗余数据,但您不需要一遍又一遍地计算值。您应该创建一个触发每个插入和更新的触发器,以填充搜索的两列。如果你读的比写的多,这将提高性能。
  4. 临时表公用表表达式也可能是一个选项。请参阅下面的查询。(免责声明:查询未针对性能和语法正确性进行测试)

with temp_table as 
   (select C_TYP, X_DESC, I_PLT, N_TBL, SUBSTR(C_TYP, 1, 4) as join_key
      from M.MASCTAB) 
select B.join_key,
       SUBSTR(B.X_DESC, 1, 25),
       SUBSTR(A.C_TYP, 5, 4),
       SUBSTR(A.X_DESC, 1, 25),
       SUBSTR(A.X_DESC, 82, 11) 
FROM temp_table A inner join temp_table B on A.join_key=B.join_key
WHERE  A.I_PLT = ?
AND    A.N_TBL = ?                                
AND    A.C_TYP LIKE ?                          
AND    A.I_PLT = B.I_PLT                            
AND    SUBSTR(A.X_DESC, 81, 1) = 'W'                
AND    B.N_TBL = 'DEPTC'                            
ORDER    BY A.C_TYP
于 2013-11-11T16:15:07.853 回答
0

看来您正在尝试使用公用表表达式,但语法并不完整。根据您原始查询的内容,并保持修改后查询的结构,您修改后的查询看起来应该是这样的:

with A as
( SELECT I_PLT
        ,N_TBL
        ,SUBSTR(C_TYP, 1, 4) MAIN_TYPE
        ,SUBSTR(C_TYP, 5, 4) SUB_TYPE
        ,SUBSTR(X_DESC, 1, 25) X_DESC_PT1
        ,SUBSTR(X_DESC,82, 11) X_DESC_PT2
    FROM M.MASCTAB 
    WHERE I_PLT = ? 
      AND N_TBL = ? 
      AND C_TYP LIKE ? 
      AND SUBSTR(A.X_DESC, 81, 1) = 'W' 
), B as 
( SELECT I_PLT
        ,SUBSTR(C_TYP, 1, 4)     MAIN_TYPE
        ,SUBSTR(X_DESC, 1, 25)   DESC_PT1
FROM M.MASCTAB
WHERE N_TBL = 'DEPTC'
) 
SELECT B.MAIN_TYPE
      ,B.DESC_1
      ,A.SUB_TYPE
      ,A.DESC_1
      ,A.DESC_2
  FROM A
  JOIN B
          ON A.I_PLT   = B.I_PLT                            
     AND A.MAIN_TYPE = B.MAIN_TYPE 
  ORDER BY MAIN_TYPE,SUB_TYPE 
WITH UR
于 2013-11-11T17:18:36.420 回答