# Call apoc.meta.graph() expected runtime

**URL:** <https://community.neo4j.com/t/call-apoc-meta-graph-expected-runtime/34552>\
**Category:** Procedures & APOC\
**Tags:** apoc, performance\
**Created:** [March 2, 2021, 10:32am UTC](https://community.neo4j.com/t/call-apoc-meta-graph-expected-runtime/34552 "2021-03-02T10:32:15Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![JaHo](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/jaho/32/4395_2.png) [@JaHo](https://community.neo4j.com/u/JaHo)\
**Post date:** [March 2, 2021, 10:32am UTC](https://community.neo4j.com/t/call-apoc-meta-graph-expected-runtime/34552/1 "2021-03-02T10:32:15Z")

</div>

Can anyone tell me how long I should roughly expect `call apoc.meta.graph()` to run on a graph with 500 million nodes and 1.5 billion relationships?

I ran into the [bug](https://github.com/neo4j/neo4j/issues/9726) with `call db.schema.visualize()` and `apoc.meta.graph()` gave the correct answer before but is taking a while this time.

Thanks!

---

<div class="post-metadata">

**Author:** ![markhneedham](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/markhneedham/32/16986_2.png) [@markhneedham](https://community.neo4j.com/u/markhneedham)\
**Post date:** [March 4, 2021, 12:07pm UTC](https://community.neo4j.com/t/call-apoc-meta-graph-expected-runtime/34552/2 "2021-03-04T12:07:08Z")

</div>

It's probably gonna take ages to return. `db.schema.visualize` is using pre-computed data, whereas `apoc.meta.graph` is computing it all from scratch. Maybe you can take a look at `apoc.meta.graphSample` instead?

> **[apoc.meta - APOC Extended Documentation](https://neo4j.com/labs/apoc/4.1/overview/apoc.meta/)**
>
> This section contains reference documentation for the apoc.meta procedures.

---

<div class="post-metadata">

**Author:** ![JaHo](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/jaho/32/4395_2.png) [@JaHo](https://community.neo4j.com/u/JaHo)\
**Post date:** [March 4, 2021, 12:35pm UTC](https://community.neo4j.com/t/call-apoc-meta-graph-expected-runtime/34552/3 "2021-03-04T12:35:09Z")

</div>

I tried `apoc.meta.graphSample` but similarly to `db.schema.visualize` (and as stated in the documentation) it returned extra relationships.  
I also played around with `apoc.meta.subGraph` a bit which I got to yield a satisfactory result in the end. I'm still a bit confused though where the computational cost is coming from; for many subsets of nodes and relationships the result was instant while including some labels with fairly small sets of nodes/relationships resulted in long runtimes that I stopped after a while.

---

<div class="post-metadata">

**Author:** ![markhneedham](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/markhneedham/32/16986_2.png) [@markhneedham](https://community.neo4j.com/u/markhneedham)\
**Post date:** [March 4, 2021, 12:38pm UTC](https://community.neo4j.com/t/call-apoc-meta-graph-expected-runtime/34552/4 "2021-03-04T12:38:32Z")

</div>

I don't know this code off by heart, but this is the function that it's calling:

> <https://github.com/neo4j-contrib/neo4j-apoc-procedures/blob/4.2/core/src/main/java/apoc/meta/Meta.java#L993>

that then calls the `metaGraph` function:

> <https://github.com/neo4j-contrib/neo4j-apoc-procedures/blob/f3a42f8b6e344a0ca1a2b7d497be7c02853b4ca9/core/src/main/java/apoc/meta/Meta.java#L888>

And actually it doesn't look like it computes everything from scratch like I thought it did. It's kinda hard to say why it would be working better for some labels than others.

---

<div class="post-metadata">

**Author:** ![JaHo](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/jaho/32/4395_2.png) [@JaHo](https://community.neo4j.com/u/JaHo)\
**Post date:** [March 4, 2021, 12:58pm UTC](https://community.neo4j.com/t/call-apoc-meta-graph-expected-runtime/34552/5 "2021-03-04T12:58:27Z")

</div>

Thanks for the pointer, I don't really know any Java, though.  
It actually only started being slow after I recently added some new labels that about doubled the number of existing nodes. With the already pretty large number of nodes before that it worked instantly and returned the correct result.

---

<div class="post-metadata">

**Author:** ![markhneedham](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/markhneedham/32/16986_2.png) [@markhneedham](https://community.neo4j.com/u/markhneedham)\
**Post date:** [March 4, 2021, 2:07pm UTC](https://community.neo4j.com/t/call-apoc-meta-graph-expected-runtime/34552/6 "2021-03-04T14:07:51Z")

</div>

And on that graph you said `apoc.meta.graphSample` returns quickly but has extra relationships?  
  
The only difference between `apoc.meta.graphSample` and `apoc.meta.graph` is a post processing step where missing relationships are removed (or not) so that's where the time must be spent.  
  
Reading the code of that function I can see that it's doing a scan of all the nodes with each label and then checking all of the relationships for 1 in 1000 of those nodes, which would be time-consuming. You can configure the sampling rate via the `sample` key e.g. `sample: 10000` would make it sample every 10,000 nodes instead of every 1,000 nodes.

---

<div class="post-metadata">

**Author:** ![JaHo](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/jaho/32/4395_2.png) [@JaHo](https://community.neo4j.com/u/JaHo)\
**Post date:** [March 10, 2021, 10:14am UTC](https://community.neo4j.com/t/call-apoc-meta-graph-expected-runtime/34552/7 "2021-03-10T10:14:03Z")

</div>

Sorry for the delay.  
Yes, for the full graph, `apoc.meta.graphSample` runs quickly but has extra relationships. I tried running it with different sample sizes but I must have been doing it wrong as there was no difference in both runtime and result. Is `call apoc.met.graphSample({sample: 1000})` the correct syntax?

---

<div class="post-metadata">

**Author:** ![markhneedham](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/markhneedham/32/16986_2.png) [@markhneedham](https://community.neo4j.com/u/markhneedham)\
**Post date:** [March 10, 2021, 11:11am UTC](https://community.neo4j.com/t/call-apoc-meta-graph-expected-runtime/34552/8 "2021-03-10T11:11:23Z")

</div>

> [@JaHo](#):
>
> call apoc.met.graphSample({sample: 1000})

Yup, just gotta fix the typo on here:

```auto
call apoc.meta.graphSample({sample: 1000})

```

---

<div class="post-metadata">

**Author:** ![JaHo](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/jaho/32/4395_2.png) [@JaHo](https://community.neo4j.com/u/JaHo)\
**Post date:** [March 10, 2021, 2:32pm UTC](https://community.neo4j.com/t/call-apoc-meta-graph-expected-runtime/34552/9 "2021-03-10T14:32:32Z")

</div>

Ah my bad. Still, even if I call it with `sample: 1` (which I guess would mean it checks every node), it returns instantly and contains additional relationships.

---

<div class="post-metadata">

**Author:** ![markhneedham](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/markhneedham/32/16986_2.png) [@markhneedham](https://community.neo4j.com/u/markhneedham)\
**Post date:** [March 10, 2021, 2:58pm UTC](https://community.neo4j.com/t/call-apoc-meta-graph-expected-runtime/34552/10 "2021-03-10T14:58:36Z")

</div>

Can you try:

```auto
call apoc.meta.graph({sample: 1000})

```

---

<div class="post-metadata">

**Author:** ![JaHo](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/jaho/32/4395_2.png) [@JaHo](https://community.neo4j.com/u/JaHo)\
**Post date:** [March 10, 2021, 3:22pm UTC](https://community.neo4j.com/t/call-apoc-meta-graph-expected-runtime/34552/11 "2021-03-10T15:22:26Z")

</div>

That seems to run slowly irrespective of what I set sample to. I haven't let it run longer than a minute or so, though.

---

<div class="post-metadata">

**Author:** ![markhneedham](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/markhneedham/32/16986_2.png) [@markhneedham](https://community.neo4j.com/u/markhneedham)\
**Post date:** [March 10, 2021, 4:43pm UTC](https://community.neo4j.com/t/call-apoc-meta-graph-expected-runtime/34552/12 "2021-03-10T16:43:58Z")

</div>

I'm playing around with it on a dummy graph with 40m nodes/relationships and I can see different speeds of response when specifying `sample`.

 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/f/3/f38baf243cda830ddf0f0dd89ddba2aabae8dde3.png)

---

<div class="post-metadata">

**Author:** ![JaHo](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/jaho/32/4395_2.png) [@JaHo](https://community.neo4j.com/u/JaHo)\
**Post date:** [March 11, 2021, 8:42am UTC](https://community.neo4j.com/t/call-apoc-meta-graph-expected-runtime/34552/13 "2021-03-11T08:42:29Z")

</div>

That's strange, not exactly sure what's going on. Anyways, it's not a pressing issue for me at the moment so I don't want to steal too much of your time. If I can help by providing more info I'd be happy to. Thanks again for your help!
