今天用户反馈有条sql语句过滤性非常好,达梦很慢,oracle非常快。
模拟sql如下:
create table t(id int,c1 date);
insert into t
select level,sysdate-FLOOR(RAND() * 1000) from SYS.SYSDUAL CONNECT by level<=10000;
COMMIT;
create index uix_c1 on T(c1);
CREATE OR REPLACE FUNCTION SY_NOW
RETURN timestamp AUTHID DEFINER
as
begin
return sysdate;
end;
select * from t where c1>SY_NOW-1; --慢,走全表扫描
1 #NSET2: [1, 500, 29]
2 #PRJT2: [1, 500, 29]; exp_num(3), is_atom(FALSE)
3 #SLCT2: [1, 500, 29]; T.C1 > exp11-var1
4 #CSCN2: [1, 10000, 29]; INDEX33555465(T); btr_scan(1)
select * from t where c1>sysdate-1; --走索引
1 #NSET2: [1, 500, 29]
2 #PRJT2: [1, 500, 29]; exp_num(3), is_atom(FALSE)
3 #BLKUP2: [1, 500, 29]; UIX_C1(T)
4 #SSEK2: [1, 500, 29]; scan_type(ASC), UIX_C1(T), scan_range(exp11-exp_cast(1),max]
难道是自定义函数导致的sql不走索引吗?这个不应该,肯定是有什么地方没注意。
进一步分析发现该自定义函数SY_NOW是非确定性函数,按照如下方法改成确定性函数:
CREATE OR REPLACE FUNCTION SY_NOW
RETURN timestamp deterministic
as
begin
return sysdate;
end;
再次验证:
性能问题解决。
文章
阅读量
获赞