alexanoid
alexanoid

Reputation: 25790

Neo4j Cypher - slow query performance on first call

I have the following Cypher query:

MATCH (v:Value)-[:CONTAINS]->(hv:HistoryValue) 
WHERE v.id = 13335 
WITH hv 
ORDER BY hv.createDate DESC 
OPTIONAL MATCH (hv)-[:CREATED_BY]->(u:User) WHERE true 
WITH COLLECT({userId: u.id, historyValueId: hv.id, historyValue: hv.originalValue, historyValueDescription: hv.description, historyValueCreateDate: hv.createDate}) AS data, count(hv) as count, ceil(toFloat(count(hv)) / 100) as step 
RETURN REDUCE(s = [], i IN RANGE(0, count - 1, CASE step WHEN 0 THEN 1 ELSE step END) | s + data[i]) AS result

On the cold Neo4j database during the first access, this query works very slow but the second and the subsequent calls work well.

This is PROFILE output:

enter image description here

Is there any way to improve this query performance(add appropriate indexes and so on)?

Upvotes: 0

Views: 697

Answers (2)

cybersam
cybersam

Reputation: 66989

Probably the main reason for relative slowness upon the first query to a cold DB is that a cold DB does not yet have any data cached in memory.

Invoking the APOC procedure apoc.warmup.run right after starting up the DB may make your "first" query faster.

Upvotes: 2

Pradeep Reddy
Pradeep Reddy

Reputation: 81

Usually, first queries take time in building the execution plan and maybe a few other things(i am not sure of the list). If the problem is with every time you run the query with a different v.id, parameterizing it should give better performance.

Upvotes: 0

Related Questions