title: Datatables server side paging codeigniter larger query with joins DOCTRINE
body: <p>I have a datatable with server side paging:</p>
  
  <pre><code>$('#answers').dataTable({
      //"processing": true,
      "serverSide": true,
      "ajax":{
          url :"/ajaxserverside/", // json datasource
          type: "post",  // method  , by default get
          'data': {
              category: category,
              topic: topic,
              lawyer: lawyer
              //action: 'search',
              // etc..
           },
          error: function(){  // error handling
  
  
          }
      }
  });
  </code></pre>
  
  <p>So my problem is that on the controller below I have some rather bigger queries that take sometime. Sometimes up to 15,000 rows. </p>
  
  <p>I have been able to get the query and get number of total records and total filtered records. 
  The problem is that every time I change pages the same controller runs again and has to execute that HUGE query and takes extra time. I wish i could find a way that it would just save the number of total records and not have to execute that HUGE query again and use that to count the number of records again.</p>
  
  <p>Below I first run the query and get the number of total records. Then I run the same query but now I let only 10 rows per page and make it start with the correct offset upon each page redraw.</p>
  
  <p>So if anyone has an idea on how to do this faster that would be great thx!</p>
  
  <pre><code>public function ajaxserverside() {
     $query1 = $this-&gt;em-&gt;createQuery('INSERT BIG QUERY HERE');
     $answers1 = $query1-&gt;getArrayResult();
     $count = count($answers1);
  
     $query = $this-&gt;em-&gt;createQuery('INSERT BIG QUERY HERE')-&gt;setMaxResults($_POST['length'])-&gt;setFirstResult($_POST['start']);
     $answers2 = $query-&gt;getArrayResult();
  
      foreach($answers2 as $answer2) {
          $sub_array = array();  
          $sub_array[] = $answer2['subject'];  
          $sub_array[] = $answer2['name'];  
          $sub_array[] = $answer2['replies'];  
          $sub_array[] = $answer2['replies'];  
  
          $data[] = $sub_array;  
      }
  
       $output = array(  
              "draw"                    =&gt;     intval($_POST["draw"]),  
              "recordsTotal"          =&gt;      intval($count),  
              "recordsFiltered"     =&gt;     intval($count),  
              "data"                    =&gt;     $data  
         );  
         echo json_encode($output); 
  }
  </code></pre>