title: I have a laravel app that displays a table with Datatables. I am using serverSide Datatables, the dataset has morethan 15k rows
body: <p>I have a laravel app that displays a table with YajraDatatable, the data are fetched from the mongoDB. I am using serverSide Datatables, the dataset has morethan 20k rows. It's really slow and giving me error 500 server error. Everything works fine if i use limit to 50000 and it takes ame time for 100 records per page or 10 records pages.</p>
  <p>/* CONTROLLER FUNCTION */</p>
  <pre><code>            $certificates =  DatatableUtils::checkRole('AddJob')
                  -&gt;with('fittingType:name')
                  -&gt;with('company:name')
                  -&gt;with('hosType:name')
                  -&gt;with('location')
                  -&gt;where('isScraped',false)
                  -&gt;select('created_at', 'trackingNumber', 'unit', 'location_id', 'diameterNumber', 'lengthFeet', 'lengthInch', 'hawpPsi', 'job_status', 'fittingId','hoseTypeId','companyId','image')
                  -&gt;orderBy('created_at','desc');
              return Datatables::of($certificates)-&gt;addIndexColumn()
                  -&gt;filter(function ($instance) use ($request) {
                      DatatableUtils::searchColumn($request, $instance);
  
                      /* Main filter */
                      DatatableUtils::mainFilter($request, $instance);
                  })
                  -&gt;editColumn('staticTestDate', function($row){
                      return date('d/M/Y', strtotime($row-&gt;staticTestDate));
                  })
                  -&gt;editColumn('image', function($row){
                      return DatatableUtils::getImg($row);
                  })
                  -&gt;editColumn('staticReTestDate', function($row){
                      return date('d/M/Y', strtotime($row-&gt;staticReTestDate));
                  })
  
                  -&gt;addColumn('date_created', function($row){
                      return $row-&gt;created_at;
                  })
                  -&gt;editColumn('status', function($row){
                      $status = '';
                      for ($i = 0; $i &lt; $row-&gt;job_status; $i++){
                          $status =  $status.'&lt;div class=&quot;glyph&quot;&gt;
                               &lt;div class=&quot;glyph-icon simple-icon-star ml-1&quot;&gt;&lt;/div&gt;
                           &lt;/div&gt;';
                      }
                      return '&lt;div class=&quot;display-ruby&quot;&gt;'.$status.'&lt;/div&gt;';
                  })
                  -&gt;addColumn('length', function($row){
                      return $row-&gt;lengthFeet .' / ' . $row-&gt;lengthInch;
                  })
                  -&gt;addColumn('action', function($row){
                      return '&lt;a href=&quot;'.route('certificates.show', $row['_id']).'&quot; class=&quot;edit btn btn-outline-primary btn-sm&quot;&gt;View&lt;/a&gt;';
                  })
                  -&gt;rawColumns(['status','Length', 'reasons', 'action','date_created'])
                  -&gt;make(true);
          }
  </code></pre>
  <p>/* DATATABLE GENERIC FUNCTION */</p>
  <pre><code>    const columnsName = [];
      if (createdRow){
          $(&quot;#&quot;+id+ &quot; thead tr th&quot;).each(function(index){
              columnsName.push(this.innerHTML);
          });
      }
      const table = $(&quot;#&quot;+id).DataTable({
          bLengthChange: false,
          processing: true,
          serverSide: true,
          pageLength: 10,
          ajax: {
              url: url,
              type: &quot;get&quot;,
              dataType: &quot;json&quot;,
              data: function (d) {
                  // filters
                  d.dateFilterMain     = $('#dateFilterMain').val()
                  d.statusFilterMain   = $('#statusFilterMain').val()
                  d.companyFilterMain  = $('#companyFilterMain').val()
                  d.locationFilterMain = $('#locationFilterMain').val()
                  d.YearFilterMain     = $('#YearFilterMain').val()
                  d.companyFilter      = $('#companyFilter').val()
  
                  // company filter location
                  d.cFilterLocation    = $('#cFilterLocation').val()
  
                  // searches
                  d.searchDate         = $('#searchDate').val();
                  d.searchCompany      = $('#searchCompany').val();
                  d.searchType         = $('#searchType').val();
                  d.searchUnit         = $('#searchUnit').val();
                  d.searchLocation     = $('#searchLocation').val();
                  d.searchStatus       = $('#searchStatus').val();
                  d.searchTracking     = $('#searchTracking').val();
                  d.searchFittings     = $('#searchFittings').val();
                  d.searchRepairedBy   = $('#searchRepairedBy').val();
  
                  // reasons searches
                  d.searchFitting      = $('#searchFitting').val();
                  d.searchClamp        = $('#searchClamp').val();
                  d.searchTube         = $('#searchTube').val();
                  d.searchCover        = $('#searchCover').val();
  
                  // searches company
                  d.cSearchDate        = $('#cSearchDate').val();
                  d.cSearchName        = $('#cSearchName').val();
                  d.cSearchCode        = $('#cSearchCode').val();
                  d.cSearchSRetest     = $('#cSearchSRetest').val();
                  d.cSearchHRetest     = $('#cSearchHRetest').val();
                  d.cSearchVRetest     = $('#cSearchVRetest').val();
                  d.cSearchNextNumber  = $('#cSearchNextNumber').val();
                  d.cSearchEmail       = $('#cSearchEmail').val();
                  d.cSearchPhone       = $('#cSearchPhone').val();
  
  
                  // searches company user
                  d.cUserName          = $('#cUserName').val();
                  d.cUserEmail         = $('#cUserEmail').val();
                  d.cUserPhone         = $('#cUserPhone').val();
                  d.cUserCompany       = $('#cUserCompany').val();
                  d.cUserStatus        = $('#cUserStatus').val();
  
                  // searches location
                  d.searchLocationName = $('#searchLocationName').val();
                  d.searchLocationCode = $('#searchLocationCode').val();
                  d.searchLocationNum  = $('#searchLocationNum').val();
  
                  d.searchHoseName     = $('#searchHoseName').val();
  
                  // searches fitting
                  d.searchFittingName       = $('#searchFittingName').val();
                  d.searchFittingDiam       = $('#searchFittingDiam').val();
                  d.searchFittingType       = $('#searchFittingType').val();
                  d.searchFittingVendor     = $('#searchFittingVendor').val();
              }
          },
          createdRow: function (row, data, dataIndex) {
              $(row).find('td').addClass('text-center');
              if (createdRow) {
                  $.each(columnsName, function (key, column) {
                      if (column === data.month) {
                          $(row).find('td:eq(' + (parseInt(key) - 1) + ')').text(data.date);
                          $(row).find('td:eq(' + (parseInt(key) - 1) + ')').addClass(data.badge);
                      }
                  });
              }
          },
          columns: dataColumns,
          buttons: [
              'copy',
              'excel',
              'csv',
              'pdf'
          ],
          destroy: true,
          info: false,
          sDom: '&lt;&quot;row view-filter&quot;&lt;&quot;col-sm-12&quot;&lt;&quot;float-left&quot;l&gt;&lt;&quot;float-right&quot;f&gt;&lt;&quot;clearfix&quot;&gt;&gt;&gt;t&lt;&quot;row view-pager&quot;&lt;&quot;col-sm-12&quot;&lt;&quot;text-center&quot;ip&gt;&gt;&gt;',
          columnDefs: [
              { &quot;orderable&quot;: false, &quot;targets&quot;: 0 }
          ],
          language: {
              paginate: {
                  previous: &quot;&lt;i class='simple-icon-arrow-left'&gt;&lt;/i&gt;&quot;,
                  next: &quot;&lt;i class='simple-icon-arrow-right'&gt;&lt;/i&gt;&quot;
              }
          },
          drawCallback: function () {
              $($(&quot;.dataTables_wrapper .pagination li:first-of-type&quot;)).find(&quot;a&quot;).addClass(&quot;prev&quot;);
              $($(&quot;.dataTables_wrapper .pagination li:last-of-type&quot;)).find(&quot;a&quot;).addClass(&quot;next&quot;);
              $(&quot;.dataTables_wrapper .pagination&quot;).addClass(&quot;pagination-sm&quot;);
              const api = $(this).dataTable().api();
              $(&quot;#pageCountDatatable span&quot;).html(&quot;Displaying &quot; + parseInt(api.page.info().start + 1) + &quot;-&quot; + api.page.info().end + &quot; of &quot; + api.page.info().recordsTotal + &quot; items&quot;);
          }
      });
      // filter
      $('#companyFilter').on('change', function () {
          table.draw();
      } )
      $('#cFilterLocation').on('change', function () {
          table.draw();
      } )
      // searches
      $(&quot;#searchDate&quot;).on(&quot;keyup&quot;, function() {
          table.draw();
      });
      $('#searchTracking').on( 'keyup', function () {
          table.draw();
      } )
      $('#searchCompany').on( 'keyup', function () {
          table.draw();
      } )
      $('#searchUnit').on( 'keyup', function () {
          table.draw();
      } );
      $('#searchLocation').on( 'keyup', function () {
          table.draw();
      } )
      $('#searchType').on( 'keyup', function () {
          table.draw();
      } )
      $('#searchStatus').on( 'keyup', function () {
          table.draw();
      } )
      $('#searchFittings').on( 'keyup', function () {
          table.draw();
      } )
      $('#searchRepairedBy').on( 'keyup', function () {
          table.draw();
      } )
  
      $('#searchFitting').on( 'keyup', function () {
          table.draw();
      } )
      $('#searchCover').on( 'keyup', function () {
          table.draw();
      } )
      $('#searchTube').on( 'keyup', function () {
          table.draw();
      } )
      $('#searchClamp').on( 'keyup', function () {
          table.draw();
      } )
  
      // company searches
      $('#cSearchDate').on( 'keyup', function () {
          table.draw();
      } )
      $('#cSearchName').on( 'keyup', function () {
          table.draw();
      } )
      $('#cSearchCode').on( 'keyup', function () {
          table.draw();
      } )
      $('#cSearchSRetest').on( 'keyup', function () {
          table.draw();
      } )
      $('#cSearchHRetest').on( 'keyup', function () {
          table.draw();
      } )
      $('#cSearchVRetest').on( 'keyup', function () {
          table.draw();
      } )
      $('#cSearchNextNumber').on( 'keyup', function () {
          table.draw();
      } )
      $('#cSearchEmail').on( 'keyup', function () {
          table.draw();
      } )
      $('#cSearchPhone').on( 'keyup', function () {
          table.draw();
      } )
  
      // searches company user
      $('#cUserName').on( 'keyup', function () {
          table.draw();
      } )
      $('#cUserEmail').on( 'keyup', function () {
          table.draw();
      } )
      $('#cUserPhone').on( 'keyup', function () {
          table.draw();
      } )
      $('#cUserCompany').on( 'keyup', function () {
          table.draw();
      } )
      $('#cUserStatus').on( 'keyup', function () {
          table.draw();
      } )
  
      // searches location
      $('#searchLocationNum').on( 'keyup', function () {
          table.draw();
      } )
      $('#searchLocationCode').on( 'keyup', function () {
          table.draw();
      } )
      $('#searchLocationName').on( 'keyup', function () {
          table.draw();
      } )
  
      // hose type search
      $('#searchHoseName').on( 'keyup', function () {
          table.draw();
      } )
  
      // fitting type search
      $('#searchFittingVendor').on( 'keyup', function () {
          table.draw();
      } )
      $('#searchFittingType').on( 'keyup', function () {
          table.draw();
      } )
      $('#searchFittingDiam').on( 'keyup', function () {
          table.draw();
      } )
      $('#searchFittingName').on( 'keyup', function () {
          table.draw();
      } )
  
      $(&quot;#pageCountDatatable .dropdown-menu a&quot;).on(&quot;click&quot;, function(event) {
          const selText = $(this).text();
          table.page.len(parseInt(selText)).draw();
      });
      $('#btnFilterSubmit').click(function(){
          table.draw();
      });
      $('table thead tr').find('th').addClass('text-center');
  }
  </code></pre>