[Solved] sql query is slow how to optimise

EverSQL Database Performance Knowledge Base

sql query is slow how to optimise

enter image description here this is my exection plan of my query

i want to optimize the query my table structure is given below

this is my collection table

this is my transaction table

SELECT DISTINCT 
        c.transaction_id, 
        c.property_id, 
        c.ward_id,
        w.ward_no, 
        c.holding_no, 
        c.collector_id, 
        t.payment_mode, 
        t.date, 
        t.payment_ref, 
        t.dd_date, 
        t.bank_name, 
        t.branch, 
        t.amount, 
        t.discount, 
        t.transaction_no, 
        t.pos_no, 
        t.sms_status, 
        t.remarks, 
        ch.id AS cheque_id, 
        ch.check_no, 
        ch.bank, 
        ch.check_date, 
        ch.amount AS chk_amount, 
        ch.reconcilation_date, 
        ch.chk_status, 
        ch.status AS isValid, 
        ch.bank_reconcilation_date, 
        t.penalty
    FROM  
        dbo.tbl_collection_master AS c 
            INNER JOIN dbo.tbl_transaction_master AS t 
                ON c.transaction_id = t.id 
                LEFT OUTER JOIN dbo.tbl_cheque_details AS ch 
                    ON t.id = ch.transaction_id 
            left join tbl_Ward_Master w 
                on c.ward_id = w.id

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. 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 `tbl_Ward_Master` ADD INDEX `tbl_master_idx_id` (`id`);
ALTER TABLE `tbl_cheque_details` ADD INDEX `tbl_details_idx_transaction_id` (`transaction_id`);
ALTER TABLE `tbl_transaction_master` ADD INDEX `tbl_master_idx_id` (`id`);
The optimized query:
SELECT
        DISTINCT c.transaction_id,
        c.property_id,
        c.ward_id,
        w.ward_no,
        c.holding_no,
        c.collector_id,
        t.payment_mode,
        t.date,
        t.payment_ref,
        t.dd_date,
        t.bank_name,
        t.branch,
        t.amount,
        t.discount,
        t.transaction_no,
        t.pos_no,
        t.sms_status,
        t.remarks,
        ch.id AS cheque_id,
        ch.check_no,
        ch.bank,
        ch.check_date,
        ch.amount AS chk_amount,
        ch.reconcilation_date,
        ch.chk_status,
        ch.status AS isValid,
        ch.bank_reconcilation_date,
        t.penalty 
    FROM
        dbo.tbl_collection_master AS c 
    INNER JOIN
        dbo.tbl_transaction_master AS t 
            ON c.transaction_id = t.id 
    LEFT OUTER JOIN
        dbo.tbl_cheque_details AS ch 
            ON t.id = ch.transaction_id 
    LEFT JOIN
        tbl_Ward_Master w 
            ON c.ward_id = w.id

Related Articles



* original question posted on StackOverflow here.