gpt4 book ai didi

Neo4j慢创建方法

转载 作者:行者123 更新时间:2023-12-02 01:34:39 25 4
gpt4 key购买 nike

<分区>

在我的 Neo4j/Neo4j Spring Data 应用程序中,我有以下实体:

VoteGroup 包含与实体 CriterionDecision 的关系 VOTED_ONVOTED_FOR 以及投票列表

@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 钩子(Hook):

@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 工作起来非常慢,延迟很大。这可能是什么原因?

添加的配置文件输出

对于

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 次数据库命中。

enter image description here

PROFILE Java 代码:

enter image description here

丰富的分析器信息:

HTML page with profiler information

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com