数据
聚焦技术和人文,分享干货,共同成长。
聚焦技术和人文,分享干货,共同成长。
users
表,包含id
、username
、email
、age
、status
、created_at
等字段,数据量达百万级。执行select * from users where age = 25 and status = 1
查询速度很慢。age
和status
字段上建立索引,导致全表扫描。create index idx_age_status on users(age, status)
。orders
表,结构包括id
、order_no
、user_id
、status
、create_time
等字段,每天有数百万数据写入,按user_id
查询最近的订单非常慢。explain select * from orders where user_id = 10086 order by create_time desc limit 10
发现是全表扫描,仅对user_id
建立单列索引效果不佳。create index idx_user_time on orders(user_id, create_time)
,再使用覆盖索引只查询需要的字段,如select order_no, status, create_time from orders where user_id = 10086 order by create_time desc limit 10
。select sql_no_cache count(*) from orders where user_id = 10086
执行时间 2.35 秒,优化后仅 0.003 秒,速度提升近 1000 倍。article
表,包含id
、author_id
、category_id
、views
、comments
、title
、content
等字段。需要查询category_id
为 1 且comments > 1
的情况下,观看数量最多的文章。explain select id,author_id from article where category_id = 1 and comments > 1 order by views desc limit 1
,显示type
为all
全表扫描,Using filesort
文件内排序。ALTER TABLE article ADD INDEX idx_article_ccv (category_id, comments, views)
,发现文件排序依然存在,删除索引后重建CREATE INDEX idx_article_ccv ON article (category_id,views)
。employees
表,包含id
、name
、age
、position
、hire_time
等字段,有组合索引idx_name_age_position
。执行EXPLAIN SELECT * FROM employees WHERE name > "LiLei" AND age = 23 AND position = "dev"
和EXPLAIN SELECT * FROM employees WHERE name < "LiLei" AND age = 23 AND position = "dev"
,发现第二个语句未使用索引查找。EXPLAIN SELECT name,age,position FROM employees WHERE name < 'LiLei' AND age = 22 AND position ='manager'
,只查询索引值,直接从索引树中获取数据,不需要回表。4180版本的是 8079 0500 0f94 c2
改为:c641 0501 b200 90
服务器的硬件时间比系统时间快100年,使用hwclock -w这个命令无法同步,大佬有别的可建议的命令吗
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本