在plsql中执行一个sql语句,视图查询 耗时1秒左右,但是在程序中 使用Criteria 分页查询 需要耗时80多秒,求指教,,,,
如题.......贴上代码:
public Pager findByPager(Session session, Pager pager, int type) {
long s = System.currentTimeMillis();
System.out.println("position....1.1:"+s);
if (pager == null) {
pager = new Pager();
}
Map<String, SearchEntity> map = parseMethod(pager.getEntity()); // 要拼接的信息
Integer pageNum = pager.getPageNum();
Integer pageSize = pager.getPageSize();
String orderBy = pager.getOrderBy();
OrderType orderType = pager.getOrderType();
Criteria criteria = session.createCriteria(pager.getEntity().getClass());
criteria.add(Restrictions.sqlRestriction(" 1=1 "));
//加入分公司编号条件
if (type == 1) {
criteria.add(Restrictions.sqlRestriction(" filialenum = "+pager.getFiliale_num()+" "));
}
String carteId = (String) ServletActionContext.getRequest().getAttribute("carteId");
criteria = getWhere(criteria, map);
ViewAdminInfo adminInfo = (ViewAdminInfo) ActionContext.getContext().getSession().get("user");
long s1 = System.currentTimeMillis();
System.out.println("position....1.2:"+s1);
// 介入数据权限
if (null != adminInfo.getIsAdmin() && adminInfo.getIsAdmin() != 0)
if (carteId != null && !"".equals(carteId)) {
Integer id = adminInfo.getId();
SQLQuery query = session
.createSQLQuery("select filed_name,sql_statement,sql_value from data_authority dt"
+ " left join dataauthority_mapping dtm on dt.id = dtm.data_id"
+ " where dtm.admin_id = "
+ id
+ " and dtm.carte_id = "
+ carteId
+ " and is_using = 1");
List dtms = query.list();
for (Object obj : dtms) {
Object[] arr = (Object[]) obj;
String filedName = (String) arr[0];
String sqlStatement = (String) arr[1];
String sqlValue = (String) arr[2];
Integer intValue = null;
boolean flag = false;
try {
intValue = Integer.parseInt(sqlValue);
} catch (NumberFormatException e) {
flag = true;
}
if (sqlStatement.contains(">=")) {
if (flag)
criteria.add(Restrictions.ge(filedName, sqlValue));
else
criteria.add(Restrictions.ge(filedName, intValue));
} else if (sqlStatement.contains("<=")) {
if (flag)
criteria.add(Restrictions.le(filedName, sqlValue));
else
criteria.add(Restrictions.le(filedName, intValue));
} else if (sqlStatement.contains(">")) {
if (flag)
criteria.add(Restrictions.gt(filedName, sqlValue));
else
criteria.add(Restrictions.gt(filedName, intValue));
} else if (sqlStatement.contains("<")) {
if (flag)
criteria.add(Restrictions.lt(filedName, sqlValue));
else
criteria.add(Restrictions.lt(filedName, intValue));
} else if (sqlStatement.contains("=")) {
if (flag)
criteria.add(Restrictions.eq(filedName, sqlValue));
else
criteria.add(Restrictions.eq(filedName, intValue));
} else if (sqlStatement.contains("<>")
|| sqlStatement.contains("!=")) {
if (flag)
criteria.add(Restrictions.ne(filedName, sqlValue));
else
criteria.add(Restrictions.ne(filedName, intValue));
} else if (sqlStatement.contains(" in")) {
String[] ids = sqlValue.split("( )*,( )*");
Integer[] intIds = new Integer[ids.length];
boolean test = false;
for (int i = 0; i < ids.length; i++) {
try {
intIds[i] = Integer.parseInt(ids[i]);
} catch (NumberFormatException e) {
test = true;
break;
}
}
if (test)
criteria.add(Restrictions.in(filedName, ids));
else
criteria.add(Restrictions.in(filedName, intIds));
} else if (sqlStatement.contains("not in")) {
String[] ids = sqlValue.split("( )*,( )*");
Integer[] intIds = new Integer[ids.length];
boolean test = false;
for (int i = 0; i < ids.length; i++) {
try {
intIds[i] = Integer.parseInt(ids[i]);
} catch (NumberFormatException e) {
test = true;
break;
}
}
if (test)
criteria.add(Restrictions.not(Restrictions.in(
filedName, ids)));
else
criteria.add(Restrictions.not(Restrictions.in(
filedName, intIds)));
} else if (sqlStatement.contains(" like")) {
criteria.add(Restrictions.like(filedName, sqlValue));
}
}
}
// 结束介入数据权限
// criteria.add(Restrictions.eq( "agentNum", pager.getLoing_usre()));
// //用户
long s2 = System.currentTimeMillis();
System.out.println("position....1.3:"+s2);
Integer totalCount = pager.getTotalCount();
if (totalCount == 0) {
totalCount = (Integer) criteria.setProjection(Projections.rowCount()).uniqueResult();
}
//计算页面数
int pageno=totalCount%pageSize==0 ? totalCount/pageSize:(totalCount/pageSize+1);
//判断当前的页面上传来的页数是不是大于最后的页数 如果大于 则改变为最后一页 update:zyy
if(pageNum >= pageno){
pageNum=pageno;
}
criteria.setProjection(null);
criteria.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
criteria.setFirstResult((pageNum - 1) * pageSize);
criteria.setMaxResults(pageSize);
long s3 = System.currentTimeMillis();
System.out.println("position....1.4:"+s3);
if (orderType != null && StringUtils.isNotEmpty(orderBy) ) {
if (orderType == OrderType.asc) {
criteria.addOrder(Order.asc(orderBy));
} else if (orderType == OrderType.desc){
criteria.addOrder(Order.desc(orderBy));
}
}
long s4 = System.currentTimeMillis();
System.out.println("position....1.5:"+s4);
long s5 = System.currentTimeMillis();
System.out.println("position....1.6:"+s5);
pager.setTotalCount(totalCount);
long s6 = System.currentTimeMillis();
System.out.println("position....1.7:"+s6);
pager.setList(criteria.list());
long s7 = System.currentTimeMillis();
System.out.println("position....1.8:"+s7);
pager.setFind_condition(find_condition);
long s8 = System.currentTimeMillis();
System.out.println("position....1.9:"+s8);
return pager;
}
控制台输出:
position....1.1:1380292729102
position....1.2:1380292729113
position....1.3:1380292729113
position....1.4:1380292729927
position....1.5:1380292729936
position....1.6:1380292729936
position....1.7:1380292729936
position....1.8:1380292814912
position....1.9:1380292814912
可以看到 1.7-1.8的那一步 耗费了85秒左右,
pager.setList(criteria.list());
使用hibernate生成的语句 拿到plsql中执行的话,只需要1秒的时间就可以查询出来,不知道是为什么,求指导, 分页 plsql sql session --------------------编程问答-------------------- 额...怎么没人回复呢.... --------------------编程问答-------------------- 在各个你认为比较耗时的操作,加 log,尽量细化,把问题锁定下 --------------------编程问答-------------------- 就是list方法的问题 看是拼SQL慢还是把查询出的结果组合慢 --------------------编程问答-------------------- hibernate速度慢是正常的
补充:Java , Web 开发