在oracle中有许多个类似下面这样的左外链接,导致我的sql查询速度变慢了,有什么好的办法可以避免这种情况吗 left join WO_WORK_ORDER_MATERIAL jdh_t on jdh_t.base_order_id = WEOD.ORDER_ID and jdh_t.state = 1 and jdh_t.material_id = 4340 left join WO_WORK_ORDER_MATERIAL jdh_t1 on jdh_t1.base_order_id = WEOD.ORDER_ID and jdh_t1.state = 1 and jdh_t1.material_id = 4403 left join WO_WORK_ORDER_MATERIAL jdh_t2 on jdh_t2.base_order_id = WEOD.ORDER_ID and jdh_t2.state = 1 and jdh_t2.material_id = 4341
时间: 2023-08-03 16:08:28 浏览: 267
左外链接本身并不会导致 SQL 查询变慢,但是在某些情况下,使用过多的链接可能会导致查询性能下降。在你的 SQL 查询中,多个左外链接引用了相同的表和条件,这可能是影响性能的一个因素。为了避免这种情况,可以考虑使用子查询或者临时表来代替多个左外链接。例如,可以使用以下查询来代替你的查询:
```
SELECT *
FROM WEOD
LEFT JOIN (
SELECT *
FROM WO_WORK_ORDER_MATERIAL
WHERE state = 1
AND material_id IN (4340, 4403, 4341)
) jdh_t ON jdh_t.base_order_id = WEOD.ORDER_ID AND jdh_t.material_id = 4340
LEFT JOIN (
SELECT *
FROM WO_WORK_ORDER_MATERIAL
WHERE state = 1
AND material_id IN (4340, 4403, 4341)
) jdh_t1 ON jdh_t1.base_order_id = WEOD.ORDER_ID AND jdh_t1.material_id = 4403
LEFT JOIN (
SELECT *
FROM WO_WORK_ORDER_MATERIAL
WHERE state = 1
AND material_id IN (4340, 4403, 4341)
) jdh_t2 ON jdh_t2.base_order_id = WEOD.ORDER_ID AND jdh_t2.material_id = 4341
```
这里使用了子查询来筛选符合条件的数据,然后将结果与主查询进行连接,可以减少链接的数量,从而提高查询性能。
阅读全文