我已经从Eloquent打开了查询日志,并且对ORM在后台进行的工作感到非常惊讶,特别是在Eager Loading中 .
让我解释:
class User extends Model{
protected $primaryKey = 'id_user';
public function child(){
return $this->hasOne('Models\Child','id_user');
}
}
然后我正在某处使用它:
User :: with('child') - > find(1);
使用 EQUAL ( = ) 运算符时,后台中的查询应该是一个简单的 WHERE ,因为Relationship是hasOne .
但是查看Query Logger,上面的操作会返回:
( Forget about the User's Query, that one is "right" )
{
"query": "select * from `childs` where `childs`.`id_user` in (?)",
"bindings": [
106
],
"time": 0.36
}
由Eloquent构建的查询使用运算符 IN 而不是 EQUAL ( = ) ,除非它们在MySQL中相同,否则这根本没有意义 .
Example:
{
"query": "select * from `childs` where `childs`.`id_user` = ?",
"bindings": [
106
],
"time": 0.36
}
我的问题是:可以优化此查询吗?这个查询比使用 EQUAL ( = ) 慢吗?
Eloquent这样做还是我对这段关系做错了?