Select the fields before group. And do you need the array.
Dec 3, 2024
17
Level 5
SQL query very fast in phpMyAdmin but slow in Laravel application
here is my sql query and it returns 1000 rows.
SELECT COUNT(*) AS `Rows`, `OrderID`
FROM `order_items`
WHERE `OrderID` IN(1,2,3,...,1000) AND `Status` != 0
GROUP BY `OrderID`
here OrderID, Status columns are indexed,
when run this query in phpmyadmin it takes 0.0082 seconds, but same query takes 0.5 upto 0.9 sec in laravel eloquent
OrderItems::selectRaw('count(*) as `Rows`, `OrderID`')
->whereIn('OrderID', $ids)
->where('Status', '!=', OrderItems::STATUS_UNCOMPLETE)
->groupBy('OrderID')
->get()
->pluck('Rows', 'OrderID')
->toArray()
I am using AWS aurora and Laravel 5.2, read write host are different.
Please or to participate in this conversation.