Neo4j慢创建方法[关闭]

2024-02-02

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

VoteGroup包含关系VOTED_ON and VOTED_FOR给实体Criterion and 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));
    }
...

}

and

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

添加配置文件输出

for

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 次数据库点击。

简介 Java 代码:

丰富的分析器信息:

包含分析器信息的 HTML 页面 http://www.thedownloadplanet.com/files/cpu-live.html


一些可能有帮助的想法:

  1. 执行查询:

    MATCH (d:Decision)<-[:VOTED_FOR]-(vg:VoteGroup)-[:VOTED_ON]->(c:Criterion) WHERE id(d) = {decisionId} AND id(c) = {criterionId} RETURN vg

从 Web 界面或控制台检查其行为方式。尝试使用您在应用程序中使用的相同 ID。检查执行时间是多少。

  1. VoteGroup 与 Votes 有很多关系吗?如果是,您可以删除:

    @RelatedTo(type = CONTAINS, direction = Direction.OUTGOING) private Set<Vote> votes = new HashSet<>();

并仅在投票端保留有关关系的信息?您能检查一下更改后的性能吗?

  1. 您可以使用某种分析器工具来确定性能问题的确切位置吗?现在可能还很难猜...

  2. 代码的行为是否符合预期?数据库中是否有重复项?也许您的 hashCode/equals 方法中存在错误,导致数据库中的更改比实际应有的更改多得多?

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Neo4j慢创建方法[关闭] 的相关文章

随机推荐