[Solved] MySQL: Alias table doesn\'t exists (complicated subqueries)

EverSQL Database Performance Knowledge Base

MySQL: Alias table doesn\'t exists (complicated subqueries)

Database type:

This is part of a (very) bigger query.

The error is the missing alias "clienti_con_rinnovo" (Table "clienti_con_rinnovo" does not exists). I don't need to rewrite the query (the logic is right) but probably an help to get that aliases.

If interesting (but you know yet) the order is LEFT JOIN and following the UNION.

Thank you in advance

SELECT * FROM
( SELECT
    id,
    MAX(dateTransaction) AS last_transaction_renew
    FROM transactions
    WHERE
    renew IS NOT NULL
    GROUP BY id ) AS clienti_con_rinnovo

UNION                                           

SELECT * FROM

    ( SELECT
            id,
            MAX(dateTransaction) AS last_transaction_renew
      FROM transactions
      WHERE renew IS NULL
      GROUP BY id ) AS clienti_senza_rinnovo

      LEFT JOIN clienti_con_rinnovo ON clienti_con_rinnovo.id = clienti_senza_rinnovo.id
      WHERE clienti_con_rinnovo.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: 17): 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 Subqueries (query line: 4): We advise against using subqueries as they are not optimized well by the optimizer. Therefore, it's recommended to join a newly created temporary table that holds the data, which also includes the relevant search index.
  3. Avoid Subqueries (query line: 19): We advise against using subqueries as they are not optimized well by the optimizer. Therefore, it's recommended to join a newly created temporary table that holds the data, which also includes the relevant search index.
  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. Explicitly ORDER BY After GROUP BY (modified query below): By default, the database sorts all 'GROUP BY col1, col2, ...' queries as if you specified 'ORDER BY col1, col2, ...' in the query as well. If a query includes a GROUP BY clause but you want to avoid the overhead of sorting the result, you can suppress sorting by specifying 'ORDER BY NULL'.
  6. Use UNION ALL instead of UNION (query line: 16): 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 `clienti_con_rinnovo` ADD INDEX `clienti_rinnovo_idx_id` (`id`);
ALTER TABLE `transactions` ADD INDEX `transactions_idx_renew_id` (`renew`,`id`);
ALTER TABLE `transactions` ADD INDEX `transactions_idx_id_datetransaction` (`id`,`dateTransaction`);
The optimized query:
SELECT
        * 
    FROM
        (SELECT
            transactions.id,
            MAX(transactions.dateTransaction) AS last_transaction_renew 
        FROM
            transactions 
        WHERE
            transactions.renew IS NOT NULL 
        GROUP BY
            transactions.id 
        ORDER BY
            NULL) AS clienti_con_rinnovo 
    UNION
    SELECT
        * 
    FROM
        (SELECT
            transactions.id,
            MAX(transactions.dateTransaction) AS last_transaction_renew 
        FROM
            transactions 
        WHERE
            transactions.renew IS NULL 
        GROUP BY
            transactions.id 
        ORDER BY
            NULL) AS clienti_senza_rinnovo 
    LEFT JOIN
        clienti_con_rinnovo 
            ON clienti_con_rinnovo.id = clienti_senza_rinnovo.id 
    WHERE
        clienti_con_rinnovo.id IS NULL

Related Articles



* original question posted on StackOverflow here.