Closed pvlg closed 8 years ago
https://github.com/yiisoft/yii2/blob/master/docs/guide/db-active-record.md#querying-data-
Note: Neither [[yii\db\ActiveRecord::findOne()]] nor [[yii\db\ActiveQuery::one()]] will add LIMIT 1 to the generated SQL statement. If your query may return many rows of data, you should call limit(1) explicitly to improve the performance, e.g., Customer::find()->limit(1)->one()
omfg :bomb: OMFG
thank you
@pvlg ActiveQuery::one() -> Query::One() -> Command::queryOne() -> Command::queryInternal('fetch', null)
deep down there is PDOStatement::fetch
call which in fact returns only 1 row. But it still generates query without limit
I'm using postgresql database and created a test table with 2000000 records.
The following code shown such results.
Yii::beginProfile('test_without_limit');
Test::find()->one();
Yii::endProfile('test_without_limit');
// 800.6 ms test_without_limit
Yii::beginProfile('test_with_limit');
Test::find()->limit(1)->one();
Yii::endProfile('test_with_limit');
// 0.9 ms test_with_limit
yea. as I sad - Yii still generates query without limit, so this still matters
oh god why?