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

不过修改表结构需谨慎。

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...