mssql sqlserver “select 1″和”select 0″关键字用法说明


摘要:
下文讲述”select 1″和”select 0″的应用场景,如下所示:
实验环境:sql server 2008 R2



场景分析

select 1、select 0应用场景:
1.当我们只需获取一张数据表拥有的行数时,
此时可使用(select 1和select 0)达到同样的效果select 1 from tableName,不返回任何列,可使系统降低开销,减少RID查找,提高运行效率。
2.exists中如何不需要返回列时,采用 “select 1″和”select 0″优化相应的查询
例:
select 1 优化改写

   select info,count (*) as countAll from tableName
   group by info;
 ---------优化后------------
   select info,count (1) as countAll from tableName
   group by info;

select a.* from tableName a where not exists(
select b.info from tableNameExtend b where keyId=a.keyId
);
--------优化后---------
select a.* from tableName a where not exists(
select 1 from tableNameExtend b
where keyId=a.keyId
);