sqlserver 优化
2017-10-16 17:17
141 查看
避免在索引列上使用计算
where子句中,如果索引列是函数的一部分,优化器将不使用索引而使用全表扫描。例如:
(低效)select ... from [dept] where [sal]*12>25000;
(高效)select ... from [dept] where [sal]>25000/12;
--------------
不同类型的索引效能是不一样的,应尽可能先使用效能高的
数字类型的索引查找效率高于字符串类型,定长字符串char、nchar的索引效率高于变长字符串varchar、nvarchar的索引。
(低效)select ... from tableName where username='张三' and age>=21
(高效)select ... from tableName where age>=21 and username='张三'
------------------------------------
使用exists或not exists代替in或not in
(高效)select * from [emp] where [empno]>0 and exists (select 'X' from [dept] where [dept].[deptno]=[emp].[deptno] and [loc]='MELB');
(低效)select * from [emp] where [empno]>0 and [deptno] in (select [deptno] from [dept] where [loc]='MELB');
----------------------------------------------------
is null或is not null操作
判断字段是否为空一般是不会应用索引的,因为索引不索引空值。不能用null作索引,任何包含null值的列都将不会被包含在索引中。也就是说如果某列存在空值,即使对该列建索引也不会提高性能。任何在where子句中使用
is null或is not null的语句优化器都不允许使用索引。
推荐方案:用其他相同功能的操作运算代替,如:a is not null改为a>0或a>''等。
---------------------------------------------------------------------------
<及>操作
大于或小于一般情况不用调整,因为它有索引就会采用索引查找,但有的情况下可以对它进行优化。如一个表有100万记录,那么执行>2与>=3的效果就有很大区别了。
(低效)select * from [emp] where [deptno]>2;
(高效)select * from [emp] where [deptno]>=3;
----------------------------------------------------------
where子句中,如果索引列是函数的一部分,优化器将不使用索引而使用全表扫描。例如:
(低效)select ... from [dept] where [sal]*12>25000;
(高效)select ... from [dept] where [sal]>25000/12;
--------------
不同类型的索引效能是不一样的,应尽可能先使用效能高的
数字类型的索引查找效率高于字符串类型,定长字符串char、nchar的索引效率高于变长字符串varchar、nvarchar的索引。
(低效)select ... from tableName where username='张三' and age>=21
(高效)select ... from tableName where age>=21 and username='张三'
------------------------------------
使用exists或not exists代替in或not in
(高效)select * from [emp] where [empno]>0 and exists (select 'X' from [dept] where [dept].[deptno]=[emp].[deptno] and [loc]='MELB');
(低效)select * from [emp] where [empno]>0 and [deptno] in (select [deptno] from [dept] where [loc]='MELB');
----------------------------------------------------
is null或is not null操作
判断字段是否为空一般是不会应用索引的,因为索引不索引空值。不能用null作索引,任何包含null值的列都将不会被包含在索引中。也就是说如果某列存在空值,即使对该列建索引也不会提高性能。任何在where子句中使用
is null或is not null的语句优化器都不允许使用索引。
推荐方案:用其他相同功能的操作运算代替,如:a is not null改为a>0或a>''等。
---------------------------------------------------------------------------
<及>操作
大于或小于一般情况不用调整,因为它有索引就会采用索引查找,但有的情况下可以对它进行优化。如一个表有100万记录,那么执行>2与>=3的效果就有很大区别了。
(低效)select * from [emp] where [deptno]>2;
(高效)select * from [emp] where [deptno]>=3;
----------------------------------------------------------
相关文章推荐
- SQLServer性能优化
- SQLServer大数据优化方法若干
- 转载别人写的比较好的一篇优化sqlserver性能的文章
- SQLSERVER完全优化教程
- SqlServer性能优化 性能调控(十)
- Sqlserver 优化的方法
- SQLSERVER语句优化及性能调试 第一篇 效率至上
- sqlserver性能优化之索引的使用和优化
- SqlServer性能检测和优化工具使用详细
- SQLSERVER海量数据库的查询优化及分页算法
- SqlServer性能优化——Compression
- sqlserver的like '%xxx%'优化,全文索引
- sqlserver 大数据量的insert、delete操作优化
- SQLServer性能优化 .net开发菜鸟总结
- SQLSERVER 2012之AlwaysOn -- 同步模式下的网卡性能优化
- SQLServer 优化SQL语句:in 和not in的替代方案
- 性能优化——统计信息——SQLServer自动更新和自动创建统计信息选项
- 第十七章——配置SQLServer(4)——优化SQLServer实例的配置
- SqlServer索引及优化详解
- SqlServer性能检测和优化工具使用详细