一文解决MySQL大部分问题

下面这两个方面能够解决MySQL80%的问题


SQL级别

1、明确select列表的列

select * from t;

优化:明确你想要查询的列

select id,name from t;

2、空间换时间:建立索引,走索引,避免全表扫描

select id ,name from t where name is null -- 空值不能利用索引,所以还是全表扫描
select id ,name from t where id in(1,2,3) -- 全表扫描
select id,name from t where id=1 or id=2 or id=3 -- 全表扫描

改造

select id,name from t where id=1 -- 走索引
union all
select id,name from t where id=2 -- 走索引
union all
select id,name from t where id=3 -- 走索引

3、如果是分区表,在where条件中明确使用哪一个分区

select id,name from t where dt='分区值'

4、尽量用union all代替union

5、如果排序字段没有用到索引,就尽量少排序

6、exists代替in


架构级别

  • 分区
  • 分表
  • 分库

分而治之思想

待续......

发布了18 篇原创文章 · 获赞 19 · 访问量 5308

猜你喜欢

转载自blog.csdn.net/weixin_44586883/article/details/105652971