Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php sql查询需要时间,因为我正在查看三个表_Php_Mysql_Sql_Aggregate_Sql Optimization - Fatal编程技术网

Php sql查询需要时间,因为我正在查看三个表

Php sql查询需要时间,因为我正在查看三个表,php,mysql,sql,aggregate,sql-optimization,Php,Mysql,Sql,Aggregate,Sql Optimization,我正在执行此查询结果很好,但问题是它花费的时间太长,有人能告诉我如何使此高效(SQL显示此查询需要2.8秒,但实际上需要10秒以上)起初我在3个表上使用join,但该查询比此查询花费的时间更长。 提前谢谢 SELECT ee_expert.expert_id , AVG( ee_expert_rating.rating_stars ) AS total_rating, ee_expert.expert_id, COUNT( DISTINCT ee_expe

我正在执行此查询结果很好,但问题是它花费的时间太长,有人能告诉我如何使此高效(SQL显示此查询需要2.8秒,但实际上需要10秒以上)起初我在3个表上使用join,但该查询比此查询花费的时间更长。 提前谢谢

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 

这应该快一点:(删除了内联分组,在这种情况下使用exists会有所帮助。)


(也可以尝试在不使用内部分组的情况下保持该状态。)

2件事会使整个过程变慢:

  • 它需要10秒钟,因为通过网络传输数据,并以HTML(phpMyAdmin?)显示,它比实际语句慢。这只是一个猜测,但在98%的情况下都是正确的

  • in()
    中使用subselect总是很慢(我认为这是因为它对每个父数据集都是递归运行的)。一个选项是使用
    EXISTS()


  • 欢迎来到SO。在发布问题时,把问题弄清楚真是太好了。事实上,我猜你是在问如何让你的查询运行得更快。第二,请,请,请-使您的查询易于阅读。解释必须有助于运行解释语句,并在下一次发布alsook。我会记住这一点,谢谢您的建议我使用的是先存在的,但这是它等于真或假,这会导致返回上一个查询返回的所有结果,从而偏离预期的结果。您可以吗建议我做任何其他事情。它可以稍微提高时间,但是否有其他方法,例如使用子需求可以帮助(我正在考虑使用交叉点,它可以提高时间吗?)如果ee_expert_categories表只包含category_id和expert_id,则可以将category id移动到ee_expert表。任何可能的索引也可以在这方面有所帮助。优化需要大量的尝试-测试-还原迭代。
    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