一.分库分表
二.加索引,索引不超过6个为最好。
三.sql编写优化
1.尽量避免在 where 子句中对字段进行 null 值判断,可将字段设置默认值。
2.尽量避免在 where 子句中使用!=或<>
3.尽量避免在where 子句中使用 or 来连接条件
select id from user where age=10 or age=20
可以修改为:
select id from user where age=10 union all select id from user where age=20
4.尽量避免查询语句中使用*,可将*转换成具体字段
5.in 和 not in 也要慎用,能用 between 就不要使用in
6.尽量避免在 where 子句中对字段进行表达式操作,可将等号左边为字段,右面为运算
select id from user where num/2=100 应改为: select id from user where num=100*2
7.尽量用varchar代替char,varchar是变长字段,占用内存少内存