雄辩,在哪里慢慢咨询

时间:2014-09-10 02:44:07

标签: mysql laravel-4 eloquent

我尝试用滔滔不绝的咨询,但速度很慢。 我的代码

$users = $this->user
                     ->whereHas('friends', function($q) use($id)
                     {
                        $q->where('friend_id', '=', $id)
                          ->where('state', '=', 'accepted');
                     })
                    ->take(16)
                    ->orderBy('name','asc')
                    ->get();    

SQL生成

select  *
    from  `users`
    where  `users`.`deleted_at` is null
      and  
      ( SELECT  count(*)
            from  `friends` as `self_04b17053094a40fb70514325b892fc19`
            where  `users`.`deleted_at` is null
              and  `self_04b17053094a40fb70514325b892fc19`.`user_id` = `users`.`id`
              and  `friend_id` = '1'
              and  `state` = 'accepted'
              and  `users`.`deleted_at` is null
      ) >= 1
    order by  `name` asc
    limit  16

0 个答案:

没有答案
相关问题