[Solved] Get current cell value in DBGrid

How to optimize this SQL query?

In case you have your own slow SQL query, you can optimize it automatically here.

For the query above, the following recommendations will be helpful as part of the 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:
CREATE INDEX operdetails_idx_coper_cdetail ON OperDetails (coper,cdetail);
CREATE INDEX details_idx_ckd_nrec ON details (ckd,NREC);
The optimized query:
SELECT
        det.*,
        od1.T_EQ T_SHABLON_EQ,
        od1.T_NV T_SHABLON_NV,
        od1.T_PRIM T_SHABLON_PRIM,
        od2.T_EQ T_PRAVKA_EQ,
        od2.T_NV T_PRAVKA_NV,
        od2.T_PRIM T_PRAVKA_PRIM,
        od3.T_EQ T_VALCOV_EQ,
        od3.T_NV T_VALCOV_NV,
        od3.T_PRIM T_VALCOV_PRIM,
        od4.T_EQ T_REZKA2_EQ,
        od4.T_NV T_REZKA2_NV,
        od4.T_PRIM T_REZKA2_PRIM 
    FROM
        CMKNEW.details det 
    LEFT JOIN
        CMKNEW.OperDetails od1 
            ON det.nrec = od1.cdetail 
            AND 81 = od1.coper 
    LEFT JOIN
        CMKNEW.OperDetails od2 
            ON det.nrec = od2.cdetail 
            AND 82 = od2.coper 
    LEFT JOIN
        CMKNEW.OperDetails od3 
            ON det.nrec = od3.cdetail 
            AND 83 = od3.coper 
    LEFT JOIN
        CMKNEW.OperDetails od4 
            ON det.nrec = od4.cdetail 
            AND 84 = od4.coper 
    WHERE
        det.ckd = :CKD 
    ORDER BY
        det.NREC

Related Articles



* original question posted on StackOverflow here.