所以我有这个邪恶的查询,需要清理它,因为它需要大约 2 分钟才能执行。我无法更改任何表结构,但我可以将其拆分为循环中的子查询等。我使用的是 C++ 和 MySQL。
基本上选择标签,然后必须通过查询选择与标签有联合的任何用户。
这是查询,其中 123 是长度 >= 1 的 CSV 标记 ID 列表,而 josh@test.com 是要忽略的电子邮件的 CSV 电子邮件列表,长度 >= 0。我知道这要求很多,但任何建议将不胜感激。
SELECT user_id,user_primaryemail,USER_EMAIL_IS_VALID
FROM users
WHERE ( ( user_id IN ( SELECT union_target_id
FROM systemtag_union
WHERE union_systemtag_id IN ( '123' )
&& union_type = 'User'
GROUP BY union_target_id
HAVING COUNT(DISTINCT union_systemtag_id) = 0) ) )
&& user_primaryemail NOT IN ( 'josh@test.com' )
&& USER_EMAIL_IS_VALID != 'No'
GROUP BY user_primaryemail
粗表结构:
users
-----
user_id
user_primaryemail
user_email_is_valid
systemtags
-----
systemtag_id
systemtag_union
-----
union_systemtag_id (corresponds to systemtags.systemtag_id)
union_target_id (corresponds, in this case, to users.user_id)
union_type (the type of the union, irrelevant in this case)
编辑:这是 EXPLAIN 的结果,作为 CSV:
"id","select_type","table","type","possible_keys","key","key_len","ref","rows","Extra"
1,"PRIMARY","users","ALL","user_email","","","",9104,"Using where; Using temporary; Using filesort"
2,"DEPENDENT SUBQUERY","systemtag_union","index","union_systemtag_id,union_type","union_target_id","4","",8,"Using where"