当前位置:编程学习 > JAVA >>

Mybatis 调用oracle 效率问题

写了一个function qx(uid,pid)
在某个查询语句中这样写select * from customer where delflag=1 and 1=qx(#{id});
这样会查询很慢。

create or replace function qx(uId varchar2){
   return varchar2 is
 hasXq varchar2(20) :='0';
 adminCount number:=0;
begin
--是否是管理员
  select count(*) into adminCount from user_role where user_id=uId and role_id=1;
 if adminCount>0 then
     hasXq:='1';
  else
     hasXq:='0';
  end if;
  return hasXq;
end;
}

若果不用函数直接这样写select * from customer where delflag=1 and 1=( select count(*)from user_role where user_id=uId and role_id=1)就很快


若果还是想用function该怎么改,才能优化查询效率 --------------------编程问答-------------------- 能不用function 还是别用function 吧,,,非要用function 就在业务代码里做处理吧。。。 --------------------编程问答-------------------- 你以后要换其他数据库就哭吧 --------------------编程问答-------------------- 为什么不用存储过程呢?为什么不建索引呢 --------------------编程问答-------------------- 这个是SQL语句的问题了,不是MyBatis的问题。
所以需要你去优化SQL --------------------编程问答-------------------- sql 执行计划看看。  --------------------编程问答--------------------
引用 3 楼 fangmingshijie 的回复:
为什么不用存储过程呢?为什么不建索引呢


这个function的作用很简单,就只是查询当前用户是不是管理员,就一句sql.
相当于在select * from customer后面加了一个where条件
若果用存储过程,应该咋写?才能实现相同的功能? --------------------编程问答--------------------
引用 4 楼 Inhibitory 的回复:
这个是SQL语句的问题了,不是MyBatis的问题。
所以需要你去优化SQL



这个function的作用很简单,就只是查询当前用户是不是管理员,就一句sql.
相当于在select * from customer后面加了一个where条件。。。
还能咋优化???
我把函数里的sql考到mybaties里就很快了。。
补充:Java ,  Web 开发
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,