MySQL 优化
1 建议一:尽量避免在列上运算
优化前:
select * from system_user where date(createtime) >= '2015-06-01'
2 建议二:用整型设计索引
用整型设计的索引,占用的字节少,相对与字符串索引要快的多。特别是创建主键索引和唯一索引的时候。
1)设计日期时候,建议用int取代char(8)。例如整型:20150603。
2)设计IP时候可以用bigint把IP转化为长整型存储。
3 建议三:join时,使用小结果集驱动大结果集
使用join的时候,应该尽量让小结果集驱动大的结果集,把复杂的join查询拆分成多个query。因为join多个表的时候,可能会有表的锁定和阻塞。如果大结果集非常大,而且被锁了,那么这个语句会一直等待。这个也是新手常犯的一个错误!
优化前:
select *from table_a aleft join table_b b on a.id = b.idleft join table_c c on a.id = c.idwhere a.id > 100 and b.id < 200
优化后:select *from ( select * from table_a where id > 100) aleft join( select * from table_b where id < 200)b on a.id = b.idleft join table_c on a.id = c.id
4 建议四:仅列出需要查询的字段
|
5 建议五:使用批量插入节省交互
优化前:
insert into system_user(username,passwd) values('test1','123456')insert into system_user(username,passwd) values('test2','123456')insert into system_user(username,passwd) values('test3','123456')
6 建议六:多习惯使用explain分析sql语句
7 建议七:多使用profiling分析sql语句时间开销