better index usage

This commit is contained in:
Athou
2013-10-13 12:15:41 +02:00
parent c993bd472d
commit 836f7eff09
3 changed files with 13 additions and 13 deletions

View File

@@ -17,6 +17,7 @@ import javax.persistence.criteria.Root;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.builder.CompareToBuilder; import org.apache.commons.lang3.builder.CompareToBuilder;
import org.hibernate.Criteria; import org.hibernate.Criteria;
import org.hibernate.criterion.Conjunction;
import org.hibernate.criterion.Disjunction; import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.MatchMode; import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order; import org.hibernate.criterion.Order;
@@ -132,7 +133,7 @@ public class FeedEntryStatusDAO extends GenericDAO<FeedEntryStatus> {
return lazyLoadContent(includeContent, statuses); return lazyLoadContent(includeContent, statuses);
} }
private Criteria buildSearchCriteria(FeedSubscription sub, boolean unreadOnly, String keywords, Date newerThan, int offset, int limit, private Criteria buildSearchCriteria(User user, FeedSubscription sub, boolean unreadOnly, String keywords, Date newerThan, int offset, int limit,
ReadingOrder order, Date last, String tag) { ReadingOrder order, Date last, String tag) {
Criteria criteria = getSession().createCriteria(FeedEntry.class, ALIAS_ENTRY); Criteria criteria = getSession().createCriteria(FeedEntry.class, ALIAS_ENTRY);
@@ -165,8 +166,10 @@ public class FeedEntryStatusDAO extends GenericDAO<FeedEntryStatus> {
} }
if (tag != null) { if (tag != null) {
criteria.createCriteria(FeedEntry_.tags.getName(), ALIAS_TAG, JoinType.INNER_JOIN, Conjunction and = Restrictions.conjunction();
Restrictions.eq(FeedEntryTag_.name.getName(), tag)); and.add(Restrictions.eq(FeedEntryTag_.user.getName(), user));
and.add(Restrictions.eq(FeedEntryTag_.name.getName(), tag));
criteria.createCriteria(FeedEntry_.tags.getName(), ALIAS_TAG, JoinType.INNER_JOIN, and);
} }
if (newerThan != null) { if (newerThan != null) {
@@ -210,7 +213,7 @@ public class FeedEntryStatusDAO extends GenericDAO<FeedEntryStatus> {
FixedSizeSortedSet<FeedEntryStatus> set = new FixedSizeSortedSet<FeedEntryStatus>(capacity, comparator); FixedSizeSortedSet<FeedEntryStatus> set = new FixedSizeSortedSet<FeedEntryStatus>(capacity, comparator);
for (FeedSubscription sub : subs) { for (FeedSubscription sub : subs) {
Date last = (order != null && set.isFull()) ? set.last().getEntryUpdated() : null; Date last = (order != null && set.isFull()) ? set.last().getEntryUpdated() : null;
Criteria criteria = buildSearchCriteria(sub, unreadOnly, keywords, newerThan, -1, capacity, order, last, tag); Criteria criteria = buildSearchCriteria(user, sub, unreadOnly, keywords, newerThan, -1, capacity, order, last, tag);
ProjectionList projection = Projections.projectionList(); ProjectionList projection = Projections.projectionList();
projection.add(Projections.property("id"), "id"); projection.add(Projections.property("id"), "id");
projection.add(Projections.property("updated"), "updated"); projection.add(Projections.property("updated"), "updated");
@@ -263,9 +266,9 @@ public class FeedEntryStatusDAO extends GenericDAO<FeedEntryStatus> {
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public UnreadCount getUnreadCount(FeedSubscription subscription) { public UnreadCount getUnreadCount(User user, FeedSubscription subscription) {
UnreadCount uc = null; UnreadCount uc = null;
Criteria criteria = buildSearchCriteria(subscription, true, null, null, -1, -1, null, null, null); Criteria criteria = buildSearchCriteria(user, subscription, true, null, null, -1, -1, null, null, null);
ProjectionList projection = Projections.projectionList(); ProjectionList projection = Projections.projectionList();
projection.add(Projections.rowCount(), "count"); projection.add(Projections.rowCount(), "count");
projection.add(Projections.max(FeedEntry_.updated.getName()), "updated"); projection.add(Projections.max(FeedEntry_.updated.getName()), "updated");

View File

@@ -95,11 +95,11 @@ public class FeedSubscriptionService {
} }
} }
public UnreadCount getUnreadCount(FeedSubscription sub) { public UnreadCount getUnreadCount(User user, FeedSubscription sub) {
UnreadCount count = cache.getUnreadCount(sub); UnreadCount count = cache.getUnreadCount(sub);
if (count == null) { if (count == null) {
log.debug("unread count cache miss for {}", Models.getId(sub)); log.debug("unread count cache miss for {}", Models.getId(sub));
count = feedEntryStatusDAO.getUnreadCount(sub); count = feedEntryStatusDAO.getUnreadCount(user, sub);
cache.setUnreadCount(sub, count); cache.setUnreadCount(sub, count);
} }
return count; return count;
@@ -109,7 +109,7 @@ public class FeedSubscriptionService {
Map<Long, UnreadCount> map = Maps.newHashMap(); Map<Long, UnreadCount> map = Maps.newHashMap();
List<FeedSubscription> subs = feedSubscriptionDAO.findAll(user); List<FeedSubscription> subs = feedSubscriptionDAO.findAll(user);
for (FeedSubscription sub : subs) { for (FeedSubscription sub : subs) {
map.put(sub.getId(), getUnreadCount(sub)); map.put(sub.getId(), getUnreadCount(user, sub));
} }
return map; return map;
} }

View File

@@ -34,12 +34,9 @@
<addForeignKeyConstraint constraintName="fk_user_id" baseTableName="FEEDENTRYTAGS" baseColumnNames="user_id" <addForeignKeyConstraint constraintName="fk_user_id" baseTableName="FEEDENTRYTAGS" baseColumnNames="user_id"
referencedTableName="USERS" referencedColumnNames="id" /> referencedTableName="USERS" referencedColumnNames="id" />
<createIndex tableName="FEEDENTRYTAGS" indexName="user_entry_index"> <createIndex tableName="FEEDENTRYTAGS" indexName="user_entry_name_index">
<column name="user_id" /> <column name="user_id" />
<column name="entry_id" /> <column name="entry_id" />
</createIndex>
<createIndex tableName="FEEDENTRYTAGS" indexName="user_name_index">
<column name="user_id" />
<column name="name" /> <column name="name" />
</createIndex> </createIndex>
</changeSet> </changeSet>