ActiveRecord中的limit(1)与one()的区别

 1 (new \yii\db\Query())->from('user')->limit(1)->one()
 2 
 3 public function one($db = null)
 4 {
 5     $row = parent::one($db);
 6     if ($row !== false) {
 7         $models = $this->populate([$row]);
 8         return reset($models) ?: null;
 9     }
10 
11     return null;
12 }

 

one()本质是从取出的结果集中取出第一条,即结果集可能很大,取第一条。条件语句中不会加上limit 1条件。
如果你清楚的知道查询将会只返回1行或几行数据(例如:通过某些主键来查询),这很好也提倡这样做。
但是,如果查询结果有可能返回大量的数据时,那么你应该显示调用limit(1)方法,以改善性能。

相关文章:

  • 2022-12-23
  • 2018-04-08
  • 2021-11-06
  • 2022-12-23
  • 2021-12-09
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-09-05
  • 2021-12-12
  • 2021-04-28
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案