# Why is executing Cypher with Java so slow?

**URL:** <https://community.neo4j.com/t/why-is-executing-cypher-with-java-so-slow/64193>\
**Category:** Cypher\
**Tags:** cypher\
**Created:** [September 20, 2023, 6:14am UTC](https://community.neo4j.com/t/why-is-executing-cypher-with-java-so-slow/64193 "2023-09-20T06:14:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![superman09201](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@superman09201](https://community.neo4j.com/u/superman09201)\
**Post date:** [September 20, 2023, 6:14am UTC](https://community.neo4j.com/t/why-is-executing-cypher-with-java-so-slow/64193/1 "2023-09-20T06:14:18Z")

</div>

hello team  
The version of neo4j is community-5.12.0.  
Here is my java code

```java
import org.neo4j.configuration.GraphDatabaseSettings;
import org.neo4j.dbms.api.DatabaseManagementServiceBuilder;
import org.neo4j.graphdb.Transaction;

import java.nio.file.Path;
import java.time.Duration;

import static org.neo4j.configuration.GraphDatabaseSettings.DEFAULT_DATABASE_NAME;

/**
 * @author SuperL
 */
public class TestShortestPath2 {
    private static final Path DB_DIR = Path.of("/Users/superl/tools/neo4j-community-5.11.0/data");
    private static final String CQL =
            """
            MATCH (n:Node {name:'node1'}) RETURN n
            """;

    public static void main(String[] args) {
        var managementServiceBuilder = new DatabaseManagementServiceBuilder(DB_DIR);
        var managementService = managementServiceBuilder.setConfig(GraphDatabaseSettings.pagecache_memory, 1024 * 1024 * 1024L)
                                                        .setConfig(GraphDatabaseSettings.transaction_timeout, Duration.ofSeconds(60))
                                                        .setConfig(GraphDatabaseSettings.preallocate_logical_logs, false)
                                                        .setConfig(GraphDatabaseSettings.debug_log_enabled, false)
                                                        .build();
        var graphDB = managementService.database(DEFAULT_DATABASE_NAME);

        try (Transaction tx = graphDB.beginTx()) {
            long startTime = System.currentTimeMillis();
            var result = tx.execute(CQL);
            System.out.println(System.currentTimeMillis() - startTime);
            while (result.hasNext()) {
            }
            tx.commit();
        }
    }
} 

```

Here is my graph.

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

The code **tx.execute(CQL)** took 683ms, but it took only 3ms in the Web UI.  
Why is that happening ？

---

<div class="post-metadata">

**Author:** ![glilienfield](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/glilienfield/32/27534_2.png) [@glilienfield](https://community.neo4j.com/u/glilienfield)\
**Post date:** [September 20, 2023, 7:55am UTC](https://community.neo4j.com/t/why-is-executing-cypher-with-java-so-slow/64193/2 "2023-09-20T07:55:52Z")

</div>

The first time a query is executed it must first be planned. This takes time. This is not necessary on subsequent executions if the query plan is still cached.

Try executing the query again in your code and compare the duration of the first and second executions.

---

<div class="post-metadata">

**Author:** ![bennu\_neo](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/bennu_neo/32/27616_2.png) [@bennu\_neo](https://community.neo4j.com/u/bennu_neo)\
**Post date:** [September 20, 2023, 12:40pm UTC](https://community.neo4j.com/t/why-is-executing-cypher-with-java-so-slow/64193/3 "2023-09-20T12:40:37Z")

</div>

Do you have an index on :Node name?

---

<div class="post-metadata">

**Author:** ![superman09201](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@superman09201](https://community.neo4j.com/u/superman09201)\
**Post date:** [September 21, 2023, 2:43am UTC](https://community.neo4j.com/t/why-is-executing-cypher-with-java-so-slow/64193/5 "2023-09-21T02:43:19Z")

</div>

> [@glilienfield](#):
>
> The first time a query is executed it must first be planned. This takes time. This is not necessary on subsequent executions if the query plan is still cached.
> 
> Try executing the query again in your code and compare the duration of the first and second executions.

Thanks , you are correct. The duration of non-first-time requests is less than 3 ms.  
How to reduce the took time of first time query ? There are so many cyphers of dynamic-build in my application and I can't warmup them all.

---

<div class="post-metadata">

**Author:** ![superman09201](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@superman09201](https://community.neo4j.com/u/superman09201)\
**Post date:** [September 21, 2023, 2:44am UTC](https://community.neo4j.com/t/why-is-executing-cypher-with-java-so-slow/64193/6 "2023-09-21T02:44:20Z")

</div>

yes, I have created index for it.  
 ![image](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/c/2/c2ad32932ea8acb606d7a780373dfeb9d8248afd.png)

---

<div class="post-metadata">

**Author:** ![glilienfield](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/glilienfield/32/27534_2.png) [@glilienfield](https://community.neo4j.com/u/glilienfield)\
**Post date:** [September 21, 2023, 1:20pm UTC](https://community.neo4j.com/t/why-is-executing-cypher-with-java-so-slow/64193/7 "2023-09-21T13:20:47Z")

</div>

What I suggest is that you always use parameters instead of building your queries.

In your example, replace ‘node1’ with a query parameter. The query will be parsed and cached, so it can be reused for any value of parameter passed.
