[Solved] MySQL sorting performance problem
Looking to automatically optimize YOUR SQL query? Start for free.

EverSQL Database Performance Knowledge Base

MySQL sorting performance problem

Database type:

I have a query with subquery but subquery table has approximately 500.000 records and runing this query takes too long. How can I speed up this query? Any suggestions:

SELECT ID, VehicleID, Plate, VehicleType, 
    COALESCE(
        (SELECT EngineState 
        FROM Locations 
        WHERE Locations.VehicleID = Clients.VehicleID ORDER BY ID DESC LIMIT 1), 
        0
    ) EngineState 
FROM Clients 
WHERE ID IN (SELECT ClientID FROM UserClients WHERE [email protected]);

There are 3 more columns which query last record from Locations table:

COALESCE(
    (SELECT EngineState FROM Locations 
    WHERE Locations.VehicleID = Clients.VehicleID ORDER BY ID DESC LIMIT 1), 
    0
) EngineState

As I see sorting the results in Locations table is the performance factor there. Locations table if filled every minute by 1000 vehicles' location data. x, y, speed ...

How to optimize this SQL query?

The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Avoid Subqueries (query line: 19): We advise against using subqueries as they are not optimized well by the optimizer. Therefore, it's recommended to join a newly created temporary table that holds the data, which also includes the relevant search index.
  2. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
Optimal indexes for this query:
ALTER TABLE `Clients` ADD INDEX `clients_idx_id` (`ID`);
ALTER TABLE `Locations` ADD INDEX `locations_idx_vehicleid_id` (`VehicleID`,`ID`);
ALTER TABLE `Locations` ADD INDEX `locations_idx_id` (`ID`);
ALTER TABLE `UserClients` ADD INDEX `userclients_idx_userid` (`UserID`);
The optimized query:
SELECT
        Clients.ID,
        Clients.VehicleID,
        Clients.Plate,
        Clients.VehicleType,
        COALESCE((SELECT
            EngineState 
        FROM
            Locations 
        WHERE
            Locations.VehicleID = Clients.VehicleID 
        ORDER BY
            Locations.ID DESC LIMIT 1),
        0) EngineState 
    FROM
        Clients 
    WHERE
        Clients.ID IN (
            SELECT
                UserClients.ClientID 
            FROM
                UserClients 
            WHERE
                UserClients.UserID = @UserID
        )

Related Articles



* original question posted on StackOverflow here.