title: How to chop up a big, slow PostgreSQL query into smaller queries then merge?
body: <p>For the sake of optimizations, we've taken a big, slow query of ours and chopped it up into smaller queries so that overloaded nodes can offset to other nodes on the server. We then merge the results of the queries into a single results list, and return that to the frontend gridview for display. We can sort and even filter resulting data in the merged list by using Node.js functions like <code>.sort((a, b) =&gt; { ... })</code>.</p>
  <p>The problem? <strong>Pagination</strong>. How would you retain accurate sorting and filtering with this method when your data is also paginated?</p>
  <p>E.g. if I have some names in the database that get displayed on the frontend:</p>
  <pre><code>Page 1:
  Sally
  Bob
  Paul
  
  Page 2:
  Hanna
  Mo
  Carl
  
  Page 3:
  Alex
  Zoe
  Sara
  </code></pre>
  <p>And I have three names per page, sorting with our current method would do this:</p>
  <pre><code>Page 1:
  Bob
  Paul
  Sally
  
  Page 2:
  Carl
  Hanna
  Mo
  
  Page 3:
  Alex
  Sara
  Zoe
  </code></pre>
  <p>Where what we need is this:</p>
  <pre><code>Page 1:
  Alex
  Bob
  Carl
  
  Page 2:
  Hanna
  Mo
  Paul
  
  Page 3:
  Sally
  Sara
  Zoe
  </code></pre>
  <p>Our queries and databases are all pretty complicated so I can't really show any specific examples - that's okay though, I'm only looking for a high-level answer here. I don't need specifics.</p>
  <p>For reference we use JavaScript, Node.js, Postgres, and use a combination of knex and sequelize.</p>
  <p>EDIT: I'm also open to better methods to optimize. We've tried adding indexes and have experimented between different types of joins and using subqueries instead (and vise versa).</p>