语句主要是view与view 做union 其中一段view 执行缓慢 加上 and (a.药房序号I = '14' or '14' = '0') 整个语句耗时很长,原因就
出在这里 ,不加条件 1s内响应,加上and (a.药房序号I = '14' or '14' = '0')执行160多s
SQL> explain plan for select sum(case when 药品类型i = 1 then
2 A.变化后库存数量 * A.成本单价
3 else
4 0
5 end) 西药成本,
6 SUM(case
7 when 药品类型i = 1 then
8 A.变化后库存数量 * A.零售单价
9 else
10 0
11 end) 西药零售,
12 sum(case
13 when (药品类型i = 2 or 药品类型i = 4) then
14 A.变化后库存数量 * A.成本单价
15 else
16 0
17 end) 中成药成本,
18 SUM(case
19 when (药品类型i = 2 or 药品类型i = 4) then
20 A.变化后库存数量 * A.零售单价
21 else
22 0
23 end) 中成药零售,
24 sum(A.变化后库存数量 * A.成本单价) As 西药成本合计,
25 sum(A.变化后库存数量 * A.零售单价) As 西药零售合计,
26 0 中药成本合计,
27 0 中药零售合计
28 from dug_d西药房库存变化明细 A
29 inner join (select 批次序号i,
30 药房序号I,
31 药品序号I,
32 max(系统序号) As 系统序号
33 from dug_d西药房库存变化明细
34 where 操作时间 <=
35 to_date('2019-03-31 23:59:59',
36 'yyyy-mm-dd hh24:mi:ss')
37 and 隶属机构I = '1'
38 group by 药房序号I, 药品序号I, 批次序号i) B
39 on A.系统序号 = B.系统序号
40 inner join doc_t药品目录_价格 bb
41 on bb.系统序号 = b.药品序号i
42 inner join doc_t药品目录 cc
43 on cc.系统序号 = bb.药品目录序号i
44 inner join doc_t药典目录 dd
45 on dd.系统序号 = cc.药典序号i
46 where 药品类型i != 5
47 and (a.药房序号I = '14' or '14' = '0');
已解释。
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 2986645705
--------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 114 | 3 (34)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 114 | | |
| 2 | NESTED LOOPS | | 1 | 114 | 3 (34)| 00:00:01 |
| 3 | NESTED LOOPS | | 1 | 114 | 3 (34)| 00:00:01 |
| 4 | NESTED LOOPS | | 1 | 107 | 3 (34)| 00:00:01 |
| 5 | NESTED LOOPS | | 1 | 99 | 2 (50)| 00:00:01 |
| 6 | NESTED LOOPS | | 1 | 91 | 2 (50)| 00:00:01 |
| 7 | VIEW | | 1 | 26 | 2 (50)| 00:00:01 |
| 8 | HASH GROUP BY | | 1 | 74 | 2 (50)| 00:00:01 |
|* 9 | TABLE ACCESS BY INDEX ROWID| DUG_D西药房库存变| 1 | 74 | 1 (0)| 00:00:01 |
|* 10 | INDEX RANGE SCAN | IDX_112598_隶属机| 1 | | 1 (0)| 00:00:01 |
|* 11 | TABLE ACCESS BY INDEX ROWID | DUG_D西药房库存变| 1 | 65 | 0 (0)| 00:00:01 |
|* 12 | INDEX RANGE SCAN | IDX_202315_药房序| 1 | | 0 (0)| 00:00:01 |
| 13 | TABLE ACCESS BY INDEX ROWID | DOC_T药品目录_价 | 1 | 8 | 0 (0)| 00:00:01 |
|* 14 | INDEX UNIQUE SCAN | PK_T药品目录_价格| 1 | | 0 (0)| 00:00:01 |
|* 15 | TABLE ACCESS BY INDEX ROWID | DOC_T药品目录 | 1 | 8 | 1 (0)| 00:00:01 |
|* 16 | INDEX UNIQUE SCAN | PK_T药品目录 | 1 | | 0 (0)| 00:00:01 |
|* 17 | INDEX UNIQUE SCAN | PK_T药典目录 | 1 | | 0 (0)| 00:00:01 |
|* 18 | TABLE ACCESS BY INDEX ROWID | DOC_T药典目录 | 1 | 7 | 0 (0)| 00:00:01 |
--------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
9 - filter("操作时间"<=TO_DATE(' 2019-03-31 23:59:59', 'syyyy-mm-dd hh24:mi:ss'))
10 - access("隶属机构I"=1)
11 - filter("A"."系统序号"="B"."系统序号")
12 - access("A"."药房序号I"=14)
14 - access("BB"."系统序号"="B"."药品序号I")
15 - filter("CC"."药典序号I" IS NOT NULL)
16 - access("CC"."系统序号"="BB"."药品目录序号I")
17 - access("DD"."系统序号"="CC"."药典序号I")
18 - filter("DD"."药品类型I"<>5)
已选择38行。
SQL> spool off;
其中id =10 走INDEX RANGE SCAN
|* 10 | INDEX RANGE SCAN | IDX_112598_隶属机| 1 | | 1 (0)| 00:00:01 |
对比索引选择性是否合理
索引选择性不高, 果然不适合走索引,删除 drop index IDX_202315_药房序号I
后,语句4s左右执行完
索引创建一定要判断列是否合适建索引,进而避免造成走错执行计划