Neo4j缓慢创建方法 [英] Neo4j slow creation method
问题描述
在我的Neo4j/Neo4j Spring Data应用程序中,我具有以下实体:
In my Neo4j/Neo4j Spring Data application I have a following entities:
VoteGroup
包含与实体Criterion
和Decision
的关系VOTED_ON
和VOTED_FOR
以及Vote
VoteGroup
contains relationships VOTED_ON
and VOTED_FOR
to entities Criterion
and Decision
and list of 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钩子:
also. I use Neo4j hook based on BaseEntity:
@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
:
in order to make a vote, I have implemented following method 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
的工作速度确实很慢,并且延迟很长..这可能是什么原因?
Right now, method VoteGroupDaoImpl.createVote
works really slow with a huge latency .. what can be a reason of that ?
添加的配置文件输出
为
MATCH (d:Decision)<-[:VOTED_FOR]-(vg:VoteGroup)-[:VOTED_ON]->(c:Criterion) WHERE id(d) = {decisionId} AND id(c) = {criterionId} RETURN vg
密码版本:CYPHER 2.2,计划程序:COST.在181毫秒内,数据库共有33次点击.
Cypher version: CYPHER 2.2, planner: COST. 33 total db hits in 181 ms.
配置文件Java代码:
丰富的探查器信息:
推荐答案
我已移至新的Neo4j 2.2.4和SDN 3.4.0.RC1,问题消失了
I'm moved to new Neo4j 2.2.4 and SDN 3.4.0.RC1 and the issue disappeared
这篇关于Neo4j缓慢创建方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!