Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

w99910's avatar

DB Query Builder updateOrInsert Vs Eloquent firstOrCreate ( Performance)

I'm using laravel mongodb and trying to import large data from json files. I tried to use Eloquent's firstOrCreate and Query Builder's updateOrInsert. I thought using query builder would be faster but it turned out that it is slower than Eloquent. Can you please help me insert data fast enough because I have millions of data left? The following is my code.

Query Builder

$ids = \Cache::remember('ids', 60 * 60, function () {
                    return array_merge(...Alert::distinct('facebookId')->get()->toArray());
                });
$chunks = array_chunk($this->data,3000);
foreach ($chunks as $posts) {
              foreach ($posts as $post) {
              if (in_array($post->account->platformId, $Ids)) {
              $likeCount = $post->statistics->actual->likeCount;
              $commentCount = $post->statistics->actual->commentCount;
              DB::collection('posts')->updateOrInsert(
               [
                  'platformId' => $post->platformId
               ],
              [ 
                 'url' => $post->postUrl,
                 'accountId' => $post->account->platformId,
                 'likes' => $likeCount, 
                  'comments' => $commentCount, 
                  'date' => $post->date ?? ''
               ]);
                        }
                    }
                }

Eloquent Builder

$ids = \Cache::remember('ids', 60 * 60, function () {
                    return array_merge(...Alert::distinct('facebookId')->get()->toArray());
                });
$chunks = array_chunk($this->data,3000);
foreach ($chunks as $posts) {
              foreach ($posts as $post) {
              if (in_array($post->account->platformId, $Ids)) {
              $likeCount = $post->likeCount;
              $commentCount = $post->commentCount;
              Post::firstOrCreate(
               [
                  'platformId' => $post->platformId
               ],
              [ 
                 'url' => $post->postUrl,
                 'accountId' => $post->account->platformId,
                 'likes' => $likeCount, 
                  'comments' => $commentCount, 
                  'date' => $post->date ?? ''
               ]);
                        }
                    }
                }
0 likes
3 replies
Tray2's avatar

The querybuilder and eloquent is more or less the same thing. You use the querybuilder to extend your eloquent.

w99910's avatar

I found that the reason why it is slow in importing although I'm running the laravel horizon job queue with 22 cores is that I check if there is duplicate value before creating a new record. But I wanna filter the duplicate values. So I have two options "check if there is same record before creating" or "filter the data after importing all data". What do you think which option is better? I have approximately 100 million records.

Please or to participate in this conversation.