DevGroup-ru / dotplant2

E-Commerce CMS - Yii Framework 2 (yii2, shop)
http://dotplant.ru/
Other
641 stars 252 forks source link

Update CategoryGroup.php #284

Closed pvlg closed 8 years ago

Philosoft commented 8 years ago

oh god why?

pvlg commented 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()

Philosoft commented 8 years ago

omfg :bomb: OMFG

thank you

Philosoft commented 8 years ago

@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

pvlg commented 8 years ago

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
Philosoft commented 8 years ago

yea. as I sad - Yii still generates query without limit, so this still matters