2016-06-14 2 views
1

MySQL 쿼리가 실행되는 데 1 분 이상 걸리는 이유를 파악하려고합니다. 비효율적으로 프로그래밍을하고 있는지 또는 이런 지연 시간을 예상해야하는지 알 수 없습니다. 아래는 코드입니다.MySQL 쿼리의 영향

내가 검색하는 테이블에는 대부분 10,000-30,000 개의 레코드가 있습니다 (출하, shipment_financials, shipment_consignees, shipment_shippers). 성명은 또한 쿼리의 처리를 늦추고있는에

 
SELECT users.*, COUNT(DISTINCT shipments.shipment_id) as COUNT_SHIPMENTS, COUNT(DISTINCT clients.client_id) as COUNT_CLIENTS, SUM(shipment_financials.shipment_financial_revenue) as SUM_USER_REVENUE, SUM(shipment_financials.shipment_financial_cost) as SUM_USER_COST 
FROM users 
JOIN shipments ON shipments.shipment_details_sales_rep = users.user_id 
LEFT JOIN shipment_financials ON shipment_financials.shipment_financial_shipment_id = shipments.shipment_id 
JOIN clients ON clients.client_id = shipments.shipment_details_client 
JOIN shipment_consignees ON shipment_consignees.shipment_consignee_shipment_id = shipments.shipment_id 
JOIN shipment_shippers ON shipment_shippers.shipment_shipper_shipment_id = shipments.shipment_id 

WHERE shipment_consignees.shipment_consignee_id = (
     SELECT MAX(shipment_consignees.shipment_consignee_id) 
     FROM shipment_consignees 
     WHERE shipments.shipment_id = shipment_consignees.shipment_consignee_shipment_id 
    ) 
    AND shipment_shippers.shipment_shipper_id = (
     SELECT MIN(shipment_shippers.shipment_shipper_id) 
     FROM shipment_shippers 
     WHERE shipments.shipment_id = shipment_shippers.shipment_shipper_shipment_id 
    ) 
    AND users.user_account_id = $account_id 
    AND shipments.shipment_voided = 0 
GROUP BY users.user_id 
ORDER BY SUM_USER_REVENUE desc 
+0

을 최적화하는 데 도움이 될 것입니다

*explain SELECT users.* ,COUNT(DISTINCT shipments.shipment_id) as COUNT_SHIPMENTS ,COUNT(DISTINCT clients.client_id) as COUNT_CLIENTS ,SUM(shipment_financials.shipment_financial_revenue) as SUM_USER_REVENUE ,SUM(shipment_financials.shipment_financial_cost) as SUM_USER_COST FROM users JOIN shipments ON shipments.shipment_details_sales_rep = users.user_id LEFT JOIN shipment_financials ON shipment_financials.shipment_financial_shipment_id = shipments.shipment_id JOIN clients ON clients.client_id = shipments.shipment_details_client JOIN shipment_consignees ON shipment_consignees.shipment_consignee_shipment_id = shipments.shipment_id JOIN shipment_shippers ON shipment_shippers.shipment_shipper_shipment_id = shipments.shipment_id WHERE shipment_consignees.shipment_consignee_id = ( SELECT MAX(shipment_consignees.shipment_consignee_id) FROM shipment_consignees WHERE shipments.shipment_id = shipment_consignees.shipment_consignee_shipment_id ) AND shipment_shippers.shipment_shipper_id = ( SELECT MIN(shipment_shippers.shipment_shipper_id) FROM shipment_shippers WHERE shipments.shipment_id = shipment_shippers.shipment_shipper_shipment_id ) AND users.user_account_id = [TEST ACCOUNT ID] AND shipments.shipment_voided = 0 GROUP BY users.user_id ORDER BY SUM_USER_REVENUE desc;* 

명령

다음 쿼리를 사용하여 설명 할 수있다. –

+0

그것을'EXPLAIN'하고 최적화를하십시오. – Blank

+0

인덱스를 사용합니까? 성능이 크게 향상됩니다. – andrew

답변

0

주로 인라인 쿼리 : 여기

는 코드입니다. 그러나 필연적이며 인라인 쿼리를 사용해야하는 경우 "LEFT OUTER JOIN"을 수행하여 "JOIN"의 출력을 줄일 수 있습니다.

"users. *"또한 테이블에서 전체 레코드를 가져 오므로 속도가 느려지지만 여전히 쿼리 속도가 느려지는 경우 가장 큰 문제가됩니다.

0

조인과 다중 쿼리입니다. 트릭은 필요한 모든 정보를 한 테이블 (출하 테이블)에 저장하는 것이 었습니다. 그런 식으로 여러 테이블에 가입 할 필요가 없었습니다.

가장 재미있는 방법은 아니지만 사람이 속도를 높였습니다. 5, 여러 상관 관계 서브 쿼리를 조인 기록의 많은 수의 :

+1

그건 많은 해결책처럼 들리지 않습니다 :-( – Strawberry

0

당신은 당신이 있기 때문에 빠른 실행하지 쿼리