title: Neo4j Cypher query does many DB hits
body: <p>I have the following query:</p>
  <pre><code>MATCH (dg:DecisionGroup {id: -3})-[rdgd:CONTAINS]-&gt;(childD:Vacancy ) -[:REQUIRES]-&gt;(ceNode:Requirable) 
  WHERE ceNode.id in [2, 4, 8, 9] 
  WITH childD , collect(ceNode) as ceNodes with childD, 
  apoc.coll.toSet(reduce(ceNodeLabels = [], n IN ceNodes | ceNodeLabels + labels(n))) as ceNodeLabels WHERE all(x IN ['Employment', 'Location'] WHERE x IN ceNodeLabels) 
  WITH childD  
  WHERE ( (childD.`hourlyRateUsd` &gt;= 35)  OR  (childD.`salaryUsd` &gt;= 5000) ) AND  (childD.`active` = true)  
  WITH childD MATCH (childD)-[:CONTAINS]-&gt;(childDStat:JobableStatistic) 
  MATCH (childD)-[:HAS_VOTE_ON]-&gt;(vc:Criterion) 
  WHERE vc.id IN [64, 65, 67, 71, 72, 74, 75, 76, 78, 79, 80, 81, 82, 83, 84, 85, 63] 
  WITH childD, childDStat, collect(DISTINCT vc.id) as vacancyCriterionIds 
  WHERE ALL(id IN childDStat.detailedCriterionIds WHERE id IN [64, 65, 67, 71, 72, 74, 75, 76, 78, 79, 80, 81, 82, 83, 84, 85, 63]) 
  UNWIND childDStat.detailedCriterionIds AS mCId 
  WITH childD, childDStat, mCId 
  WHERE (childDStat['criterionAvgVoteWeights.' + mCId] = 0 OR childDStat['criterionAvgVoteWeights.' + mCId] &lt;= {`80`:1.4, `84`:2.8, `72`:3.0, `83`:1.4, `82`:1.4, `71`:5.0, `81`:4.2, `77`:0.0, `76`:5.0, `65`:2.0, `64`:4.0, `75`:3.0, `74`:4.0, `85`:2.8, `63`:4.0, `79`:5.0, `68`:0.0, `78`:2.8, `67`:1.0}[toString(mCId)]) AND (childDStat['criterionExperienceMonths.' + mCId] = 0 OR childDStat['criterionExperienceMonths.' + mCId] &lt;= {`80`:0, `84`:0, `72`:48, `83`:0, `82`:0, `71`:36, `81`:0, `77`:0, `76`:0, `65`:7, `64`:96, `75`:36, `74`:72, `85`:0, `63`:60, `79`:0, `68`:0, `78`:0, `67`:4}[toString(mCId)]) 
  WITH childD, collect(mCId) as mCIds, childDStat WHERE size(mCIds) &gt;= size(childDStat.detailedCriterionIds) 
  WITH childD, childDStat 
  UNWIND childDStat.criterionIds AS cId 
  WITH childD, sum(childDStat['criterionCoefficients.' + cId] * {`80`:1.4, `84`:2.8, `72`:3.0, `83`:1.4, `82`:1.4, `71`:5.0, `81`:4.2, `77`:0.0, `76`:5.0, `65`:2.0, `64`:4.0, `75`:3.0, `74`:4.0, `85`:2.8, `63`:4.0, `79`:5.0, `68`:0.0, `78`:2.8, `67`:1.0}[toString(cId)]) as weight, sum({`80`:1, `84`:1, `72`:1, `83`:1, `82`:1, `71`:1, `81`:1, `77`:1, `76`:1, `65`:1, `64`:1, `75`:1, `74`:1, `85`:1, `63`:1, `79`:1, `68`:1, `78`:1, `67`:1}[toString(cId)]) as totalVotes, sum(childDStat['criterionCoefficients.' + cId]) as criterionCoefficientSum WITH childD, weight, totalVotes, criterionCoefficientSum 
  MATCH (dg:DecisionGroup {id: -3})-[rdgd:CONTAINS]-&gt;(childD) 
  OPTIONAL MATCH (childD)-[ru:CREATED_BY]-&gt;(u:User) 
  WITH childD, dg, rdgd, u, ru, weight, totalVotes , criterionCoefficientSum  
  ORDER BY  weight / criterionCoefficientSum DESC, childD.createdAt DESC 
  SKIP 0 LIMIT 10 
  OPTIONAL MATCH (jobable:Decision:Profile {id: 35463})  
  RETURN childD AS decision, dg, rdgd, u, ru, weight, totalVotes, 
  [ (jobable)-[vg1:HAS_VOTE_ON]-&gt;(c1:Criterion)&lt;-[:HAS_VOTE_ON]-(childD) | {criterion: c1, relationship: vg1} ] AS jobableWeightedCriteria , 
  [(jobable)-[:HAS_VOTE_ON]-&gt;(c1:Criterion)&lt;-[vg1:HAS_VOTE_ON]-(childD) | {criterion: c1, relationship: vg1} ] AS weightedCriteria , 
  [ (childD)-[:REQUIRES]-&gt;(ce:CompositeEntity) | {entity: ce} ] AS decisionCompositeEntities, 
  [ (childD)-[:REQUIRES]-&gt;(ce:CompositeEntity)-[:CONTAINS]-&gt;(trans:Translation) WHERE trans.iso6391 = 'uk' | {entityId: toInteger(id(ce)), translation: trans} ] AS decisionCompositeEntitiesTranslations, 
  [ (childD)-[:CONTAINS]-&gt;(trans:Translation) WHERE trans.iso6391 = 'uk' | {entityId: toInteger(childD.id), translation: trans} ] AS decisionTranslations
  </code></pre>
  <p>I pass every single value to the query as parameters. This is just the output for debugging.</p>
  <p>Most of the query works fine except the part at the beginning:</p>
  <p><strong>Cypher version: CYPHER 4.4, planner: COST, runtime: INTERPRETED. 781495 total db hits in 577 ms.</strong></p>
  <p><a href="https://i.stack.imgur.com/DeVsE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DeVsE.png" alt="enter image description here" /></a></p>
  <p>Please advise how to optimize/refactor this part of the query in order to reduce DB hits. Thanks!</p>