SQL: SELECT COUNT(DISTINCT friend.id_) FROM ftsp_kh_khxx khxx JOIN ftsp_kh_pg pg ON pg.kh_khxx_id_ = khxx.id_ JOIN ftsp_infra_user u ON u.id_ = pg.infra_user_id_ AND u.is_delete_ = '0' JOIN ftsp_zj_bmyh bmyh ON bmyh.infra_user_id_ = u.id_ AND bmyh.is_delete_ = '0' JOIN ftsp_zj_bmxx bmxx ON bmyh.zj_bmxx_id_ = bmxx.id_ AND bmxx.is_delete_ = '0' JOIN ftsp_wechat_friend friend ON u.work_weixin_id_ = friend.infra_user_wxid_ AND friend.source_ = '2' AND friend.is_delete_ = '0' LEFT JOIN ftsp_wechat_kh_relation relation ON relation.kh_khxx_id_ = khxx.id_ AND relation.type_ = '1' AND friend.wxid_ = relation.wxid_ WHERE bmyh.is_active_ = '1' AND bmxx.id_ IN ('h0000000000000498698775546511360' , 'h0000000000000498698781397606400', 'h0000000000000498698787076653056', 'h0000000000000498698798514569216', 'h0000000000000498698804499791872', 'h0000000000000498698810493493248') AND bmxx.zj_zjxx_id_ = 'h0000000000000498723219407372288' AND khxx.zj_zjxx_id_ = 'h0000000000000498723219407372288' AND CONVERT( FROM_BASE64(friend.nickname_) , CHAR) LIKE CONCAT('%', '辻弎&', '%') AND EXISTS( SELECT 1 FROM ftsp_wechat_kh_relation re JOIN ftsp_kh_khxx kh ON re.kh_khxx_id_ = kh.id_ WHERE re.type_ = '1' AND re.wxid_ = friend.wxid_ AND kh.name_ LIKE CONCAT('%', '鲁创', '%')) AND khxx.zj_zjxx_id_ = 'h0000000000000498723219407372288' AND khxx.id_ IN (SELECT DISTINCT pg_.kh_khxx_id_ FROM ftsp_kh_pg pg_, ftsp_zj_bmxx bm_ WHERE pg_.zj_bmxx_id_ = bm_.id_ AND bm_.zj_zjxx_id_ = 'h0000000000000498723219407372288' AND bm_.id_ IN ('h0000000000000498698775546511360' , 'h0000000000000498698781397606400', 'h0000000000000498698787076653056', 'h0000000000000498698798514569216', 'h0000000000000498698804499791872', 'h0000000000000498698810493493248', 'h0000000000000512492012482002944', 'h0000000000000498698775546511360', 'h0000000000000498698787076653056', 'h0000000000000498698781397606400')); 执行计划: +----+-------------+----------+------------+--------+-------------------------------------------------------------------+-------------------------------+---------+---------------------------------------------------------------+------+----------+--------------------------------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+----------+------------+--------+-------------------------------------------------------------------+-------------------------------+---------+---------------------------------------------------------------+------+----------+--------------------------------------------+ | 1 | SIMPLE | bmxx | NULL | range | PRIMARY,idx_ftsp_zj_bmxx_1 | PRIMARY | 130 | NULL | 6 | 0.83 | Using where; Start temporary | | 1 | SIMPLE | bm_ | NULL | range | PRIMARY,idx_ftsp_zj_bmxx_1 | PRIMARY | 130 | NULL | 7 | 0.71 | Using where; Using join buffer (hash join) | | 1 | SIMPLE | bmyh | NULL | ref | idx_ftsp_zj_bmyh_2,idx_ftsp_zj_bmyh_1 | idx_ftsp_zj_bmyh_1 | 130 | ftsp_product.bmxx.id_ | 15 | 1.00 | Using where; Using index | | 1 | SIMPLE | u | NULL | eq_ref | PRIMARY,idx_ftsp_infra_user_7 | PRIMARY | 130 | ftsp_product.bmyh.infra_user_id_ | 1 | 10.00 | Using where | | 1 | SIMPLE | friend | NULL | ref | idx_ftsp_wechat_friend_2,idx_ftsp_wechat_friend_1 | idx_ftsp_wechat_friend_2 | 258 | ftsp_product.u.work_weixin_id_ | 54 | 1.00 | Using index condition; Using where | | 1 | SIMPLE | pg_ | NULL | ref | uk_idx_ftsp_kh_pg_275043495,idx_ftsp_kh_pg_2_f_1494918858 | idx_ftsp_kh_pg_2_f_1494918858 | 131 | ftsp_product.bm_.id_ | 71 | 100.00 | Using where; Using index | | 1 | SIMPLE | khxx | NULL | eq_ref | PRIMARY,idx_ftsp_kh_khxx_1,idx_ftsp_kh_khxx_4,idx_ftsp_kh_khxx_13 | PRIMARY | 130 | ftsp_product.pg_.kh_khxx_id_ | 1 | 5.00 | Using where | | 1 | SIMPLE | re | NULL | ref | idx_ftsp_wechat_kh_relation_1,idx_ftsp_wechat_kh_relation_3 | idx_ftsp_wechat_kh_relation_3 | 259 | ftsp_product.friend.wxid_,const | 1 | 100.00 | NULL | | 1 | SIMPLE | kh | NULL | eq_ref | PRIMARY | PRIMARY | 130 | ftsp_product.re.kh_khxx_id_ | 1 | 11.11 | Using where | | 1 | SIMPLE | relation | NULL | ref | idx_ftsp_wechat_kh_relation_1,idx_ftsp_wechat_kh_relation_3 | idx_ftsp_wechat_kh_relation_3 | 259 | ftsp_product.friend.wxid_,const | 1 | 100.00 | Using where | | 1 | SIMPLE | pg | NULL | ref | uk_idx_ftsp_kh_pg_275043495,idx_ftsp_kh_pg_3_f_1803002659 | uk_idx_ftsp_kh_pg_275043495 | 262 | ftsp_product.pg_.kh_khxx_id_,ftsp_product.bmyh.infra_user_id_ | 1 | 100.00 | Using index; End temporary | +----+-------------+----------+------------+--------+-------------------------------------------------------------------+-------------------------------+---------+---------------------------------------------------------------+------+----------+--------------------------------------------+ FORMAT=Tree -> Aggregate: count(distinct friend.id_) (cost=2.88 rows=0) -> Remove duplicate (bmxx, bmyh, friend, khxx, relation, pg) rows using temporary table (weedout) (cost=2.88 rows=0) -> Nested loop inner join (cost=2.88 rows=0) -> Nested loop left join (cost=2.88 rows=0) -> Nested loop inner join (cost=2.88 rows=0)S -> Nested loop inner join (cost=2.88 rows=0) -> Nested loop inner join (cost=2.88 rows=0) -> Nested loop inner join (cost=2.87 rows=0) -> Nested loop inner join (cost=2.87 rows=0) -> Inner hash join (no condition) (cost=2.87 rows=0) -> Filter: ((bm_.zj_zjxx_id_ = 'h0000000000000498723219407372288') and (bm_.id_ in ('h0000000000000498698775546511360','h0000000000000498698781397606400','h0000000000000498698787076653056','h0000000000000498698798514569216','h0000000000000498698804499791872','h0000000000000498698810493493248','h0000000000000512492012482002944','h0000000000000498698775546511360','h0000000000000498698787076653056','h0000000000000498698781397606400'))) (cost=9.70 rows=0) -> Index range scan on bm_ using PRIMARY over (id_ = 'h0000000000000498698775546511360') OR (id_ = 'h0000000000000498698781397606400') OR (5 more) (cost=9.70 rows=7) -> Hash -> Nested loop inner join (cost=2.87 rows=0) -> Nested loop inner join (cost=2.86 rows=0) -> Filter: ((bmxx.id_ in ('h0000000000000498698775546511360','h0000000000000498698781397606400','h0000000000000498698787076653056','h0000000000000498698798514569216','h0000000000000498698804499791872','h0000000000000498698810493493248')) and (bmxx.zj_zjxx_id_ = 'h0000000000000498723219407372288') and (bmxx.is_delete_ = 0)) (cost=2.71 rows=0) -> Index range scan on bmxx using PRIMARY over (id_ = 'h0000000000000498698775546511360') OR (id_ = 'h0000000000000498698781397606400') OR (4 more) (cost=2.71 rows=6) -> Filter: ((bmyh.is_active_ = 1) and (bmyh.is_delete_ = 0)) (cost=1.70 rows=0) -> Covering index lookup on bmyh using idx_ftsp_zj_bmyh_1 (zj_bmxx_id_=bmxx.id_) (cost=1.70 rows=15) -> Filter: ((u.is_delete_ = 0) and (u.work_weixin_id_ is not null)) (cost=2.31 rows=0) -> Single-row index lookup on u using PRIMARY (id_=bmyh.infra_user_id_) (cost=2.31 rows=1) -> Filter: ((cast(from_base64(friend.nickname_) as char charset utf8mb4) like (concat('%','辻弎&','%'))) and (friend.source_ = 2) and (friend.is_delete_ = 0)) (cost=1505.31 rows=1) -> Index lookup on friend using idx_ftsp_wechat_friend_2 (infra_user_wxid_=u.work_weixin_id_), with index condition: (u.work_weixin_id_ = friend.infra_user_wxid_) (cost=1505.31 rows=55) -> Filter: (pg_.kh_khxx_id_ is not null) (cost=437719.90 rows=93) -> Covering index lookup on pg_ using idx_ftsp_kh_pg_2_f_1494918858 (zj_bmxx_id_=bm_.id_) (cost=437719.90 rows=93) -> Filter: (khxx.zj_zjxx_id_ = 'h0000000000000498723219407372288') (cost=3.54 rows=0) -> Single-row index lookup on khxx using PRIMARY (id_=pg_.kh_khxx_id_) (cost=3.54 rows=1) -> Index lookup on re using idx_ftsp_wechat_kh_relation_3 (wxid_=friend.wxid_, type_=1) (cost=1421.98 rows=1) -> Filter: ((kh.name_ like (concat('%','鲁创','%'))) and (re.kh_khxx_id_ = kh.id_)) (cost=12.55 rows=0) -> Single-row index lookup on kh using PRIMARY (id_=re.kh_khxx_id_) (cost=12.55 rows=1) -> Filter: (relation.kh_khxx_id_ = pg_.kh_khxx_id_) (cost=1421.98 rows=1) -> Index lookup on relation using idx_ftsp_wechat_kh_relation_3 (wxid_=friend.wxid_, type_='1') (cost=1421.98 rows=1) -> Covering index lookup on pg using uk_idx_ftsp_kh_pg_275043495 (kh_khxx_id_=pg_.kh_khxx_id_, infra_user_id_=bmyh.infra_user_id_) (cost=733.85 rows=1)