[Solved] Select column from another table if it matches a content

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 Subqueries (query line: 21): 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.
  2. Avoid Subqueries (query line: 53): 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. 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.
  4. Replace In Subquery With Correlated Exists (modified query below): In many cases, an EXISTS subquery with a correlated condition will perform better than a non correlated IN subquery.
Optimal indexes for this query:
ALTER TABLE `icinga_customvariables` ADD INDEX `icinga_customvaria_idx_varvalue_object_id` (`varvalue`,`object_id`);
ALTER TABLE `icinga_hostgroup_members` ADD INDEX `icinga_members_idx_host_id` (`host_object_id`);
ALTER TABLE `icinga_hostgroups` ADD INDEX `icinga_hostgroups_idx_alias_hostgroup_id` (`alias`,`hostgroup_id`);
ALTER TABLE `icinga_objects` ADD INDEX `icinga_objects_idx_object_id` (`object_id`);
ALTER TABLE `icinga_objects` ADD INDEX `icinga_objects_idx_is_active_object_id` (`is_active`,`object_id`);
ALTER TABLE `icinga_services` ADD INDEX `icinga_services_idx_service_id` (`service_object_id`);
ALTER TABLE `icinga_servicestatus` ADD INDEX `icinga_servicestat_idx_state_sched_curre_probl_servi_last` (`state_type`,`scheduled_downtime_depth`,`current_state`,`problem_has_been_acknowledged`,`service_object_id`,`last_check`);
The optimized query:
SELECT
        icinga_objects.object_id,
        icinga_objects.name1 AS host_name,
        icinga_objects.name2 AS ServiceName,
        'service' AS Type,
        icinga_servicestatus.last_check AS LastCheckTime,
        icinga_servicestatus.last_hard_state_change AS LastStateChange,
        TIMEDIFF(now(),
        icinga_servicestatus.last_hard_state_change) AS SinceTime,
        CASE 
            WHEN icinga_servicestatus.current_state = 0 THEN '0' 
            WHEN icinga_servicestatus.current_state = 1 THEN '2' 
            WHEN icinga_servicestatus.current_state = 2 THEN '3' 
            ELSE '3' END AS state 
FROM
icinga_objects,
icinga_servicestatus,
icinga_services 
WHERE
icinga_servicestatus.service_object_id IN (
    SELECT
        icinga_services.service_object_id 
    FROM
        icinga_services 
    WHERE
        EXISTS (
            SELECT
                1 
            FROM
                icinga_hostgroup_members 
            WHERE
                (
                    EXISTS (
                        SELECT
                            1 
                        FROM
                            icinga_hostgroups 
                        WHERE
                            (
                                icinga_hostgroups.alias = 'MY-HOSTGROUP-TO-FILTER'
                            ) 
                            AND (
                                icinga_hostgroup_members.hostgroup_id = icinga_hostgroups.hostgroup_id
                            )
                    )
                ) 
                AND (
                    icinga_services.host_object_id = icinga_hostgroup_members.host_object_id
                )
            )
    ) 
    AND icinga_servicestatus.service_object_id NOT IN (
        SELECT
            icinga_services.service_object_id 
        FROM
            icinga_services 
        WHERE
            EXISTS (
                SELECT
                    1 
                FROM
                    icinga_objects 
                WHERE
                    (
                        icinga_objects.is_active = 1 
                        AND EXISTS (
                            SELECT
                                1 
                            FROM
                                icinga_customvariables 
                            WHERE
                                (
                                    icinga_customvariables.varvalue = '8x5'
                                ) 
                                AND (
                                    icinga_objects.object_id = icinga_customvariables.object_id
                                )
                        )
                    ) 
                    AND (
                        icinga_services.service_object_id = icinga_objects.object_id
                    )
                )
        ) 
        AND icinga_servicestatus.last_check > NOW() - INTERVAL 3 HOUR 
        AND icinga_servicestatus.state_type = 1 
        AND icinga_servicestatus.scheduled_downtime_depth = 0 
        AND icinga_objects.object_id = icinga_services.service_object_id 
        AND icinga_servicestatus.service_object_id = icinga_services.service_object_id 
        AND icinga_servicestatus.current_state = 2 
        AND icinga_servicestatus.problem_has_been_acknowledged = 0

Related Articles



* original question posted on StackOverflow here.