Eager Loading is taking a while

Posted 1 year ago by ddotgrass

Hi everyone!

I just wanted to see if this was an expected result whilst using Eager Loading. I'm having a bit of a slowness issue.

Basically, this query:

select * from `products` where `products`.`id` in ('1', '2', '3', '4', '5', '7', '8', '11', '14', '17', '18', '19', '20', '21', '22', '23', '24', '33', '35', '86', '101') and `products`.`deleted_at` is null

is fine. It doesn't take long to run at all, a few ms.

The code for it is as simple as:

$model = $this->model->with('products')->take(1500)->get();

I haven't added the pagination yet so I'm aware of that!

Anyway, another query which does the same just with less results, seems to be taking 4 seconds to load with Eager Loading, and 2 seconds without.

The slow query has quite a large IN clause, though...which I suspect is the problem.

select `product` from `accounts` where `accounts`.`product` in ('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '70', '71', '72', '73', '74', '75', '76', '77', '78', '79', '80', '81', '82', '83', '84', '85', '86', '87', '88', '89', '90', '91', '92', '93', '94', '95', '96', '97', '98', '99', '100', '101', '102', '103', '104', '105', '106', '107', '108', '109', '110', '111', '112') and `accounts`.`deleted_at` is null
$model = $this->model->with('accounts')->take(100)->get();

I don't know if the IN clause is simply too big or there's an underlying problem that I'm not seeing/something I should investigate.

If I remove the "->with('products')" it'll load in 2 seconds, which still seems a bit much for 100 results.

The table structure is small, I'm only selecting 6 columns.

I appreciate any insights!

Please sign in or create an account to participate in this conversation.