Neo4j慢创建方法

Neo4j slow creation method

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

VoteGroup 包含与实体 CriterionDecision 的关系 VOTED_ONVOTED_FOR 以及 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 工作起来真的很慢,而且延迟很大。这可能是什么原因?

添加了配置文件输出

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 page with profiler information

一些可能有帮助的想法:

  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 方法中存在错误,导致数据库中的更改比实际应该发生的更改多得多?

您可以尝试重新制定 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

我已转移到新的 Neo4j 2.2.4 和 SDN 3.4.0.RC1,问题消失了