質問

I am executing this query the results are coming fine but the thing is that it is taking too long, can someone advise me how to make this efficient (SQL is showing that this query is taking 2.8 sec but actually its taking more than 10 sec) at first i am using join on 3 tables but that query taking more time than this one. Thanks in advance.

SELECT 
    ee_expert.expert_id , 
    AVG( ee_expert_rating.rating_stars ) AS total_rating, 
    ee_expert.expert_id, 
    COUNT( DISTINCT ee_expert_rating.rating_id ) AS rating_count 
FROM 
    ee_expert_rating 
        RIGHT JOIN ee_expert 
            ON ee_expert.expert_id = ee_expert_rating.expert_id 
WHERE 
    expert_verified_email =2 
    AND expert_brief_description != '' 
    AND expert_account_status =1 
    AND ee_expert.expert_id IN 
        (
            SELECT 
                expert_id 
            FROM 
                ee_expert_categories 
            WHERE 
                ee_expert_categories.category_id =5 
            GROUP BY 
                expert_id 
        ) 
GROUP BY 
    ee_expert.expert_id 
ORDER BY 
    rating_count DESC 
役に立ちましたか?

解決

This should be a bit faster: (removed the inline group by, and using exists in this case can help.)

SELECT
    ee_expert.expert_id ,
    AVG( ee_expert_rating.rating_stars ) AS total_rating,
    COUNT( DISTINCT ee_expert_rating.rating_id ) AS rating_count
FROM
    ee_expert_rating RIGHT JOIN
    ee_expert ON ee_expert.expert_id = ee_expert_rating.expert_id
WHERE
    expert_verified_email =2 AND          
    expert_brief_description != '' AND    
    expert_account_status =1 AND          
    exists(                               
        SELECT                            
            expert_id                     
        FROM                              
            ee_expert_categories
        WHERE
            ee_expert_categories.category_id =5 and
            ee_expert_categories.expert_id=ee_expert.expert_id
    )
GROUP BY
    ee_expert.expert_id
ORDER BY
    rating_count DESC

(Try it with keeping the IN also/without the internal group by.)

他のヒント

2 things slow down the whole thing:

  1. It takes 10 seconds, because transfering the data through the network, and display in HTML (phpMyAdmin?), it's slower than the actual statement. This is just a guess but true in 98% of all cases.

  2. Using subselect in IN() is always slow (I think because it's ran recursively for every parent dataset). An option would be using EXISTS()

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top