LANDE_FINANCE's avatar

Simple request with many parameters very slow.

Hi, guys. I try to execute simple query:

// $codes array contains 4k+ values
$wheels = Wheel::select(array('Wheel_Size'))->whereIn('Product_code', $codes)->get();

My first problem, that SQL Server not supported more then 2100 parameters in one query, so I must explode my $codes array by packs with 2000 values and after query I should merge that all in one collection.

But second and main problem, that query with 2000 parameters executed 11 seconds in Laravel, direct query into database via workbench executed 1 second.

Where is problem? How it resolve?

0 likes
2 replies
pmall's avatar

Where is problem? How it resolve?

Creating eloquent object is slow. You shouldn't use eloquent to query thousands of records (what's the point anyway ?).

You can have a look at the chunk method, it can create eloquent object by slices of N objects :

$query = Wheel::select(array('Wheel_Size'))->whereIn('Product_code', $codes);

$query->chunk(20, function ($wheels) {

    // $wheels is a collection of 20 wheels
    // this callback is executed for each slices of 20 models

});

Please or to participate in this conversation.