[Solved] if join returns false use other join at once. is it possable?

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. 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 `exclusion_policies` ADD INDEX `exclusion_policies_idx_active_client_mcc_mnc` (`active`,`client_id`,`mcc`,`mnc`);
The optimized query:
SELECT
        r.id,
        IF(exp.id != 0,
        1,
        IF(exp2.id != 0,
        1,
        IF(exp3.id != 0,
        1,
        0))) AS white_list 
    FROM
        rates AS r 
    LEFT JOIN
        exclusion_policies AS exp 
            ON (
                1 = 1 
                AND exp.client_id = r.client_id 
                AND exp.mcc = r.mcc 
                AND exp.mnc = r.mnc 
                AND exp.active = 1
            ) 
    LEFT JOIN
        exclusion_policies AS exp2 
            ON (
                1 = 1 
                AND exp2.client_id = r.client_id 
                AND exp2.mcc = r.mcc 
                AND exp2.active = 1
            ) 
    LEFT JOIN
        exclusion_policies AS exp3 
            ON (
                1 = 1 
                AND exp3.client_id = r.client_id 
                AND exp3.active = 1
            )

Related Articles



* original question posted on StackOverflow here.