gpt4 book ai didi

Mysql查询优化或缩短查询

转载 作者:行者123 更新时间:2023-11-29 10:28:13 24 4
gpt4 key购买 nike

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

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

最佳答案

摆脱低效

   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 对进行重复数据删除。

关于Mysql查询优化或缩短查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47904613/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com