[Solved] How to optimize the performance of this query

EverSQL Database Performance Knowledge Base

How to optimize the performance of this query

I have a query that takes about 5 minutes to complete. Is there any way to improve it? My database contains about 300 000 records in each of the tables.

My query:

SELECT *
FROM Client_Master B
INNER JOIN Client_Bank C ON B.client_id=C.Client_Id
LEFT JOIN CL_Client_Bank_Details_Dropout A ON A.client_id=B.Client_Id
 WHERE B.Status = 'D'
 AND A.Client_Id IS NULL

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 Selecting Unnecessary Columns (query line: 2): Avoid selecting all columns with the '*' wildcard, unless you intend to use them all. Selecting redundant columns may result in unnecessary performance degradation.
  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 `CL_Client_Bank_Details_Dropout` ADD INDEX `cl_bank_idx_client_id` (`Client_Id`);
ALTER TABLE `Client_Bank` ADD INDEX `client_bank_idx_client_id` (`Client_Id`);
ALTER TABLE `Client_Master` ADD INDEX `client_master_idx_status` (`Status`);
The optimized query:
SELECT
        * 
    FROM
        Client_Master B 
    INNER JOIN
        Client_Bank C 
            ON B.client_id = C.Client_Id 
    LEFT JOIN
        CL_Client_Bank_Details_Dropout A 
            ON A.client_id = B.Client_Id 
    WHERE
        B.Status = 'D' 
        AND A.Client_Id IS NULL

Related Articles



* original question posted on StackOverflow here.