Cypher FOREACH MERGE not hitting the index

2019-08-05 08:36发布

I've got a following parametrized Cypher query:

MERGE (p:Person {pid: {personId}}) ON CREATE SET p.value=rand()
MERGE (c:Page {url: {pageUrl}}) ON CREATE SET c.value=rand()
MERGE p-[:REL]->c
FOREACH (tagValue IN {tags} | 
  MERGE (t:Tag {value:tagValue})
  MERGE c-[:hasTag]->t)

This is very slow, the profiling shows:

    EmptyResult
      |
      +UpdateGraph(0)
        |
        +Eager(0)
          |
          +UpdateGraph(1)
            |
            +Eager(1)
              |
              +UpdateGraph(2)

    +----------------+------+--------+------------------------------+------------------------------------------------------------------------------+
    |       Operator | Rows | DbHits |                  Identifiers |                                                                        Other |
    +----------------+------+--------+------------------------------+------------------------------------------------------------------------------+
    |    EmptyResult |    0 |      0 |                              |                                                                              |
    | UpdateGraph(0) |    1 |  79222 |                              |                                                                      Foreach |
    |       Eager(0) |    1 |      0 |                              |                                                                              |
    | UpdateGraph(1) |    1 |      5 |           p, c,   UNNAMED163 |                                                                 MergePattern |
    |       Eager(1) |    1 |      0 |                              |                                                                              |
    | UpdateGraph(2) |    1 |     14 |                   p, p, c, c | 
MergeNode; {personId}; :Person(pid); MergeNode; {pageUrl}; :Page(url) |
    +----------------+------+--------+------------------------------+------------------------------------------------------------------------------+

    Total database accesses: 79241

As you can see, it's apparently not using the index I've defined on :Tag(value)

Any ideas how to fix this? I'm running out of ideas and I'm starting to think this might be connected to https://github.com/neo4j/neo4j/issues/861

FYI, the MERGEs are really convenient for me and this query perfectly matches (or would if it worked:) the usage I need for data ingestion.

标签: neo4j cypher
1条回答
贪生不怕死
2楼-- · 2019-08-05 08:57

Hmmm, does it use an index if you use UNWIND instead of FOREACH?

MERGE (p:Person {pid: {personId}}) ON CREATE SET p.value=rand()
MERGE (c:Page {url: {pageUrl}}) ON CREATE SET c.value=rand()
MERGE p-[:REL]->c
WITH c
UNWIND {tags} AS tagValue
MERGE (t:Tag {value:tagValue})
MERGE c-[:hasTag]->t
查看更多
登录 后发表回答