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
을 최적화하는 데 도움이 될 것입니다
명령
다음 쿼리를 사용하여 설명 할 수있다. –그것을'EXPLAIN'하고 최적화를하십시오. – Blank
인덱스를 사용합니까? 성능이 크게 향상됩니다. – andrew