问题描述:
项目反馈某功能响应时间很长,高峰期时系统整体响应很慢。。。
获取相应的AWR,问题确实比较严重,latch: cache buffers chains等待,因为这些会话SQL执行时间太长,并发情况下数据库连接数迅速增长,从应用服务器的性能计数器上确实可以看到数据库连接激增的情况。
原因分析:
Oracle使用WHERE C1='' OR C2 IN(SubQuery)的方式会造成索引失效问题,以下是测试示例:
create table tkk22
as
select * from user_tables t; alter table tkk22 add constraint pk_tkk22 primary key(table_name);
create index idx_tkk22_rows on tkk22(num_rows); analyze table tkk22 compute statistics; -- 全表扫描
select *
from tkk22 t
where t.num_rows = 37 or t.num_rows in (select 10 from dual); -- 局部索引查找
select *
from tkk22 t
where t.num_rows = 37 or t.num_rows in (10); -- OR
select * from tkk22 t where t.num_rows = 37
union
select * from tkk22 t where t.num_rows in (select 10 from dual);
解决方案:
1、将子查询的结果先取出,然后再静态编入到IN()条件;
2、把两个条件的OR,改为UNION
select *
from tkk22 t
where t.num_rows = 37 or t.num_rows in (10); -- OR
select * from tkk22 t where t.num_rows = 37
union
select * from tkk22 t where t.num_rows in (select 10 from dual);