大家帮我看看,这两行代码,哪句效率高些,为什么,谢谢.
--------------------------------------------create table g1
(
f1 int,
f2 varchar(5)
)insert into g1
select 1,'zzzz'
union all select 2,''
union all select 2,null
union all select 2,'bbbb'
select *
from g1--我想查询所有不为null,而且不为空白字符串的记录
--我想问问下面两种方法,哪种效率高,为什么?--方法1:
select *
from g1
where isnull(f2,'')<>''--方法2:
select *
from g1
where f2 is not null and f2<>''
--------------------------------------------create table g1
(
f1 int,
f2 varchar(5)
)insert into g1
select 1,'zzzz'
union all select 2,''
union all select 2,null
union all select 2,'bbbb'
select *
from g1--我想查询所有不为null,而且不为空白字符串的记录
--我想问问下面两种方法,哪种效率高,为什么?--方法1:
select *
from g1
where isnull(f2,'')<>''--方法2:
select *
from g1
where f2 is not null and f2<>''
谢谢指教,可是我今天看到资料说,where语句中,对字段进行函数处理是不好的习惯,所以我才问这个问题的.
真的一样吗?
第一种进行一次函数运算,再进行一次比较运算.(ISNULL函数进行一次比较运算和一次替换操作)
第二种进行三次比较运算
操作都是在CPU内部进行的,对速度影响不会很大.
二次判断比较if f2 is not null and f2<>''
也是二次判断
where语句中,对字段进行函数处理是不好的习惯——同意因为用了函数之后,相关的索引就用不上了。
因为用了函数之后,相关的索引就用不上了。 同意
set statistics io on
看看哪个逻辑读取少