[Solved] Is there any way to simplify union query while using multiple tables, with reference to main table ( Performance Optimization )

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. Avoid Selecting Unnecessary Columns (query line: 18): Avoid selecting all columns with the '*' wildcard, unless you intend to use them all. Selecting redundant columns may result in unnecessary performance degradation.
  3. Avoid Selecting Unnecessary Columns (query line: 34): Avoid selecting all columns with the '*' wildcard, unless you intend to use them all. Selecting redundant columns may result in unnecessary performance degradation.
  4. 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.
  5. Index Function Calls Using Generated Columns (modified query below): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index to optimize the search. Creating and indexing a generated column (supported in MySQL 5.7) will allow MySQL to optimize the search.
  6. Use UNION ALL instead of UNION (query line: 33): Always use UNION ALL unless you need to eliminate duplicate records. By using UNION ALL, you'll avoid the expensive distinct operation the database applies when using a UNION clause.
Optimal indexes for this query:
ALTER TABLE `geo_loc1` ADD INDEX `geo_loc1_idx_key1_key2` (`key1`,`key2`);
ALTER TABLE `geo_loc2` ADD INDEX `geo_loc2_idx_key1_key2` (`key1`,`key2`);
ALTER TABLE `geo_loc3` ADD INDEX `geo_loc3_idx_key1_key2` (`key1`,`key2`);
ALTER TABLE `reference` ADD INDEX `reference_idx_key1_key2_latitude` (`key1`,`key2`,`latitude`);
The optimized query:
SELECT
        reference.*,
        geo_loc1.bathy,
        geo_loc1.gravity,
        geo_loc1.magnet,
        'data1' AS type 
    FROM
        reference,
        geo_loc1 
    WHERE
        reference.latitude BETWEEN -30 AND -10 
        AND reference.longitude BETWEEN 10 AND 50 
        AND reference.date_st_date BETWEEN '2000-07-05' AND '2011-11-10' 
        AND reference.key1 = geo_loc1.key1 
        AND reference.key2 = geo_loc1.key2 
    UNION
    SELECT
        reference.*,
        geo_loc2.bathy,
        NULL AS gravity,
        geo_loc2.magnet,
        'data2' AS type 
    FROM
        reference,
        geo_loc2 
    WHERE
        reference.latitude BETWEEN -30 AND -10 
        AND reference.longitude BETWEEN 10 AND 50 
        AND reference.date_st_date BETWEEN '2000-07-05' AND '2011-11-10' 
        AND reference.key1 = geo_loc2.key1 
        AND reference.key2 = geo_loc2.key2 
    UNION
    SELECT
        reference.*,
        NULL AS bathy,
        NULL AS gravity,
        geo_loc3.magnet,
        'data3' AS type 
    FROM
        reference,
        geo_loc3 
    WHERE
        reference.latitude BETWEEN -30 AND -10 
        AND reference.longitude BETWEEN 10 AND 50 
        AND reference.date_st_date BETWEEN '2000-07-05' AND '2011-11-10' 
        AND reference.key1 = geo_loc3.key1 
        AND reference.key2 = geo_loc3.key2

Related Articles



* original question posted on StackOverflow here.