MySql query taking too much time, While all columns are indexed

42 Views Asked by At

Below query is taking more then 30 seconds to fetch 10 records.

        SELECT
        `u`.*,`f`.`functional_area`,`f`.`is_active` AS `is_ft_active`,`pe`.`title`,`pe`.`company`,
    (
        SELECT
            cv_file
        FROM
            profile_cvs
        WHERE
            profile_cvs.user_id = u.id AND profile_cvs.is_active = 1 AND deleted_at IS NULL
        ORDER BY
            profile_cvs.id
        DESC
    LIMIT 1
    ) AS cv_file
    FROM
        `users` AS `u`
    LEFT JOIN `functional_areas` AS `f` ON `f`.`id` = `u`.`functional_area_id`
    LEFT JOIN `profile_experiences` AS `pe` ON `pe`.`user_id` = `u`.`id`
    LEFT JOIN `profile_cvs` AS `pc` ON `pc`.`user_id` = `u`.`id`
    LEFT JOIN `profile_summaries` AS `ps` ON `ps`.`user_id` = `u`.`id`
    LEFT JOIN `profile_educations` AS `ped` ON `ped`.`user_id` = `u`.`id`
    LEFT JOIN `profile_skills` AS `psk` ON `psk`.`user_id` = `u`.`id`
    LEFT JOIN `job_skills` AS `js` ON `js`.`id` = `psk`.`job_skill_id`
    LEFT JOIN `admins` AS `ad` ON `ad`.`id` = `u`.`created_by`
    WHERE
                (`js`.`job_skill` IN("HVAC")) OR(`f`.`functional_area` LIKE "%HVAC %") OR(`u`.`search` LIKE "%HVAC%") OR(`ps`.`summary` LIKE "% HVAC%") OR(`ped`.`degree_title` LIKE "HVAC")
            
    GROUP BY
        `u`.`id`
    ORDER BY  CASE WHEN
js.job_skill LIKE '%HVAC %' THEN 1 ELSE 0
END + CASE WHEN f.functional_area LIKE '%HVAC %' THEN 1 ELSE 0
END + CASE WHEN ps.summary LIKE '% HVAC%' THEN 1 ELSE 0
END + CASE WHEN u.search LIKE '% HVAC %' THEN 1 ELSE 0
END
DESC
,
        `u`.`is_blocked` ASC,
        `js`.`job_skill` DESC,
        `f`.`functional_area` DESC,
        `u`.`search` DESC,
        `u`.`id` DESC
    LIMIT 10;

We checked every column is indexed, which is used in this query. Also user table has less then 1,00,000 records. So please explain why this query is taking too much time.

Update 1:- Here is the explanation of the above query:-

Explantion

0

There are 0 best solutions below