CleverCode在實際的工作也寫過一些低效率的sql語句。這些語句會給數據庫帶來很大的壓力,最主要的表現就是sql語句運行慢,後來逐漸的去優化和嘗試。總結了一些高質量的sql語句的寫法。這裡CleverCode總結一下分享給大家。
select * from system_user where date(createtime) >= '2015-06-01'優化後:
select * from system_user where createtime >= '2015-06-01'
select * from system_user where age + 10 >= 20優化後:
select * from system_user where age >= 10
select * from table_a a left join table_b b on a.id = b.id left join table_c c on a.id = c.id where a.id > 100 and b.id < 200
select * from ( select * from table_a where id > 100 ) a left join( select * from table_b where id < 200 )b on a.id = b.id left join table_c on a.id = c.id
select * from system_user where age > 10優化後:
select username,email from system_user where age > 10
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')
insert into system_user(username,passwd) values('test1','123456'),('test2','123456'),('test3','123456')