我的環境的簡短說明:學說:leftJoin上表產生不必要的查詢
- 的Symfony:1.4.8
- 學說:1.2.3
- Centos的5.5(PHP 5.3.3)
- MySQL的52年5月1日
我已經建立在Symfony的一個新項目,這裏的項目模式:
# Car
RjCar:
actAs: { Timestampable: ~ }
columns:
id: { type: integer(4), unsigned: true, primary: true, autoincrement: true }
year: { type: integer(2), unsigned: true, notnull: true }
engine_mod: { type: string(1000) }
exterior_mod: { type: string(1000) }
suspension_mod: { type: string(1000) }
audio_mod: { type: string(1000) }
vote_pos: { type: integer(4), notnull: true, unsigned: true, default: 0 }
vote_neg: { type: integer(4), notnull: true, unsigned: true, default: 0 }
views: { type: integer(4), notnull: true, unsigned: true, default: 0 }
# Foreign keys
category_id: { type: integer(1), unsigned: true, notnull: true }
category_check: { type: boolean, notnull: true, default: 0 }
user_id: { type: integer(5) }
relations:
RjCategory: { onDelete: CASCADE, local: category_id, foreign: id, foreignAlias: RjCars }
sfGuardUser: { onDelete: CASCADE, local: user_id, foreign: id, foreignAlias: RjCars }
# Category
RjCategory:
columns:
id: { type: integer(1), unsigned: true, primary: true, autoincrement: true }
name: { type: string(255), notnull: true}
# I do not include the sfGuardUser schema, but it's the default one from the plugin
當我想和類別名稱和用戶名檢索最後10輛汽車,我用在RjCarTable.class.php下面的代碼:
$last_cars = $this->createQuery('car')
->leftJoin('car.sfGuardUser user')
->leftJoin('car.RjCategory categ')
->orderBy('car.created_at DESC')
->limit(10)
->execute();
return $last_cars;
在我的網頁一切正常,我已經所有我的結果,但在調試欄中,我看到22個查詢(而不是2)。
這裏是第一個是正常的查詢輸出:
SELECT
/* Everything about the car */
r.id AS r__id,
r.year AS r__year,
r.engine_mod AS r__engine_mod,
r.exterior_mod AS r__exterior_mod,
r.suspension_mod AS r__suspension_mod,
r.audio_mod AS r__audio_mod,
r.vote_pos AS r__vote_pos,
r.vote_neg AS r__vote_neg,
r.views AS r__views,
r.type_id AS r__type_id,
r.category_id AS r__category_id,
r.category_check AS r__category_check,
r.user_id AS r__user_id,
r.created_at AS r__created_at,
r.updated_at AS r__updated_at,
/* ... hidden because irrelevant... retrieve everything about the sfGuardUser and profile... */
/* Everything about the category */
r2.id AS r2__id,
r2.name AS r2__name,
FROM rj_car r
LEFT JOIN sf_guard_user s ON r.user_id = s.id
LEFT JOIN rj_category r2 ON r.category_id = r2.id
ORDER BY r.created_at DESC
LIMIT 10
所以在那之前一切正常,除了這個查詢被20人以獲取有關每個類別的信息(2個查詢每個結果很明顯),而在前面的查詢中可以注意到這些信息是可用的。我不會把所有的人,但這裏是一些:
SELECT r.id AS r__id, r.name AS r__name FROM rj_category r WHERE (r.id = '8') LIMIT 1
SELECT r.id AS r__id, r.name AS r__name FROM rj_category r WHERE (r.id = '8') LIMIT 1
SELECT r.id AS r__id, r.name AS r__name FROM rj_category r WHERE (r.id = '9') LIMIT 1
SELECT r.id AS r__id, r.name AS r__name FROM rj_category r WHERE (r.id = '9') LIMIT 1
/* etc.. 20 times */
所以我的真正的問題是: - 爲什麼執行所有這些不必要的查詢,而第一個查詢應該有這些信息? - 爲什麼sfGuardUser表不會發生這種情況?我的RjCar對象和sfGuardUser對象之間的關係顯然與RjCar和RjCategory之間的關係相同。
如果有人已經面臨同樣的問題,我會很高興聽到它。正如我所說的一切工作正常,但我prefere這個模塊不會產生不必要的查詢,因爲它應該在我的應用程序的主頁上執行。
雖然你是正確的,有時有必要打破ORM,這絕對不是案件之一。 – 2010-11-19 00:19:52