在我的Neo4j / Neo4j Spring Data应用程序中,我有以下实体:
VoteGroup
包含VOTED_ON
和VOTED_FOR
与实体Criterion
和Decision
的关系以及Vote
的列表
@NodeEntity
public class VoteGroup extends BaseEntity {
private static final String VOTED_ON = "VOTED_ON";
private final static String VOTED_FOR = "VOTED_FOR";
private final static String CONTAINS = "CONTAINS";
@GraphId
private Long id;
@RelatedTo(type = VOTED_FOR, direction = Direction.OUTGOING)
private Decision decision;
@RelatedTo(type = VOTED_ON, direction = Direction.OUTGOING)
private Criterion criterion;
@RelatedTo(type = CONTAINS, direction = Direction.OUTGOING)
private Set<Vote> votes = new HashSet<>();
private double avgVotesWeight;
private long totalVotesCount;
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
VoteGroup voteGroup = (VoteGroup) o;
if (id == null)
return super.equals(o);
return id.equals(voteGroup.id);
}
@Override
public int hashCode() {
return id != null ? id.hashCode() : super.hashCode();
}
.....
}
Vote
实体看起来像:
@NodeEntity
public class Vote extends BaseEntity {
private final static String CONTAINS = "CONTAINS";
private final static String CREATED_BY = "CREATED_BY";
@GraphId
private Long id;
@RelatedTo(type = CONTAINS, direction = Direction.INCOMING)
private VoteGroup group;
@RelatedTo(type = CREATED_BY, direction = Direction.OUTGOING)
private User author;
private double weight;
....
}
public class BaseEntity {
private Date createDate;
private Date updateDate;
public BaseEntity() {
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
public Date getUpdateDate() {
return updateDate;
}
public void setUpdateDate(Date updateDate) {
this.updateDate = updateDate;
}
}
也。我使用基于BaseEntity的Neo4j钩子:
@Configuration
@EnableNeo4jRepositories(basePackages = "com.example")
@EnableTransactionManagement
public class Neo4jConfig extends Neo4jConfiguration implements BeanFactoryAware {
...
/**
* Hook into the application lifecycle and register listeners that perform
* behaviour across types of entities during this life cycle
*
*/
@Bean
protected ApplicationListener<BeforeSaveEvent<BaseEntity>> beforeSaveEventApplicationListener() {
return new ApplicationListener<BeforeSaveEvent<BaseEntity>>() {
@Override
public void onApplicationEvent(BeforeSaveEvent<BaseEntity> event) {
BaseEntity entity = event.getEntity();
if (entity.getCreateDate() == null) {
entity.setCreateDate(new Date());
} else {
entity.setUpdateDate(new Date());
}
}
};
}
...
}
为了进行投票,我实施了以下方法VoteGroupDaoImpl.createVote
:
@Service
@Transactional
public class VoteGroupDaoImpl implements VoteGroupDao {
@Autowired
private VoteRepository voteRepository;
@Autowired
private VoteGroupRepository voteGroupRepository;
@Override
public Vote createVote(Decision decision, Criterion criterion, User author, String description, double weight) {
VoteGroup voteGroup = getVoteGroupForDecisionOnCriterion(decision.getId(), criterion.getId());
if (voteGroup == null) {
voteGroup = new VoteGroup(decision, criterion, weight, 1);
} else {
long newTotalVotesCount = voteGroup.getTotalVotesCount() + 1;
double newAvgVotesWeight = (voteGroup.getAvgVotesWeight() * voteGroup.getTotalVotesCount() + weight) / newTotalVotesCount;
voteGroup.setAvgVotesWeight(newAvgVotesWeight);
voteGroup.setTotalVotesCount(newTotalVotesCount);
}
voteGroup = voteGroupRepository.save(voteGroup);
return voteRepository.save(new Vote(voteGroup, author, weight, description));
}
...
}
和
@Repository
public interface VoteGroupRepository extends GraphRepository<VoteGroup>, RelationshipOperationsRepository<VoteGroup> {
@Query("MATCH (d:Decision)<-[:VOTED_FOR]-(vg:VoteGroup)-[:VOTED_ON]->(c:Criterion) WHERE id(d) = {decisionId} AND id(c) = {criterionId} RETURN vg")
VoteGroup getVoteGroupForDecisionOnCriterion(@Param("decisionId") Long decisionId, @Param("criterionId") Long criterionId);
}
现在,方法VoteGroupDaoImpl.createVote
的工作速度非常慢,而且延迟很大......这可能是什么原因?
ADDED PROFILE输出
的
MATCH (d:Decision)<-[:VOTED_FOR]-(vg:VoteGroup)-[:VOTED_ON]->(c:Criterion) WHERE id(d) = {decisionId} AND id(c) = {criterionId} RETURN vg
Cypher版本:CYPHER 2.2,计划者:COST。在181毫秒内总共有33次点击率
PROFILE Java代码:
丰富的分析器信息:
答案 0 :(得分:1)
可能有所帮助的一些想法:
执行查询:
MATCH (d:Decision)<-[:VOTED_FOR]-(vg:VoteGroup)-[:VOTED_ON]->(c:Criterion)
WHERE id(d) = {decisionId} AND id(c) = {criterionId} RETURN vg
并检查其行为方式。尝试使用您在应用中使用的相同ID。检查执行时间是多少。
VoteGroup与投票有多少关系?如果是,可以删除:
@RelatedTo(type = CONTAINS, direction = Direction.OUTGOING)
private Set<Vote> votes = new HashSet<>();
仅保留投票方面的关系信息?你可以检查一下这个改变后的表现吗?
您是否可以使用某种分析工具来确定性能问题的确切位置?现在可能仍然难以猜测......
代码的行为是否正常?你在数据库中有任何重复吗?也许你的hashCode / equals方法中有bug会导致DB发生更多变化?
答案 1 :(得分:1)
您可以尝试重新制定 getVoteGroupForDecisionOnCriterion 查询,如下所示,以避免使用笛卡尔积:
MATCH (d:Decision) WHERE id(d) = {decisionId}
WITH d MATCH (c:Criterion) WHERE id(c) = {criterionId}
WITH d,c MATCH d<-[:VOTED_FOR]-(vg:VoteGroup)-[:VOTED_ON]->c
RETURN vg
答案 2 :(得分:0)
我搬到了新的Neo4j 2.2.4和SDN 3.4.0.RC1,问题就消失了