sql优化

1.检查优化效果

查看执行时间和cpu占用时间

set statistics time on
select * from dbo.Product
set statistics time off

查看I/0的操作情况

set statistics io on
select * from dbo.Product
set statistics io off

 

2.优化查询sql

2.1.细化查询字段

尽量不查询多余的列与行,不使用select *

2.2where 子句

尽量避免在 where 子句中使用!=,<>,in,not in,or,like操作符

很多时候用 exists 代替 in 是一个好的选择

尽量避免在 where 子句中对字段进行表达式操作(进行 null 值判断),函数操作

可以使用默认值代替null

引擎放弃使用索引而进行全表扫描。

 

3.数值信息的字段尽量不要设计为字符型

4.尽可能的使用 varchar/nvarchar 代替 char/nchar

首先变长字段存储空间小,可以节省存储空间,其次对于查询来说,在一个相对较小的字段内搜索效率显然要高些。

5.尽可能的使用 count(*),不要具体到某个字段

count(column) 是表示结果集中有多少个column字段不为空的记录

count(*) 是表示整个结果集有多少条记录

6.union all比union更高效

7.索引,分库

posted @ 2017-05-18 17:17  corolcorona  阅读(102)  评论(0编辑  收藏  举报