[Solved] How to optimize my sql query?

EverSQL Database Performance Knowledge Base

How to optimize my sql query?

SELECT ORG.DESCRIPTION, ORG.EXTERNALKEY,
   (SELECT P.VALUE FROM PROPERTY P, ORGANIZATION ORG WHERE P.IDX=24 AND ORG.ID=P.ID AND ORG.EXTERNALKEY='200004') AS COR_ACCOUNT,
   (SELECT ORG.DESCRIPTION FROM ORGANIZATION ORG WHERE ORG.NAME = P.VALUE) AS CPC,
   (SELECT P.VALUE FROM PROPERTY P, ORGANIZATION ORG WHERE P.IDX = 4 AND P.ID = ORG.ID AND ORG.EXTERNALKEY = '200004') AS VOEN
FROM PROPERTY P, ORGANIZATION ORG
WHERE P.IDX = 22 AND ORG.EXTERNALKEY = '200004' AND P.ID = ORG.ID;

Hi. How to optimize this sql or is this normal?

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.
  2. Use Numeric Column Types For Numeric Values (query line: 33): Referencing a numeric value (e.g. 200004) as a string in a WHERE clause might result in poor performance. Possible impacts of storing numbers as varchars: more space will be used, you won't be able to perform arithmetic operations, the data won't be self-validated, aggregation functions like SUM won't work, the output may sort incorrectly and more. If the column is numeric, remove the quotes from the constant value, to make sure a numeric comparison is done.
  3. Use Numeric Column Types For Numeric Values (query line: 12): Referencing a numeric value (e.g. 200004) as a string in a WHERE clause might result in poor performance. Possible impacts of storing numbers as varchars: more space will be used, you won't be able to perform arithmetic operations, the data won't be self-validated, aggregation functions like SUM won't work, the output may sort incorrectly and more. If the column is numeric, remove the quotes from the constant value, to make sure a numeric comparison is done.
  4. Use Numeric Column Types For Numeric Values (query line: 27): Referencing a numeric value (e.g. 200004) as a string in a WHERE clause might result in poor performance. Possible impacts of storing numbers as varchars: more space will be used, you won't be able to perform arithmetic operations, the data won't be self-validated, aggregation functions like SUM won't work, the output may sort incorrectly and more. If the column is numeric, remove the quotes from the constant value, to make sure a numeric comparison is done.
Optimal indexes for this query:
ALTER TABLE `ORGANIZATION` ADD INDEX `organization_idx_externalkey_id` (`EXTERNALKEY`,`ID`);
ALTER TABLE `ORGANIZATION` ADD INDEX `organization_idx_name` (`NAME`);
ALTER TABLE `PROPERTY` ADD INDEX `property_idx_idx_id` (`IDX`,`ID`);
The optimized query:
SELECT
        ORG.DESCRIPTION,
        ORG.EXTERNALKEY,
        (SELECT
            P.VALUE 
        FROM
            PROPERTY P,
            ORGANIZATION ORG 
        WHERE
            P.IDX = 24 
            AND ORG.ID = P.ID 
            AND ORG.EXTERNALKEY = '200004') AS COR_ACCOUNT,
        (SELECT
            ORG.DESCRIPTION 
        FROM
            ORGANIZATION ORG 
        WHERE
            ORG.NAME = P.VALUE) AS CPC,
        (SELECT
            P.VALUE 
        FROM
            PROPERTY P,
            ORGANIZATION ORG 
        WHERE
            P.IDX = 4 
            AND P.ID = ORG.ID 
            AND ORG.EXTERNALKEY = '200004') AS VOEN 
    FROM
        PROPERTY P,
        ORGANIZATION ORG 
    WHERE
        P.IDX = 22 
        AND ORG.EXTERNALKEY = '200004' 
        AND P.ID = ORG.ID

Related Articles



* original question posted on StackOverflow here.