We’re using Criteria frequently in our project for dynamic query generation. I really love the way queries are expressed. The problem is that we’ve found that this particular query cannot be made to use indexes based on clientId, and so we need to change it. Here is the working non-indexed query:
public List<EventInstance> getEventInstances(Date from, Date to, Integer clientId) {
Session session = this.getSession();
Criteria criteria = session.createCriteria(EventInstance.class);
if(clientId != null){
criteria.add(Restrictions.disjunction()
.add(Restrictions.eq("clientId", clientId))
.add(Restrictions.eq("team1ClientId", clientId))
.add(Restrictions.eq("team2ClientId", clientId))
);
}
if(from != null){
criteria.add(Restrictions.ge("startTime", from));
}
if(to != null){
criteria.add(Restrictions.le("startTime", to));
}
@SuppressWarnings("unchecked")
List<EventInstance> events = criteria.list();
this.releaseSession(session);
return events;
}
This query can only use the startTime index, and is unable to use indexes with any of the clientId’s. I found that the following form of the query uses our indexes effectively, and I want to create this query with criteria:
select * from ( select * from eventInstance where (clientId = 8 or team1ClientId = 8 or team2ClientId = 8) ) evtalias where evtalias.startTime < now()
I have been able to do a sub-select in the WHERE clause with this code:
public List<EventInstance> getEventInstances(Date from, Date to, Integer clientId){
Session session = this.getSession();
DetachedCriteria subSelectClient = DetachedCriteria.forClass(EventInstance.class);
if(clientId != null){
subSelectClient.add(Restrictions.disjunction()
.add(Restrictions.eq("clientId", clientId))
.add(Restrictions.eq("team1ClientId", clientId))
.add(Restrictions.eq("team2ClientId", clientId))
)
.setProjection(Property.forName("id"));
}
Criteria criteria = session.createCriteria(EventInstance.class);
if(clientId != null){
criteria.add(Property.forName("id").in(subSelectClient));
}
if(from != null){
criteria.add(Restrictions.ge("startTime", from));
}
if(to != null){
criteria.add(Restrictions.le("startTime", to));
}
@SuppressWarnings("unchecked")
List<EventInstance> events = criteria.list();
this.releaseSession(session);
return events;
}
This generates a query like this:
select * from eventInstance this_ where this_.id in (select this_.id as y0_ from eventInstance this_ where (this_.clientId=8 or this_.team1ClientId=8 or this_.team2ClientId=8)) and this_.startTime<=now();
Which is even worse at using the indexes than my original query, and does not subselect in FROM.
So my question is, can I do this in criteria, or am I stuck with HQL or even native SQL. Alternatively if you know how to create an index that will work that would solve my problem, but my understanding from the mysql documentation is that this is impossible.
Here is the output from explain for the target query I want to create:
mysql> explain select * from ( select * from eventInstance where (clientId = 8 or team1ClientId = 8 or team2ClientId = 8) ) evtalias where evtalias.startTime < now();
+----+-------------+---------------+-------------+-------------------------------+----- ------------------+---------+------+------+------------------------------------------------ --------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------------+-------------+-------------------------------+-----------------------+---------+------+------+--------------------------------------------------------------+
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 288 | Using where |
| 2 | DERIVED | eventInstance | index_merge | eijoin2,ei_client,team2,team1 | ei_client,team1,team2 | 5,5,5 | NULL | 300 | Using union(ei_client,team1,team2); Using where; Using index |
+----+-------------+---------------+-------------+-------------------------------+-----------------------+---------+------+------+--------------------------------------------------------------+
2 rows in set (0.00 sec)
And this is the explain from hibernate’s criteria subquery:
+----+--------------------+-------+-----------------+---------------------------------------+---------+---------+------+-------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+-------+-----------------+---------------------------------------+---------+---------+------+-------+-------------+
| 1 | PRIMARY | this_ | ALL | ei3 | NULL | NULL | NULL | 49434 | Using where |
| 2 | DEPENDENT SUBQUERY | this_ | unique_subquery | PRIMARY,eijoin2,ei_client,team2,team1 | PRIMARY | 4 | func | 1 | Using where |
+----+--------------------+-------+-----------------+---------------------------------------+---------+---------+------+-------+-------------+
2 rows in set (0.00 sec)
As far as I know, neither
Criterianor HQL can produce queries with subqueries infromclause, so that you have to use native SQL.