0

我编写了一个视图逻辑来根据要求提取数据。我需要建议或解决方案来提高视图逻辑的性能。

表:

|SID | SREF     |  STYPE  |  SQID  | SNAME | CCID1 | CCID2 | CCID3 |
--------------------------------------------------------------------
|XX  | SREF001  |  HR     |  1     | 10001 | ABC   | DEF   | XYZ   |
|XX  | SREF001  |  HR     |  2     | 10001 | ABC   | DEF   | XYZ   |
|XX  | SREF002  |  KR     |  1     | 10001 | ABC   | DEF   | XYZ   |
|XX  | SREF002  |  KR     |  2     | 10001 | ABC   | DEF   | XYZ   |
|YY  | SREF011  |  HR     |  1     | 20001 | ABC   | DEF   | XYZ   |
|YY  | SREF011  |  HR     |  2     | 20001 | ABC   | DEF   | XYZ   |
|YY  | SREF012  |  KR     |  1     | 20001 | ABC   | DEF   | XYZ   |
|YY  | SREF012  |  KR     |  2     | 20001 | ABC   | DEF   | XYZ   |

我的观点:

CREATE VIEW MYTAB_VIEW AS
SELECT SID, SREF,
   MAX(CASE WHEN STYPE='HR' AND SQID=1 THEN SNAME END) AS HR1_SNAME,
   MAX(CASE WHEN STYPE='HR' AND SQID=1 THEN CCID1 END) AS HR1_CCID1,
   MAX(CASE WHEN STYPE='HR' AND SQID=1 THEN CCID2 END) AS HR1_CCID2,
   MAX(CASE WHEN STYPE='HR' AND SQID=1 THEN CCID3 END) AS HR1_CCID3,
   MAX(CASE WHEN STYPE='HR' AND SQID=2 THEN SNAME END) AS HR2_SNAME,
   MAX(CASE WHEN STYPE='HR' AND SQID=2 THEN CCID1 END) AS HR2_CCID1,
   MAX(CASE WHEN STYPE='HR' AND SQID=2 THEN CCID2 END) AS HR2_CCID2,
   MAX(CASE WHEN STYPE='HR' AND SQID=2 THEN CCID3 END) AS HR2_CCID3,
   MAX(CASE WHEN STYPE='KR' AND SQID=1 THEN SNAME END) AS KR1_SNAME,
   MAX(CASE WHEN STYPE='KR' AND SQID=1 THEN CCID1 END) AS KR1_CCID1,
   MAX(CASE WHEN STYPE='KR' AND SQID=1 THEN CCID2 END) AS KR1_CCID2,
   MAX(CASE WHEN STYPE='KR' AND SQID=1 THEN CCID3 END) AS KR1_CCID3,
   MAX(CASE WHEN STYPE='KR' AND SQID=2 THEN SNAME END) AS KR2_SNAME,
   MAX(CASE WHEN STYPE='KR' AND SQID=2 THEN CCID1 END) AS KR2_CCID1,
   MAX(CASE WHEN STYPE='KR' AND SQID=2 THEN CCID2 END) AS KR2_CCID2,
   MAX(CASE WHEN STYPE='KR' AND SQID=2 THEN CCID3 END) AS KR2_CCID3
FROM MYTABLE
GROUP BY SID, SREF;

“MYTABLE”将有数百万的记录。因此,视图逻辑成为生产中最大的性能打击。我有 STYPE、SQID、SNAME、CCID1、CCID2、CCID3 的索引。但是还是很慢...

执行时间需要 105 秒,现在!!

请建议我如何提高性能...

更新:请不要建议MaterializedView

谢谢!

4

0 回答 0