在我的一个项目中,我正在使用 dblink 对 View 进行直接查询,但经过性能检查后发现它的成本很高(很多嵌套循环,高 CPU 使用)。
现在我们找到了一种解决方案,即使用函数在视图中触发这些查询。那么它是正确的解决方案还是有更好的方法来执行此操作。
示例查询和性能报告:
- 不使用函数:
询问 :
SELECT t1.,
t2.
FROM table1 t1
join table2 t2
ON t1.id = t2.id
WHERE t1.bookingId = '0250014547'
业绩报告:
SQL> explain plan for SELECT t1., t2. FROM table1 t1 JOIN table2 t2 ON t1.id = t2.id WHERE t1.bookingId = '0250014547';
SQL> select * from table(dbms_xplan.display);
-------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 331 | 6 (0)| 00:00:01 |
| 1 | NESTED LOOPS | | 1 | 331 | 6 (0)| 00:00:01 |
| 2 | NESTED LOOPS | | 1 | 299 | 5 (0)| 00:00:01 |
| 3 | NESTED LOOPS | | 1 | 210 | 4 (0)| 00:00:01 |
| 4 | NESTED LOOPS | | 1 | 185 | 3 (0)| 00:00:01 |
| 5 | NESTED LOOPS | | 1 | 156 | 2 (0)| 00:00:01 |
|* 6 | TABLE ACCESS BY INDEX ROWID| GENERAL_ACCT_MAST_TABLE | 1 | 66 | 1 (0)| 00:00:01 |
|* 7 | INDEX UNIQUE SCAN | IDX_GAM_FORACID | 1 | | 1 (0)| 00:00:01 |
| 8 | TABLE ACCESS BY INDEX ROWID| SERVICE_OUTLET_TABLE | 1694 | 148K| 1 (0)| 00:00:01 |
|* 9 | INDEX UNIQUE SCAN | IDX_SERVICE_OUTLET_TABLE | 1 | | 1 (0)| 00:00:01 |
| 10 | TABLE ACCESS BY INDEX ROWID | GEN_SCHM_PARM_TABLE | 356 | 10324 | 1 (0)| 00:00:01 |
|* 11 | INDEX UNIQUE SCAN | IDX_GEN_SCHM_PARM_TABLE | 1 | | 1 (0)| 00:00:01 |
|* 12 | TABLE ACCESS BY INDEX ROWID | ACCT_STATEMENT_TABLE | 12M| 309M| 1 (0)| 00:00:01 |
|* 13 | INDEX UNIQUE SCAN | IDX_ACCT_STATEMENT_TABLE | 1 | | 1 (0)| 00:00:01 |
| 14 | TABLE ACCESS BY INDEX ROWID | CUST_MAST_GEN_TABLE | 24M| 2119M| 1 (0)| 00:00:01 |
|* 15 | INDEX UNIQUE SCAN | IDX_CUST_MAST_GEN_TABLE | 1 | | 1 (0)| 00:00:01 |
| 16 | TABLE ACCESS BY INDEX ROWID | BRANCH_BIC_TABLE | 65258 | 2039K| 1 (0)| 00:00:01 |
|* 17 | INDEX UNIQUE SCAN | IDX_BRANCH_BIC_TABLE | 1 | | 1 (0)| 00:00:01 |
-------------------------------------------------------------------------------------------
- 使用功能:
询问:
SELECT Funquery1('0250014547')
FROM dual;
业绩报告:
SQL> explain plan for select gtt('0252050014577') from dual;
SQL> select * from table(dbms_xplan.display);
-----------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
-----------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 2 (0)| 00:00:01 |
| 1 | FAST DUAL | | 1 | 2 (0)| 00:00:01 |
-----------------------------------------------------------------