したがって、この不浄なクエリがあり、実行に約 2 分かかるため、クリーンアップする必要があります。テーブル構造を変更することはできませんが、ループなどでサブクエリに分割することはできます。C++ と MySQL を使用しています。
基本的にタグが選択され、そのタグと結合しているユーザーをクエリで選択する必要があります。
これがクエリです。123 は長さ >= 1 の CSV タグ ID リストであり、josh@test.com は長さ >= 0 の無視する電子メールの CSV 電子メール リストです。アドバイスをいただければ幸いです。
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)
編集: CSV としての EXPLAIN の結果は次のとおりです。
"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"