title: laravel retrieving data from database takes too much time
body: <p>I am working with laravel project which uses a mysql database.It has some tables which has over 5 million data.it takes too much time to get these data to frontend. following is the function i use to get data. i have some filters used such as date range (from,to) search by name($seach) pagination amount(records_number) etc. but when i try to get all records it takes too much time. is there any solutions/optimizations for this matter?</p>
  
  <p>Thanks.</p>
  
  <pre><code>public function transactionListBetween($from, $to, $sort, $search, $records_number, $filter, $previous_sort) {
  
      $query = $this-&gt;accountTransactions
          -&gt;with('transactionType', 'giver', 'recipient')
          -&gt;leftJoin('tbdb_users as recipient', 'recipient.id', '=', 'tbdb_account_transaction.recipient_id')
          -&gt;leftJoin('tbdb_users as giver', 'giver.id', '=', 'tbdb_account_transaction.giver_id')
          -&gt;leftJoin('tbdb_account_transaction_type', 'tbdb_account_transaction.account_transaction_type_id', '=', 'tbdb_account_transaction_type.id')
          -&gt;where('recipient.name', 'like', "%$search%")
          -&gt;select('tbdb_account_transaction.*');
  
      if($filter) {
          if($filter == -1) {
              $transactionTypeModel = \App::make('App\Models\AccountTransactionTypeModel');
              $depositTypeIds = $transactionTypeModel-&gt;whereIn('name', ['PayPal Deposit', 'Eway Deposit', 'Bank Deposit', 'BPay Deposit', 'Poli Deposit'])-&gt;lists('id');
              $query = $query-&gt;whereIn('tbdb_account_transaction.account_transaction_type_id', $depositTypeIds);
          } else {
              $query = $query-&gt;where('tbdb_account_transaction.account_transaction_type_id', $filter);
          }
      }
  
      if($from) {
          $query = $query-&gt;where('tbdb_account_transaction.created_date', '&gt;=', $from);
      }
  
      if($to) {
          $query = $query-&gt;where('tbdb_account_transaction.created_date', '&lt;=', $to-&gt;endOfDay());
      }
  
      if($records_number == 'no_paginate') {
          return $query-&gt;orderBy($sort, $previous_sort)
              -&gt;get();
      } else {
          $totalAmount = $query-&gt;sum('amount');
          $collection = $query-&gt;orderBy($sort, $previous_sort)-&gt;paginate($records_number);
          $collection-&gt;totalAmount = number_format($totalAmount / 100, 2);
          return $collection;
      }
  
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/2InND.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2InND.png" alt="laravel debugger screenshot"></a></p>