我正在尋找方法來優化以下SQL查詢。我正在使用MS SQL Server 2008.我想如果有任何方法來整合外部連接,它會加快速度......如果任何人有任何輸入,它將不勝感激。謝謝查詢中使用多個外連接的查詢優化
SELECT employee.employee_id,
coalesce (room.ship_id, ship.ship_id) AS ship_id,
SUBSTRING (employee.service_id, 0, 6) AS cost_code,
employee_station.room_cat AS nws_room_cat
FROM employee
LEFT OUTER JOIN room
ON employee.ship_id = room.ship_id
AND employee.floor_id = room.floor_id
AND employee.room_id = room.room_id
LEFT OUTER JOIN ship
ON employee.ship_id = ship.ship_id
LEFT OUTER JOIN employee_station
ON COALESCE (ship.property_type, '') =
COALESCE (employee_station.property_type, '')
AND COALESCE (room.work_center_id, '') = COALESCE (employee_station.work_center_id, '')
AND COALESCE (employee.pay_plan, '') =
COALESCE (employee_station.pay_plan, '')
AND COALESCE (employee.employee_type, '') =
COALESCE (employee_station.employee_type, '')
AND COALESCE (SUBSTRING (employee.service_id, 0, 6), '') =
COALESCE (employee_station.cost_code, '')
LEFT OUTER JOIN roomtype
ON room.room_type = roomtype.room_type
LEFT OUTER JOIN dp
ON employee.service_id = dp.service_id
注意:爲了清晰起見,我特意在這裏省略了SELECT語句中的很多字段。因此,roomtype和dp表格實際上正在使用...
爲什麼LEFT JOIN的對錶'roomtype'和'dp'?他們在這裏沒有做任何事情。 – BellevueBob
@BobDuell:很好,我正想問同樣的問題。 – Kaf
哦,實際上有更多的字段是SELECT的一部分..不過我爲了清晰的目的而將它們排除在外......只是使它更簡潔。所以這些表格實際上是需要的,因爲數據正在被他們從中拉出......我只是沒有在這裏包括實際的字段。 – dido