优化一个mysql语句
发表于:2025-11-07 作者:千家信息网编辑
千家信息网最后更新 2025年11月07日,优化了一个mysql的语句,没有什么技术含量,说一下过程:优化前语句如下,大约执行时间为1-2秒select u.id ,u.s_user_name,u.s_user_img,(select cou
千家信息网最后更新 2025年11月07日优化一个mysql语句
优化了一个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: 0mysql> 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)
不过修改表结构需谨慎。
助手
语句
结构
再次
含量
字段
技术
时间
索引
过程
查询
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
sql数据库建表教程
超融合技术共享数据库
迪蒙网络安全黑板报
常州服务器托管
超融合云平台服务器价格
奉贤区数据软件开发答疑解惑
网络安全官 抖音
软件开发师的发展趋势
百度搜索数据库删除数据
服务器启动安全模式
服务器中扫号病毒
计算机网络技术是什么的含义
软件开发工程师电脑包
在更新数据库密码前一定要先
我市网络安全宣传片
闲来麻将软件开发公司
网络安全作文电子版
数据库著作
关于网络安全漏洞的提示
研究网络安全法内容
网络安全的稿子100字
衡阳正规软件开发
网络服务器日常安全与维护
安装数据库的流程图
读秀数据库的技术
数据库的维护和简单操作
毕节网络安全系统报价
江苏惠普服务器维修价格
逸动xt车机无法连接服务器
搜索软件网络安全