# Debug cypher query

**URL:** <https://community.neo4j.com/t/debug-cypher-query/25778>\
**Category:** Cypher\
**Tags:** apoc, cypher\
**Created:** [September 22, 2020, 12:12pm UTC](https://community.neo4j.com/t/debug-cypher-query/25778 "2020-09-22T12:12:49Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![gabriel\_toma](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/gabriel_toma/32/722_2.png) [@gabriel\_toma](https://community.neo4j.com/u/gabriel_toma)\
**Post date:** [September 22, 2020, 12:12pm UTC](https://community.neo4j.com/t/debug-cypher-query/25778/1 "2020-09-22T12:12:49Z")

</div>

Is there a way to proper **debug** a cypher query?

I've been busting my head over an integer value vs string property which I could not detect as it just did not give any error, not even a hint. And also the query was embedded in apoc.do.case, hence, no real visibility on what happens in there.

Explain and profile are not quite what I would call "debug" = being able to pause, as to check the variable value and the result of the function, step by step

Cutting and shortening the query into pieces and checking each piece individually is not really fun.

---

<div class="post-metadata">

**Author:** ![accounts](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/accounts/32/4145_2.png) [@accounts](https://community.neo4j.com/u/accounts)\
**Post date:** [September 22, 2020, 8:40pm UTC](https://community.neo4j.com/t/debug-cypher-query/25778/2 "2020-09-22T20:40:03Z")

</div>

apart from cloning the neo4j [repo](https://github.com/neo4j/neo4j) and debugging the cypher execution ( which i have not done ) i don't know of a way to _proper **debug** a cypher query_

As an alternative you I find it easy to debug whilest deploying the cypher work as a procedure and debugging it that way.

---

<div class="post-metadata">

**Author:** ![tony.chiboucas](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/tony.chiboucas/32/5980_2.png) [@tony.chiboucas](https://community.neo4j.com/u/tony.chiboucas)\
**Post date:** [September 22, 2020, 9:22pm UTC](https://community.neo4j.com/t/debug-cypher-query/25778/3 "2020-09-22T21:22:54Z")

</div>

It depends on the problem.

1. [PROFILE](https://neo4j.com/docs/cypher-manual/current/query-tuning/how-do-i-profile-a-query/) and EXPLAIN can help identify poor selectors or clauses, and where you might use too much ram, or hit cycles.
2. [WITH](https://neo4j.com/docs/cypher-manual/current/clauses/with/) and RETURN, will help get a much clearer picture of the data, at any step in your Cypher. (pick a step, `WITH` the props and vars that are related to the problem, and `RETURN` them.
3. Simplify your commands. When commands get complicated, they'll often run better, and more easily debugged, as two or more sequential commands instead.
4. Share your code and describe your problem. _Seriously_. There's a lot of people here happy to help. You can always @me.
5. Apoc stuff is often tricky to debug _because_ most of it is doing a lot of work in Java-land that is invisible to Neo4j and Cypher.
  - For apoc, see the `WITH` comment above. You should be able to get some basic tabular data of the conditional, and the ids/names/etc of nodes you'll be operating on.
  - I've sometime also found it useful to return a string representation of a simple command I'm trying to run:

```auto
RETURN
   "MERGE (a:Thing {name: '" + a.name + "'})-[r]->" + 
   "(b {name: '" + b.name + "'}) " +
   "SET r.score = sum(c.val) ["+ sum(c.val) + "]"
as cy

```

---

<div class="post-metadata">

**Author:** ![gabriel\_toma](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/gabriel_toma/32/722_2.png) [@gabriel\_toma](https://community.neo4j.com/u/gabriel_toma)\
**Post date:** [September 28, 2020, 5:54pm UTC](https://community.neo4j.com/t/debug-cypher-query/25778/4 "2020-09-28T17:54:44Z")

</div>

I wonder how this can be done....

---

<div class="post-metadata">

**Author:** ![tony.chiboucas](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/tony.chiboucas/32/5980_2.png) [@tony.chiboucas](https://community.neo4j.com/u/tony.chiboucas)\
**Post date:** [October 3, 2020, 12:07am UTC](https://community.neo4j.com/t/debug-cypher-query/25778/6 "2020-10-03T00:07:36Z")

</div>

Can share the Cypher you're trying to debug, with some sample data?

---

<div class="post-metadata">

**Author:** ![clem](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/clem/32/4364_2.png) [@clem](https://community.neo4j.com/u/clem)\
**Post date:** [December 7, 2020, 10:10pm UTC](https://community.neo4j.com/t/debug-cypher-query/25778/7 "2020-12-07T22:10:08Z")

</div>

When there is a syntax error, it's relatively easy to debug. But what is frustrating to debug is when the query goes through yet nothing is returned.

It would be nice if there was a trace mode so that you can find out which MATCH actually failed and returned nothing and why. E.g. Maybe you are querying for a property that doesn't exist (because you misspelled the Property Name) or the Value doesn't exist or something.

I'm trying to import some relationships, which includes nodes that do NOT exist and I don't want to create either the node or the relationship. I'd like to have a trace so that I can see the failing matches (for which I'm OK) and the succeeding matches and see why the Relationship creations fail.

I'm still puzzling this out.

---

<div class="post-metadata">

**Author:** ![tony.chiboucas](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/tony.chiboucas/32/5980_2.png) [@tony.chiboucas](https://community.neo4j.com/u/tony.chiboucas)\
**Post date:** [December 15, 2020, 11:27pm UTC](https://community.neo4j.com/t/debug-cypher-query/25778/8 "2020-12-15T23:27:16Z")

</div>

> [@clem](#):
>
> It would be nice if there was a trace mode so that you can find out which MATCH actually failed and returned nothing and why. E.g. Maybe you are querying for a property that doesn't exist (because you misspelled the Property Name) or the Value doesn't exist or something.

That's what the Neo4j Browser is for. Debugging your queries. You can see the whole process via `PROFILE`, or...

> [@clem](#):
>
> I'd like to have a trace so that I can see the failing matches (for which I'm OK) and the succeeding matches and see why the Relationship creations fail.

...adjust your query to return those parts and patterns you want to take a closer look at.

---

<div class="post-metadata">

**Author:** ![clem](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/clem/32/4364_2.png) [@clem](https://community.neo4j.com/u/clem)\
**Post date:** [December 15, 2020, 11:50pm UTC](https://community.neo4j.com/t/debug-cypher-query/25778/9 "2020-12-15T23:50:45Z")

</div>

I'm sorry to say that doesn't help me that much....

My latest query I was trying to debug was:

```auto
MATCH(c:Category)
WHERE c.Name CONTAINS "Mammal" AND NOT c.Name CONTAINS "Non-Mamal" AND NOT c.Name CONTAINS "Non Mamal"
return c

```

I was still getting Categories that contain "Non-Mammal".

What I discovered was that I had a misspelling. (`Non-Mamal` should have been `Non-Mammal`)

The PROFILE shows **estimated** rows and it doesn't break apart the expression. It would have been nice to know that the first part of the complex boolean allowed N rows, and the next part of the boolean allowed N rows again, (instead of smaller number.)

I'm kind of getting the hang of it, but it would be really nice if there was a tutorial on how to go about debugging CYPHER.

One thing I have discovered is it might be possible to log events as the Cypher Query executes. I haven't tried it yet, but this looks interesting:

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

The other thing that makes Query Languages a bit harder is that they are **declarative** and not **procedural** , so the intuition that people may have with procedural debugging breaks down with a declarative language.

---

<div class="post-metadata">

**Author:** ![tony.chiboucas](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/tony.chiboucas/32/5980_2.png) [@tony.chiboucas](https://community.neo4j.com/u/tony.chiboucas)\
**Post date:** [December 16, 2020, 9:13pm UTC](https://community.neo4j.com/t/debug-cypher-query/25778/10 "2020-12-16T21:13:47Z")

</div>

You're certainly right that this kind of debugging is _very_ different. I have one giant one that imports from an API that's 300 lines long.

One basic trick still applies though: break the problem into smaller chunks.

Starting with a problem here:

```auto
MATCH(c:Category)
WHERE c.Name CONTAINS "Mammal" AND NOT c.Name CONTAINS "Non-Mamal" AND NOT c.Name CONTAINS "Non Mamal"
RETURN c

```

Split it into the component parts:

```auto
MATCH (a:Category {name: "Mammal"}), (b:Category {name: "Non-Mamal"}), (c:Category {name: "Non Mammal"}) 
RETURN count(a), count(b), count(c)

```

But really, even in procedural code, typos can be hard to find.

---

<div class="post-metadata">

**Author:** ![clem](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/clem/32/4364_2.png) [@clem](https://community.neo4j.com/u/clem)\
**Post date:** [December 16, 2020, 9:23pm UTC](https://community.neo4j.com/t/debug-cypher-query/25778/11 "2020-12-16T21:23:10Z")

</div>

Well, in procedural code, you can step through the code with a debugger. Or even use print statements to track down the problem.

It would be nice if PROFILE output could be used as an "outline" for a debugging process. That is, if you could "step" through each of the blocks and examine what the intermediate values contained.

---

<div class="post-metadata">

**Author:** ![gabriel\_toma](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/gabriel_toma/32/722_2.png) [@gabriel\_toma](https://community.neo4j.com/u/gabriel_toma)\
**Post date:** [December 16, 2020, 9:32pm UTC](https://community.neo4j.com/t/debug-cypher-query/25778/12 "2020-12-16T21:32:14Z")

</div>

Well....is there some debugging functionality scheduled for development?  
Or, at least, on the roadmap plan?

[@Michael Hunger](mailto:michael.hunger@neo4j.com) , maybe you have (as always) some pretty good advice?  
Overall, we would need something to better debug cypher queries.  
Whereas we have already used profile and explain and ... they were not sufficient to easily determine the issue.  
Or maybe we are not using them correctly?

Anyway, what I would need, is to actually see the intermediate result of the query, but for real, really see it, as per the execution plan - not just the estimated rows.

Is there a way to do this?

Regards,  
Gab

mie., 16 dec. 2020, 22:24 Tony Chiboucas via Neo4j Online Community \<[neo4jcommunity@discoursemail.com](mailto:neo4jcommunity@discoursemail.com)\> a scris:

---

<div class="post-metadata">

**Author:** ![tony.chiboucas](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/tony.chiboucas/32/5980_2.png) [@tony.chiboucas](https://community.neo4j.com/u/tony.chiboucas)\
**Post date:** [December 16, 2020, 9:34pm UTC](https://community.neo4j.com/t/debug-cypher-query/25778/13 "2020-12-16T21:34:26Z")

</div>

Ah... you want a debugger to be able to step through Cypher?

That's.... pretty silly.

The only way such a thing would have helped in your example, was if you step through the whole script, and at each stop, also step through every record.

That wouldn't be worth the time it would take to debug that way.

---

<div class="post-metadata">

**Author:** ![gabriel\_toma](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/gabriel_toma/32/722_2.png) [@gabriel\_toma](https://community.neo4j.com/u/gabriel_toma)\
**Post date:** [December 17, 2020, 4:50pm UTC](https://community.neo4j.com/t/debug-cypher-query/25778/14 "2020-12-17T16:50:03Z")

</div>

@tony.chiboucas just to let you know I did not appreciate your comment "That's.... pretty silly.".

---

<div class="post-metadata">

**Author:** ![andrew\_bowman](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/andrew_bowman/32/73_2.png) [@andrew\_bowman](https://community.neo4j.com/u/andrew_bowman)\
**Post date:** [December 18, 2020, 8:29pm UTC](https://community.neo4j.com/t/debug-cypher-query/25778/15 "2020-12-18T20:29:31Z")

</div>

Having the PROFILE plan be the basis of some kind of debugging tool does sound rather useful. We can certainly put the idea on our backlog to consider.

That said, when we have APOC executing Cypher strings, such a theoretical debugger likely won't be able to have visibility here.

When debugging issues from APOC Cypher strings, usually you have to rip that part out, and PROFILE that segment outside of APOC, but it does require some work to ensure you have a sample of data for it to execute upon.

---

<div class="post-metadata">

**Author:** ![clem](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/clem/32/4364_2.png) [@clem](https://community.neo4j.com/u/clem)\
**Post date:** [January 6, 2021, 12:39am UTC](https://community.neo4j.com/t/debug-cypher-query/25778/16 "2021-01-06T00:39:56Z")

</div>

One thing to do is, is break your Cypher query into simpler parts.

Start with the first part of the query and see if it's returning what you are expecting. Then add the next part, and check the results.

---

<div class="post-metadata">

**Author:** ![gabriel\_toma](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/gabriel_toma/32/722_2.png) [@gabriel\_toma](https://community.neo4j.com/u/gabriel_toma)\
**Post date:** [January 6, 2021, 6:51am UTC](https://community.neo4j.com/t/debug-cypher-query/25778/17 "2021-01-06T06:51:19Z")

</div>

We still need a debugger.

---

<div class="post-metadata">

**Author:** ![DaveA](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@DaveA](https://community.neo4j.com/u/DaveA)\
**Post date:** [August 16, 2024, 11:41pm UTC](https://community.neo4j.com/t/debug-cypher-query/25778/18 "2024-08-16T23:41:41Z")

</div>

> Ah... you want a debugger to be able to step through Cypher?
> 
> That's.... pretty silly.

Look at how Pentaho does it. That works really nicely.
