0

所以我有这个邪恶的查询,需要清理它,因为它需要大约 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"
4

2 回答 2

2

与实际答案相反,而是更详细的问题澄清......您的内部查询似乎正在查询(在此处转述)

SystemTag_Union 表中具有一个或多个列出的标签的任何用户 ID,但不同标签的计数 = 0。

这听起来像是一个矛盾的白痴...给我一些具有这些标签之一的东西,但是标签的数量= 0...就是这样...为了符合条件,必须有一个满足 WHERE 子句.

您能否阐明此查询的最终意图是什么?您是否正在尝试寻找那些可能(或没有)与您将采取行动的特定标签相关联的用户?

我实际上会将查询更改为使用不同的,例如

SELECT DISTINCT 
      U.user_id,
      U.user_primaryemail,
      U.USER_EMAIL_IS_VALID
   FROM 
      users U
         JOIN systemtag_union STU
            ON U.User_ID = STU.union_target_id
           AND STU.Union_Type = 'User'
           AND STU.union_systemtag_id IN ( '123' )
   WHERE 
          U.USER_EMAIL_IS_VALID != 'No'
      AND U.user_primaryemail NOT IN ( 'josh@test.com' )
于 2011-07-18T19:29:58.173 回答
0

So what I ended up doing was twofold. I added indexes and reoptimized my tables, which helped a bit, then I completely extracted the systemtag subquery, and stored it in a variable, which I then plugged into the bigger query. Even though the subquery only took .2 seconds, it must have been executing for each iteration of the 20k user database. Thanks so much to all of you, your guidance was indispensable.

于 2011-07-18T21:28:19.453 回答