Search code examples
mysqlsqlquery-optimization

Query Optimization for large database


Hi i am in need of help to optimize a query for large database records above 1 Millions . Current query is taking 27-30 seconds to execute.

SELECT SQL_CALC_FOUND_ROWS
candidate.candidate_id AS candidateID,
candidate.candidate_id AS exportID,
candidate.is_hot AS isHot,
candidate.date_modified AS dateModifiedSort,
candidate.date_created AS dateCreatedSort,
candidate.first_name AS firstName,
candidate.last_name AS lastName,
candidate.city AS city,
candidate.state AS state,
candidate.key_skills AS keySkills,
owner_user.first_name AS ownerFirstName,
owner_user.last_name AS ownerLastName,
CONCAT(owner_user.last_name,
        owner_user.first_name) AS ownerSort,
DATE_FORMAT(candidate.date_created, '%m-%d-%y') AS dateCreated,
DATE_FORMAT(candidate.date_modified, '%m-%d-%y') AS dateModified,
candidate.email2 AS email2 FROM
candidate
    LEFT JOIN
user AS owner_user ON candidate.owner = owner_user.user_id
    LEFT JOIN
saved_list_entry ON saved_list_entry.data_item_type = 100
    AND saved_list_entry.data_item_id = candidate.candidate_id WHERE
is_active = 1 GROUP BY candidate.candidate_id ORDER BY    dateModifiedSort 
DESC LIMIT 0 , 15

is there any method to reduce the execution time of the query. I have also added the index in tables but it is not working fine.

Indexes


Solution

  • I've changed changed the table alias in the below query, use this This Must Solve Your Problem

    SELECT SQL_CALC_FOUND_ROWS
    candidate.candidate_id AS candidateID,
    candidate.candidate_id AS exportID,
    candidate.is_hot AS isHot,
    candidate.date_modified AS dateModifiedSort,
    candidate.date_created AS dateCreatedSort,
    candidate.first_name AS firstName,
    candidate.last_name AS lastName,
    candidate.city AS city,
    candidate.state AS state,
    candidate.key_skills AS keySkills,
    user.first_name AS ownerFirstName,
    user.last_name AS ownerLastName,
    CONCAT(user.last_name,
            user.first_name) AS ownerSort,
    DATE_FORMAT(candidate.date_created, '%m-%d-%y') AS dateCreated,
    DATE_FORMAT(candidate.date_modified, '%m-%d-%y') AS dateModified,
    candidate.email2 AS email2 FROM
    candidate
        LEFT JOIN
    user ON candidate.owner = user.user_id
        LEFT JOIN
    saved_list_entry ON saved_list_entry.data_item_type = 100
        AND saved_list_entry.data_item_id = candidate.candidate_id WHERE
    is_active = 1 GROUP BY candidate.candidate_id ORDER BY    dateModifiedSort 
    DESC LIMIT 0 , 15
    

    use the below queries to create indexes for join conditions

    create index index_user user(user_id);
    
    create index index_saved_list_entry saved_list_entry(data_item_type,data_item_id);
    
    create index index_candidate candidate(is_active,candidate_id,dateModifiedSort);