@kkstar34 do you have any indexes set on your transactions table? Can you supply the migration for that table ?
Apr 7, 2021
6
Level 1
Laravel slow query for 30000 db rows
Hi
I have 32000 occurrence of transactions in my db, this query takes too long about 10 sec or more,
how can i optimize please
$data = DB::table('transactions')
->when($merchantId, function($query) use($merchantId) {
return $query->where('user_id', $merchantId);
})
->whereBetween('created_at', [
Carbon::now()->startOfYear(),
Carbon::now()->endOfYear(),
])->when($app, function($query) use($app) {
return $query->where('application_id', $app);
})
->where(function ($query) {
$query->where('status', 'CANCELLED')
->orWhere('status', 'FAILED');
})
Level 73
Try adding an index to the created_at field and you should see an performance increase.
$table->index('created_at');
https://laravel.com/docs/8.x/migrations#available-index-types
1 like
Please or to participate in this conversation.