Mysql查询优化或缩短查询

时间:2017-12-20 11:06:55

标签: mysql query-optimization

我的查询需要几分钟才能运行,如何缩短此查询并使其运行得更快

SELECT *
FROM
(
SELECT distinct(ja.applied_recruiter_id) as r_ids
 FROM (`game_applied` AS ja)
 JOIN `games_post` AS jp ON `jp`.`id` = `ja`.`game_post_id`
 JOIN `user_user` AS uu ON `uu`.`id` = `ja`.`applied_recruiter_id`
 WHERE `ja`.`game_post_id` =  j_id
 AND `uu`.`user_member_type_id` =  2
 AND `jp`.`status` NOT IN ('3', '6', '7') 
 UNION
 SELECT distinct(jr.referby_user_id) as r_ids
 FROM (`game_refer` AS jr)
 JOIN `games_post` AS jp ON `jp`.`id` = `jr`.`game_id`
 JOIN `user_user` AS uu ON `uu`.`id` = `jr`.`referby_user_id`
 WHERE `jr`.`game_id` =  j_id
 AND `uu`.`user_member_type_id` =  2
 AND `jp`.`status` NOT IN ('3', '6', '7') 
 UNION
 SELECT distinct(js.referrer_id) as r_ids
 FROM (`game_share_log` AS js)
 JOIN `games_post` AS jp ON `jp`.`id` = `js`.`game_id`
 JOIN `user_user` AS uu ON `uu`.`id` = `js`.`referrer_id`
 WHERE `js`.`game_id` =  j_id
 AND `uu`.`user_member_type_id` =  2
 AND `jp`.`status` NOT IN ('3', '6', '7') 
 UNION
 SELECT distinct(uf.user_user_id) as r_ids
 FROM (`user_follow_company` AS uf)
 JOIN `user_user` AS uu ON `uu`.`id` = `uf`.`user_user_id`
 WHERE `uf`.`gamer_company_id` =  c_id
 AND `uu`.`user_member_type_id` =  2
 UNION
 SELECT distinct(rj.user_id) as r_ids
 FROM (`recruiter_game_views` AS rj)
 JOIN `games_post` AS jp ON `jp`.`id` = `rj`.`game_id`
 JOIN `user_user` AS uu ON `uu`.`id` = `rj`.`user_id`
 WHERE `rj`.`game_id` =  j_id
 AND `uu`.`user_member_type_id` =  2     
 AND `jp`.`status` NOT IN ('3', '6', '7') 
    UNION
 SELECT distinct(`jf`.`user_id`) as r_ids
 FROM (`games_favourite` AS `jf`)
 JOIN `games_post` AS `jp` ON `jp`.`id` = `jf`.`game_post_id`
 JOIN `user_user` AS `uu` ON `uu`.`id` = `jf`.`user_id`
 WHERE `jf`.`game_post_id` =  j_id
 AND `uu`.`user_member_type_id` =  2 AND `jf`.`game_favourite_status` =  '1'
 AND `jp`.`status` NOT IN ('3', '6', '7')
    UNION 
     SELECT distinct (`jiu`.`user_id`) as r_ids
 FROM (`game_insight_user` AS `jiu`)
 JOIN `game_insight` AS `ji` ON `ji`.`id` = `jiu`.`insight_id`
 WHERE `ji`.`game_post_id` = j_id
    ) AS r_ids
WHERE r_ids not in ( SELECT referby_user_id FROM game_refer_to_member jrm1 JOIN game_refer jr ON jrm1.rid = jr.id JOIN user_socialconnections AS ruef ON (jrm1.referto_addressbookid = ruef.id) JOIN user_user AS eu1 ON jr.referby_user_id = eu1.id  WHERE 1=1 AND jrm1.id in (select DISTINCT(referred_by) as referred_by from game_applied where game_post_id = j_id ))

1 个答案:

答案 0 :(得分:1)

摆脱效率低下的

   AND  jrm1.id in (
        SELECT  DISTINCT(referred_by) as referred_by
            from  game_applied
            where  game_post_id = j_id )

将其替换为

   AND NOT EXISTS ( SELECT 1 FROM game_applied
                       WHERE game_post_id = j_id
                         AND jrm1.id = referred_by )

添加复合索引:

uu:  INDEX(user_member_type_id, id)

jr:  INDEX(referby_user_id, game_id)
js:  INDEX(referrer_id, game_id)
(etc for the other variants)

(此时,因为我无法分辨j_id表格中的哪个表格,所以我不知道了。)

不要说DISTINCT(col_name)DISTINCT不是函数,适用于后面的所有列/表达式。在你的情况下无关紧要,但

SELECT DISTINCT(a), b FROM ...

相同
SELECT DISTINCT a, b FROM ...

它表示要对a,b对进行重复删除。