cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Neo4j JS Driver slow response

sai1
Node Link

Hi All,
when I am running the below query in neo4j browser, it takes around 2-3ms to give results but when I run using JS driver, it takes around 450-500ms to run. the HTTP endpoint takes 200ms to run.
I am using the latest driver (ran this cmd npm install neo4j-driver ) .
below is the query

match(u:User{id:'5b186ae490d3be0b45dfe4c4'})-[:POSTED_ON_2020_05_17|POSTED_ON_2020_05_10]->(p:Post) return p order by p.createdAt

I am not sure if there is any mistake from my side. Can someone please help me?
below is the JS snippet I used

let [driver, session] = getDriverAndSession();
      var t0 = performance.now();
      var dayArr = ["2020_05_17", "2020_05_10"],
        userID = "5b186ae490d3bf0b45dfe4c4",
        fnlRes = [],
        querySkltn = `match(u:User{id:"${userID}"})-[:{fnldate}]->(p:Post) return p order by p.createdAt;`,
        relSkltn = `POSTED_ON_{date}|`,
        tempRel = "";
      const runQuery = (query) => {
        return new Promise((resolve, reject) => {
          session
            .run(`${query}`)
            .then((res) => {
              // console.log(`running for ${date}`);
              //console.log(res.summary);
              // var temp = [];
              // for (let index = 0; index < res.records.length; index++) {
              //   temp.push(res.records[index].get("p").properties);
              //   if (index == res.records.length - 1) {
              //     resolve(temp);
              //   }
              // }
              resolve(res);
            })
            .catch((e) => {
              reject(e);
            });
        });
      };
      for (let index = 0; index < dayArr.length; index++) {
        tempRel = tempRel.concat(relSkltn.replace("{date}", dayArr[index]));
      }
      tempRel = tempRel.slice(0, -1);
      querySkltn = querySkltn.replace("{fnldate}", tempRel);
      console.log(querySkltn);
      var res = await runQuery(querySkltn);
      fnlRes.push(res);
      session.close();
      driver.close();
      var t1 = performance.now();
      console.log("Call took " + (t1 - t0) + " milliseconds.");

2 REPLIES 2

Hi Sai,

As I said in the neode issue, the driver you are using is exactly the same as the one used in Neo4j Browser so the issue won't be the driver itself.

A couple of things to check:

  • Are you querying with the same user id and getting the exact same results? If the user you are querying here has many POSTED_ON_* relationships then the query will take longer.
  • Do you have an index on constraint on the id property for User? If not, and you have added more users since the original query this will be slower
  • Closing the session and driver may also take time, but this won't be accounted for in the time calculated in the time

It is also a good idea to get into the practise of using $parameters - this will speed up subsequent queries because neo4j won't have to profile the query before it runs. You also protect yourself against the user injecting malicious commands into the query.

You also don't need the runQuery function because session.run already returns a promise. You could optimise the code into something like the following:

const dayArr = ["2020_05_17", "2020_05_10"]

const query = `
    MATCH (n:User {id: $id})-[:${ dayArr.map(date => `POSTED_ON_${date}`).join('|') }]->(p:Post) 
    RETURN p 
    ORDER BY p.createdAt
`
const params = { id:  '5b186ae490d3bf0b45dfe4c4' }

session.run(query, params)
    .then(res => res.records.map(row => row.get('p')))
    .then(arrayOfPostNodes => {
        // Do something with the array of post nodes
    })

The result of the query also comes with a summary that you can double check the performance on:

const consumedAfter = res.summary.resultConsumedAfter.toNumber()
const availableAfter = res.summary.resultAvailableAfter.toNumber()

return `
    Started streaming ${res.records.length} record${res.records.length === 1 ? '' : 's'} 
    after ${availableAfter}ms 
    and completed after ${consumedAfter}ms
`

sai1
Node Link

I do have indexes on it but I found the issue. The issue wasn't with the driver. I have my neo4j enterprise on GCP and I am trying on my local machine to connect to that instance and run the query. When I deployed the same JS script onto one of the VM, I could see the results are coming in 30ms. the time dropped from 400ms to 30ms. I am thinking this 30ms is due to opening and closing of session object and all but the 400ms might be because of network latency. I am not too sure whether 30ms is a good sign but as of now for me 400 to 30ms is a huge thing.

Also, I will remember this parameter usage.Thanks for the reply Adam