如何优化3个内连接mysql查询?

时间:2015-09-19 03:14:33

标签: mysql sql join

我需要执行3个表连接才能收到我想要的结果。查询执行时间为20秒,这对我的应用程序来说是不可接受的。

SELECT DISTINCT test.ORDER.PLANT_NAME FROM test.ORDER
INNER JOIN test.LOAD
    ON test.ORDER.ORDER_CODE = test.LOAD.ORDER_CODE 
    AND test.ORDER.ORDER_DATE = test.LOAD.ORDER_DATE
    AND test.ORDER.PROD_CODE = test.LOAD.PROD_CODE
    AND test.ORDER.REMOVE_RSN_CODE = test.LOAD.REMOVE_RSN_CODE
INNER JOIN test.TRUCK
    ON test.TRUCK.truck_code = test.LOAD.TRUCK_CODE
    AND test.TRUCK.hler_code = "4000584"
ORDER BY PLANT_NAME ASC;

这是EXPLAIN:

 '1','SIMPLE','ORDER','ALL',NULL,NULL,NULL,NULL,'4511','Using temporary; Using filesort'
 '1','SIMPLE','TRUCK','ALL',NULL,NULL,NULL,NULL,'10100','Using where; Distinct; Using join buffer'
 '1','SIMPLE','LOAD','ALL',NULL,NULL,NULL,NULL,'13452','Using where; Distinct; Using join buffer'

对于如何使用如此多的内部联接优化查询,任何人都有任何想法吗?

1 个答案:

答案 0 :(得分:0)

SELECT DISTINCT test.ORDER.PLANT_NAME FROM test.ORDER INNER JOIN test.LOAD ON test.ORDER.ORDER_CODE = test.LOAD.ORDER_CODE AND test.ORDER.ORDER_DATE = test.LOAD.ORDER_DATE AND test.ORDER.PROD_CODE = test.LOAD.PROD_CODE AND test.ORDER.REMOVE_RSN_CODE = test.LOAD.REMOVE_RSN_CODE INNER JOIN test.TRUCK ON test.TRUCK.truck_code = test.LOAD.TRUCK_CODE WHERE test.TRUCK.hler_code = "4000584" ORDER BY PLANT_NAME ASC;

这应该对你有所帮助,但尝试对列进行索引。