1.传递一组agent_id 提高SQL性能,不要一次传递一个agent_id
select agent_id agentId ,count(service_type) counts, sum(time_to_sec(end_time)-time_to_sec
(start_time)) seconds from et_service_log where service_type=? and site_id=? and agent_id in (?, ?,
?, ?, ?, ?, ?, ?, ?, ?) group by agent_id
SQLQuery sqlQuery = this.getSession().createSQLQuery(conditionSql);
sqlQuery.setParameterList("agentId", agentIdList);
2.主从明细表关系查询优化
普遍方法是 先查询主表IDLIST,再通过每个ID,获取一组明细表中的数据
优化方法是 主从表 LEFT JOIN 一次执行就可以把相关数据RESRARCH
CODE :
Criteria criteria = createCriteria(MessageCategory.class)
.add(Restrictions.eq("entityPk.siteId", siteId))
.add(Restrictions.eq("deleted", false));
criteria.createAlias("messageContentSet",
"messageContentSet",JoinFragment.LEFT_OUTER_JOIN).setFetchMode("messageContentSet",
FetchMode.JOIN).add(
Restrictions.eq("messageContentSet.deleted", false)).addOrder
(Order.desc("entityPk.id"));
List listData=criteria.list();
Set set =new HashSet(listData); (* 过滤重复数据)
------君临天下,舍我其谁
------