MERGE Query takes too much time to execute

Hello Devs,

I am new on neo4j please help

 const response = await session.run(`
                    match (device:Device{serialNo:$serialNo}) 
                    merge (request:ThresholdRequest{oldValue:$oldValue,newValue:$newValue,key:$key,requestStatus:$requestStatus}) 
                    merge (device)-[rel:HAS_THRESHOLD_REQUEST]->(request)
                    on CREATE SET request.createdDateTime=timestamp()
                    on MATCH SET request.createdDateTime=timestamp()
                    return device,request
                    `, { serialNo, oldValue, newValue, key, requestStatus })

This is my query

But the below statement takes too much time to execute

merge (device)-[rel:HAS_THRESHOLD_REQUEST]->(request)

When i execute this query on Neo4j Desktop its executes quickly but when i execute in my code its takes too much time

I don't understand why it would take longer using the driver vs. browser. I was going to ask do you have indexes created so the match/merges will be fast? Your merge on ThresholdRequest is using four properties. Do you need all four to uniquely identify this node? Typically you match/merge on the unique identifier(s) only. This way you can create an index for the label and property(s) need for the match.

Hii @glilienfield

I will tell you my scenario

Step 1:

I have applied trigger on threshold value of device node
Using below query i update threshold value of device.

const response = await session.run(`
                    MATCH (device:Device{serialNo:$serialNo}) 
                    SET device.threshold=$threshold
                    RETURN device
                    `, { serialNo, threshold })

Step 2

When threshold value update trigger call below function which create threshold request for device.

const response = await session.run(`
                    MATCH (device:Device{serialNo:$serialNo}) 
                    MERGE (request:ThresholdRequest{nodeId:$nodeId}) 
                    SET request.newValue=$newValue,
                    request.oldValue=$oldValue,
                    request.serialNo=$serialNo,
                    request.requestStatus=$requestStatus
                    MERGE (device)-[:HAS_THRESHOLD_REQUEST]->(request)
                    on CREATE SET request.createdDateTime=timestamp()
                    on MATCH SET request.createdDateTime=timestamp()
                    RETURN device,request
                    `, { nodeId, serialNo, oldValue, newValue, key, requestStatus })

Problem is, when i update threshold value of device first time it takes too much time to execute approx. 1 min

When i update threshold value of device later it execute quickly no problem at all

Only problem is to first time execution of the query
Am i doing something wrong???

Please Help