简体   繁体   English

可以在 laravel eloquent whereIn 子句中使用的数组的最大大小是多少?

[英]What is maximum size of array that can be used in laravel eloquent whereIn clause?

I am currently working in search based on relevancy of keyword.我目前正在根据关键字的相关性进行搜索。 I have customers and their following fields.我有客户及其以下领域。

 1. name
 2. designation
 3. address
 .
 ...

When search is made with keyword "Computer Engineer" Then My search displays customers in following order of matching,当使用关键字“计算机工程师”进行搜索时,我的搜索按以下匹配顺序显示客户,

1. both keyword match
2. first keyword match
3. second keyword match

In this way, I can get the list of customers.这样,我可以得到客户列表。

I want to use the customer_id of search result list in other query like:我想在其他查询中使用搜索结果列表的customer_id ,例如:

Model::whereIn('customer_id', $searchResultCustomerIds)->get();

There may be any number of customers in the search result based on no of customers in database.根据数据库中没有客户,搜索结果中可能有任意数量的客户。 What is the maximum limit of size of array that can be used in eloquent whereIn clause? eloquent whereIn子句中可以使用的数组大小的最大限制是多少?

Around 1500 entries.大约1500个条目。 I think that is a Laravel limitation on eloquent.我认为这是对 eloquent 的 Laravel 限制。 If the whereIn query return 0 results, do a whereRaw instead, like the following: (supposing the wherein is on id column)如果 whereIn 查询返回 0 结果,请改为执行 whereRaw,如下所示:(假设 where 在 id 列上)

Obs: NEVER use $query->get() on a extremely large query result. Obs:永远不要在非常大的查询结果上使用 $query->get()。 If you're expecting a big return, paginate or do a cursor().如果您期望获得丰厚回报,请分页或执行 cursor()。

// using cursor, for each item, laravel relases the item from the memory, if you do a ->get(), laravel stores everything at memory, you can down your server doing that. // using cursor, for each item, laravel relases the item from the memory, if you do a ->get(), laravel stores everything at memory, you can down your server doing that.

foreach($query->cursor() as $item) {} 

// continuing // 继续

$ids = [1,2,3,4,........ 9000,9001,9002];
$query = Model::query();
$rawwhereIn = 'id IN (\'' . implode('\',\'', (array)$ids) . '\')';
$query->whereRaw($rawwhereIn);

foreach($query->cursor() as $item) {
   echo "$item->id found";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM