title: can we avoid eloquent in large no of records
body: <p>I am using laravel eloquent and have condition where thousands of records inside database so when using eloquent relationship my query is executing slow. Should i avoid using eloquent in these satuations or any other way?</p>
  <p>here is mysql query</p>
  <pre><code>$leads=Lead::select($col)
                  -&gt;join(&quot;gc_od_leads_detail as ld&quot;, &quot;gc_od_leads.leads_id&quot;, &quot;=&quot;, &quot;ld.ld_leads_id&quot;)
                  -&gt;join(&quot;gc_od_chat as c&quot;, &quot;gc_od_leads.leads_chat_id&quot;, &quot;=&quot;, &quot;c.chat_id&quot;)
                  -&gt;join(&quot;gc_od_group as g&quot;, &quot;c.chat_group_id&quot;, &quot;=&quot;, &quot;g.group_octachat_id&quot;)
                  -&gt;where('c.chat_tags','sales')
                  -&gt;whereIn('c.chat_group_id',$filter['groups']);
              if(!empty($filter['keyword'])) {
                  $leads=$leads-&gt;where(function ($q) use ($filter) {
                             $q-&gt;where('ld_name','like', &quot;%&quot;.$filter['keyword'].&quot;%&quot;)
                             -&gt;orWhere('ld_email','like', &quot;%&quot;.$filter['keyword'].&quot;%&quot;)
                             -&gt;orWhere('ld_phoneno','like', &quot;%&quot;.$filter['keyword'].&quot;%&quot;);
                     });
              }
              if(!empty($filter['startDate']) &amp;&amp; !empty($filter['endDate'])){
                  $leads=$leads-&gt;whereBetween('leads_created_date', [$filter['startDate'].&quot; 00:00:00&quot;,$filter['endDate'].&quot; 23:59:59&quot;]);
              }
              $leads=$leads-&gt;orderBy('leads_created_date','desc');
              return $leads;
          }
  </code></pre>
  <p>I have more than 500 000 recordings in side messages and chats table. i changed query in eloquent and debugged it</p>
  <p>Query:</p>
  <pre><code>    Lead::select('leads_id','leads_chat_id')-&gt;with([&quot;detail&quot;=&gt;function($q){
                 $q-&gt;select(&quot;ld_leads_id&quot;);
             }])-&gt;with([&quot;chat&quot;=&gt;function($q){
                 $q-&gt;select(&quot;chat_id&quot;)-&gt;where(['chat_status'=&gt;1]);
             }])-&gt;where(&quot;leads_status&quot;,1)-&gt;get();
  
  Debuging Ouput
      array:3 [▼
        0 =&gt; array:3 [▼
          &quot;query&quot; =&gt; &quot;select `leads_id`, `leads_chat_id` from `gc_od_leads` where `leads_status` = ?&quot;
          &quot;bindings&quot; =&gt; array:1 [▼
            0 =&gt; 1
          ]
          &quot;time&quot; =&gt; 14.85
        ]
        1 =&gt; array:3 [▼
          &quot;query&quot; =&gt; &quot;select `ld_leads_id` from `gc_od_leads_detail` where `gc_od_leads_detail`.`ld_leads_id` in (2278918, 2278919, 2278920, 2278921, 2278922, 2278923, 2278924, 22789 ▶&quot;
          &quot;bindings&quot; =&gt; []
          &quot;time&quot; =&gt; 0.59
        ]
        2 =&gt; array:3 [▼
          &quot;query&quot; =&gt; &quot;select `chat_id` from `gc_od_chat` where `gc_od_chat`.`chat_id` in (3496457, 3496458, 3496459, 3496460, 3496461, 3496462, 3496463, 3496464, 3496465, 3496466, 34 ▶&quot;
          &quot;bindings&quot; =&gt; array:1 [▶]
          &quot;time&quot; =&gt; 4.21
        ]
      ]
  </code></pre>
  <p>i above output you can see that it get all records of leads first then going to lead detail and chats table if i only want to find out the leads having chat status =1 it will still query all leads this is what slowing my query</p>
  <p>where we use join it will not work in this way i think which will save time and space both that's my i post this question i think a lot of people having same problem and no one discuss this point</p>