简体   繁体   中英

How to optimize 3 inner join mysql query?

I am required to execute a 3 table join in order to receive the result that I desire. The query has been taking 20 seconds to execute which is unacceptable for my application.

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;

Here is the 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'

Anyone have any ideas on how to optimize a query with so many inner joins ?

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;

这对您无济于事,但尝试在列上建立索引。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM