发表于 2018-10-8 10:34:35

优化一个mysql语句

mysql> EXPLAIN selectu.id ,u.s_user_name,u.s_user_img,(select count(a.n_copy_count) from t_fonts ajoin t_practicerecords b on b.n_font_id=a.id where a.n_user_id=u.id and b.n_delete=0 group by a.n_user_id)as n_copy_count,case when (select count(id) from t_friendships   where n_user_id=11311 and n_friend_id=u.id)>0 then 'true' else 'false' end as is_collectedfrom t_users u   where BINARY `s_user_name` = '做字小助手'order by LENGTH(s_user_name) asc ,id asc limit 0,10;  
+----+--------------------+---------------+------------+--------+-------------------------------------------------------------+---------------------------------+---------+------------------+---------+----------+-----------------------------+
  
| id | select_type      | table         | partitions | type   | possible_keys                                             | key                           | key_len | ref            | rows    | filtered | Extra                     |
  
+----+--------------------+---------------+------------+--------+-------------------------------------------------------------+---------------------------------+---------+------------------+---------+----------+-----------------------------+
  
|1 | PRIMARY            | u             | NULL       | ALL    | NULL                                                      | NULL                            | NULL    | NULL             | 1360606 |   100.00 | Using where; Using filesort |
  
|3 | DEPENDENT SUBQUERY | t_friendships | NULL       | eq_ref | PRIMARY,PK__T_friend__3213E83F571DF1D5,user_index,n_user_id | PRIMARY                         | 8       | const,sykdb.u.id |       1 |   100.00 | NULL                        |
  
|2 | DEPENDENT SUBQUERY | a             | NULL       | ref    | PRIMARY,n_user_index                                        | n_user_index                  | 4       | sykdb.u.id       |       7 |   100.00 | Using index condition       |
  
|2 | DEPENDENT SUBQUERY | b             | NULL       | ref    | idx_fid_score_delete_uploadtime                           | idx_fid_score_delete_uploadtime | 5       | sykdb.a.id,const |       5 |   100.00 | Using index               |
  
+----+--------------------+---------------+------------+--------+-------------------------------------------------------------+---------------------------------+---------+------------------+---------+----------+-----------------------------+
  
4 rows in set, 4 warnings (0.00 sec)


页: [1]
查看完整版本: 优化一个mysql语句