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()"

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

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料