1,790   MySQL

一,尽量采用索引可以有效提高查询速度,以下为一些使用建议

 

1,SQL少用like匹配:

like ‘%test%’ 是不会使用到索引的,而 like ‘test%’ 才会用到索引;

 

2,不要在字段上进行截取或者运算

/* 这会使索引失效 */
select * from post where DATE_FORMAT(time,'%Y%m%d') < 20150225

/* 改成这样,效率会高很多 */
select * from post where time < '2015-02-25 00:00:00'

 

3,只用一个索引
MySQL查询只使用一个索引,比如

/* where 中的 age 已经使用了索引,后面的order by time是不会使用索引的 */
select * from user where age = '25' order by time desc; 

4,索引列值中不要包含null,因为这样也会是索引失效

 

二,索引缺点:

1,建立过多的索引会导致性能下降,因为每次操作表格,同时也要更新索引

2,大数据表格索引文件会非常大,会占据大量磁盘空间




Leave a Reply

Your email address will not be published. Required fields are marked *