优化一个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)

不过修改表结构需谨慎。

相关内容

热门资讯

河南2026年“三支一扶”计划... 针对近期备受关注的2026年河南省“三支一扶”计划招募相关舆情,河南省“三支一扶”领导小组协调办公室...
以卵击石的“汉光演习”不过是一... 昨天题为《人民解放军越强大,台海和平越有保障》的海风文章,已经反映了一部分台海局势的现实。若再把8月...
美国一养老社区将推出面向老人的... 据《独立报》8月1日报道,美国亚利桑那州一养老社区将引入一台专为老年人设计的大麻自助售卖机,居民无需...
“你头发都白了”,同守广西边防... “你头发都白了,模样还是没变,就是没有年轻时候那么帅了。”
小米手机再度涨价:旗舰最高上涨... 8月2日,小米商城价格更新,多款主力机型上调300元至500元不等。其中,REDMI Turbo 5...
“真实社交”售卖“优先访问权”... 据了解,购买这项服务的企业能够直接接入“真实社交”数据源,辅以人工智能技术解读信息,进而提前把握市场...
原创 狂... 英伟达正式官宣与 Safe Superintelligence(SSI)达成长期战略合作,总投资规模...
马斯克最新预言来了,钱要没用了... “以法莲是商人,手里有诡诈的天平,爱行欺骗!”——圣经 据红星新闻报道,本月,埃隆·马斯克在接受专...
遭欧盟22国“围攻”,西班牙怒... 西班牙首相桑切斯与22个欧盟成员国领导人激烈交锋后,由轮值主席国爱尔兰正式召集,欧盟将于本周二(8月...
创维电视42d9指示灯不亮 1、可能为电源插座插头的故障。2、可能为电源连接线的故障。3、可能为电视内部开关电源电路出了故障。4...