title: Neo4J - How to do post processing UNION (pagination)
body: <p>I'm writing a cypher query to load data from my Neo4J DB, this is my data model</p>
  
  <p><a href="https://i.stack.imgur.com/zN0aH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zN0aH.png" alt="data model"></a></p>
  
  <p>So basically what I want is a query to return a Journal with all of its properties and everything related to it, Ive tried doing the simple query but it is not performant at all and my ec2 instance where the DB is hosted runs out of memory quickly
      <code>MATCH p=(j:Journal)-[*0..]-(n) RETURN p</code></p>
  
  <p>I managed to write a query using UNIONS</p>
  
  <pre><code>`MATCH p=(j:Journal)&lt;-[:BELONGS_TO]-(at:ArticleType) RETURN p 
  UNION 
  MATCH p=(j:Journal)&lt;-[:OWNS]-(jo:JournalOwner) RETURN p 
  UNION  
  MATCH p=(j:Journal)&lt;-[:BELONGS_TO]-(s:Section) RETURN p 
  UNION 
   MATCH p=(j:Journal)-[:ACCEPTS]-&gt;(fc:FileCategory) RETURN p 
  UNION
  MATCH p=(j:Journal)-[:CHARGED_BY]-&gt;(a:APC) RETURN p 
  UNION
  MATCH p=(j:Journal)-[:ACCEPTS]-&gt;(sft:SupportedFileType) RETURN p 
  UNION
  MATCH p=(j:Journal)&lt;-[:BELONGS_TO|:CHILD_OF*..]-(c:Classification) RETURN p
  </code></pre>
  
  <p>SKIP 0 LIMIT 100`</p>
  
  <p>The query works fine and its performance is not bad at all, the only problem I'm finding is in the limit, I've been googling around and I've seen that post-processing queries with UNIONS is not yet supported.</p>
  
  <p><strong>The referenced github issue is not yet resolved, so post processing of UNION is not yet possible</strong> <a href="https://github.com/neo4j/neo4j/issues/1879" rel="nofollow noreferrer">github link</a></p>
  
  <p>Logically the first thing I tried when I came across this issue was to put the pagination on each individual query, but this had some weird behaviour that didn't make much sense to myself.</p>
  
  <p>So I tried to write the query without using UNIONS, I came up with this</p>
  
  <pre><code>`MATCH (j:Journal)
  WITH j LIMIT 10
  MATCH pa=(j)&lt;-[:BELONGS_TO]-(a:ArticleType) 
  MATCH po=(j)&lt;-[:OWNS]-(o:JournalOwner)
  MATCH ps=(j)&lt;-[:BELONGS_TO]-(s:Section)
  MATCH pf=(j)-[:ACCEPTS]-&gt;(f:FileCategory)
  MATCH pc=(j)-[:CHARGED_BY]-&gt;(apc:APC)
  MATCH pt=(j)-[:ACCEPTS]-&gt;(sft:SupportedFileType)
  MATCH pl=(j)&lt;-[:BELONGS_TO|:CHILD_OF*..]-(c:Classification)
  RETURN pa, po, ps, pf, pc, pt, pl`
  </code></pre>
  
  <p>This query however breaks my DB, I feel like I'm missing something essential for writing CQL queries... </p>
  
  <p>I've also looked into COLLECT and UNWIND in this <a href="https://neo4j.com/blog/cypher-union-query-using-collect-clause/" rel="nofollow noreferrer">neo blog post</a> but couldn't really make sense of it.</p>
  
  <p>How can I paginate my query without removing the unions? Or is there any other way of writing the query so that pagination can be applied at the Journal level and the performance isn't affected?</p>
  
  <p>--- EDIT ---</p>
  
  <p>Here is the execution plan for my second query</p>
  
  <p><a href="https://i.stack.imgur.com/q702x.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/q702x.png" alt="enter image description here"></a></p>