dpojoxa5613 2015-11-05 08:06
浏览 282
已采纳

Laravel 4的whereIn()在找到第一个值后是否停止获取行?

Currently I only have 2 users as I'm testing; My User model doesn't allow calling the first() method on queries with whereIn().

I'm wondering if the whereIn() method call on a big user table using an array variable of ids of users will continue comparing a value after already finding it?

My code :

$users = User::whereIn('id',$table_of_ids)->remember(2)->get();

I get an error thrown when I use the first() method like so:

$users = User::whereIn('id',$table_of_ids)->remember(2)->first();

I'm not sure what it does under the hood, my second question is: is there a method equivalent to combining whereIn() and remember() and first() in one way or another?

update

my User model :

`protected $table = 'users';

protected $hidden = array('password', 'remember_token');

protected $fillable = array(

    'field1','field2',...);

public static $rules = array(

    'field1'=>'required|email|max:254|unique:users',        
    'field2'=>'required|alpha_num|max:35|unique:users',
    ...

    );

I do not have any relationships in my project, i do all queries manually, and he's my error screenshot :

enter image description here

The query fetch only id's i'm wondering what sql query does eloquent generate at first to see if the get() method on whereIn is optimized..

  • 写回答

1条回答 默认 最新

  • douhe3313 2015-11-05 15:03
    关注

    I've found the obvious solution of using the take() method which will add a limit to the query, this way mysql will stop searching after having the nth row that are generally speaking almost close to each other, which is really optimized for queries in big tables.

    $users = User::whereIn('id',$allusers)->take(count($allusers))->remember(2)->get();
    

    I can't use first() because it gives something that is not fetchable with eloquent collection methods for instance "Filter()"

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改