2017-12-20 10 views
0

私のクエリを実行するために数分を要し、クエリ、それはMySQLのクエリの最適化や短縮私はこのクエリを短くして作ることができる方法

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)) 
+0

テーブル構造を提供し、達成しようとしていることを説明してください。 – Bentaye

+0

確かに..テーブル構造を提供します..データを取得するのに10分以上かかる場合 – dude

+0

Grrrrrr ...クエリでは 'j_id'がどのテーブルに入っているのかわからないので、 'SHOW CREATE TABLE'。 –

答えて

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は関数ではありません、DISTINCT(col_name)を言ってはいけません。それはあなたのケースでは問題ありませんが、

SELECT DISTINCT(a), b FROM ... 

SELECT DISTINCT a, b FROM ... 

と同じであり、それがa,bのペアが解除duppedすることを言います。

関連する問題