优化一个mysql语句
admin
2023-05-17 11:22:22
0

优化了一个mysql的语句,没有什么技术含量,说一下过程:

优化前语句如下,大约执行时间为1-2秒

select  u.id ,u.s_user_name,u.s_user_img,(select count(a.n_copy_count) from t_fonts a 
join 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_collected  from t_users u  
where BINARY `s_user_name` = '做字小助手'  order by LENGTH(s_user_name) asc ,id asc limit 0,10

相关的执行计划如下:

mysql> EXPLAIN select  u.id ,u.s_user_name,u.s_user_img,(select count(a.n_copy_count) from t_fonts a  join 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_collected  from 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)

对t_users表的s_user_name字段创建索引,并修改表结构

mysql> create index idx_user_name on t_users (s_user_name);
Query OK, 0 rows affected (5.55 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table t_users modify s_user_name varchar(100) binary;
Query OK, 1474778 rows affected (45.23 sec)
Records: 1474778  Duplicates: 0  Warnings: 0

修改语句把binary去掉,再次查询如下:

mysql> select  u.id ,u.s_user_name,u.s_user_img,(select count(a.n_copy_count) from t_fonts a  join 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_collected  from t_users u   where `s_user_name` = '做字小助手'  order by LENGTH(s_user_name) asc ,id asc limit 0,10;
+----+-----------------+--------------------------------+--------------+--------------+
| id | s_user_name     | s_user_img                     | n_copy_count | is_collected |
+----+-----------------+--------------------------------+--------------+--------------+
|  2 | 做字小助手 | /0/1/photo/20160729035328.jpeg |         NULL | false        |
+----+-----------------+--------------------------------+--------------+--------------+
1 row in set (0.00 sec)

不过修改表结构需谨慎。

相关内容

热门资讯

伊朗:已制定管理霍尔木兹海峡指... 新华社德黑兰5月16日电 伊朗伊斯兰议会国家安全和外交政策委员会主席易卜拉欣·阿齐兹16日在社交媒体...
微纳星空双星成功入轨!迈入批产... 来源:北京日报客户端 5月15日12时33分,由海淀企业北京微纳星空科技股份有限公司研制的泰景三号0...
摆拍盲道被撞,反对“愤怒诱饵”... □李琛5月16日,据北京警方通报:刘某(男,26岁)和江某某(女,24岁)为吸粉引流、博取关注、牟取...
原创 这... 此次美高层行程中,随行团队中两位华裔面孔格外引人注目。一位是AI领域的重量级人物黄仁勋,自带科技光环...
联合国教科文组织总干事阿纳尼一... 5月14日下午,联合国教科文组织总干事阿纳尼一行到访张江人工智能创新小镇AI应用商店。 上海于20...
小米Air手机曾接近量产 上市... 快科技5月16日消息,卢伟冰在今晚直播聊手机产品规划时,透露一个重磅内幕,小米其实早就规划过对标苹果...
《十诫》《蓝白红》编剧皮耶谢维... 澎湃新闻记者 程晓筠据多家波兰媒体报道,该国知名编剧克日什托夫·皮耶谢维奇(Krzysztof Pi...
记者卧底“濮阳到桂林六日游”低... 极目新闻记者 郭奕今年3月,极目新闻记者卧底从河南濮阳市出发的199元桂林六日游旅行团发现,参团人员...
惊人策划透出,“下一个是古巴”... 古巴局势正面临严重升级。据三位知情人士周五向美联社透露,美国司法部正准备对现年94岁的古巴前领导人劳...
人间烟火暖 家和岁月安——记2... 新华社北京5月15日电 题:人间烟火暖 家和岁月安——记2026年“最美家庭”新华社记者董博婷家是最...