我一直在使用油滑,我碰到一個我似乎無法解決的問題。 我想要做的是跨多個表連接,Slick似乎直接將連接轉換爲嵌套選擇,我不認爲這是有效的。連接成爲嵌套選擇
例如,這是我使用,使加入的各種功能:
def partialReviewByPlaceIds(q: ReviewQuery, placeIds: Long*)(implicit psDAO: PlaceStatusDAO, upDAO: UserProfileDAO) = for {
((r, up), ps) <- withUP(q) join psDAO.PlaceStatuses /*.filter(_.placeId.inSet(placeIds))*/ on {
case ((r, _), ps) => r.providerPlaceId === ps.providerSpecId && ps.provider === r.provider
} if ps.placeId.inSet(placeIds)
} yield (r, up)
而且withUP
樣子:
def withUP(q: ReviewQuery) = for {
(r, a) <- q join upDAO.UserProfiles on ((r, u) => r.providerUserId === u.providerUserId && r.provider === u.provider)
} yield (r, a)
,這將直接轉化爲SQL:
SELECT
x2.x3,
x2.x4,
x2.x5,
x2.x6,
x2.x7,
x8.`url`,
x2.x9,
x2.x10,
x8.`provider_user_id`,
x2.x11,
x8.`id`,
x2.x12,
x8.`provider`,
x2.x13,
x2.x14,
x2.x15,
x8.`name`,
x2.x16,
x8.`image_url`,
x2.x17
FROM (SELECT
`created` AS x14,
`done` AS x10,
`favourite` AS x15,
`url` AS x5,
`text` AS x17,
`provider` AS x7,
`provider_review_id` AS x4,
`id` AS x16,
`read` AS x13,
`provider_place_id` AS x6,
`rating` AS x3,
`provider_user_id` AS x9,
`flagged` AS x12,
`language` AS x11
FROM `review`
ORDER BY `created` DESC) x2, `user_profile` x8, `place_status` x18
WHERE ((x18.`place_id` IN (17)) AND ((x2.x9 = x8.`provider_user_id`) AND (x2.x7 = x8.`provider`))) AND
((x2.x6 = x18.`provider_place_id`) AND (x18.`provider` = x2.x7))
LIMIT 0, 21
什麼是更好的方法來解決這個問題? (我不加入的模式,因爲我不認爲它一定需要的情況&可以看查詢統計出來。)
編輯 當我編輯partialReviewByPlaceIds
方法做一切都在爲理解而不是:
def partialReviewByPlaceIds(q: ReviewQuery, placeIds: Long*)(implicit psDAO: PlaceStatusDAO, upDAO: UserProfileDAO) = for {
((r, up), ps) <- Reviews join upDAO.UserProfiles on ((r, up) => r.providerUserId === up.providerUserId && r.provider === up.provider) join psDAO.PlaceStatuses /*.filter(_.placeId.inSet(placeIds))*/ on {
case ((r, _), ps) => r.providerPlaceId === ps.providerSpecId && ps.provider === r.provider
} if ps.placeId.inSet(placeIds)
} yield (r, up)
我得到一個平坦的查詢像:
SELECT
x2.`favourite`,
x3.`image_url`,
x2.`provider_place_id`,
x2.`id`,
x2.`rating`,
x2.`provider_user_id`,
x2.`provider`,
x3.`id`,
x3.`provider_user_id`,
x2.`done`,
x2.`flagged`,
x2.`language`,
x3.`provider`,
x2.`url`,
x2.`text`,
x2.`provider_review_id`,
x2.`read`,
x2.`created`,
x3.`name`,
x3.`url`
FROM `review` x2, `user_profile` x3, `place_status` x4
WHERE
((x4.`place_id` IN (17)) AND ((x2.`provider_user_id` = x3.`provider_user_id`) AND (x2.`provider` = x3.`provider`)))
AND ((x2.`provider_place_id` = x4.`provider_place_id`) AND (x4.`provider` = x2.`provider`))
LIMIT 0, 21
在我看來,Slick在查詢操作時會立即將查詢編譯爲SQL,如對查詢進行排序和過濾 - 這使得查詢很難組合在一起。
例如,可以期望通過review.created進行排序,而不是在子查詢中,但在聯接中。
播放框架2.5.8
播放光滑2.0.2
需要考慮的事情 - 如果您得到了您想要的結果,我會解釋分析結果,並使用手工連接比較「理想」語法。在很多情況下,我發現性能是相同的。 – easel