if (select count(*) from table_name where 条件) 与 if exists(select * from table_name where 条件) 哪个效率更高?

解决方案 »

  1.   

    应该是 if (select count(*) from table_name where 条件)>0 与 if exists(select * from table_name where 条件) 比较吧?if exists(select * from table_name where 条件)效率明显高。
      

  2.   

    个人认为,后者快一些....因为前者在where过滤条件之后还要进行count(*)操作...,这是一个再限定条件
      

  3.   

    if exists(select 1 from table_name where 条件) 
    这个比你说的那两个更高~
      

  4.   

    itblog(BegCSharp) ( ) 信誉:100  2006-06-13 15:44:00  得分: 0  
     
     
       if exists(select 1 from table_name where 条件) 
    这个比你说的那两个更高~
      
     
    ------------------------------------------------------------
     if exists(select * from table_name where 条件) 
    与  if exists(select 1 from table_name where 条件) 的执行计划是一样的,
    效率应该是一致的。
      

  5.   

    if exists(select * from table_name where 条件) 
    与  if exists(select 1 from table_name where 条件) 的执行计划是一样的,
    效率应该是一致的。------------------------------------
    恐怕你的列数比较少
      

  6.   

    if exists(select * from table_name where 条件) 
    最快
      

  7.   

    if exists(select * from table_name where 条件) 比
    if exists(select 1 from table_name where 条件) 
    if exists(select 某字段 from table_name where 条件) 
    if (select count(*) from table_name where 条件)>0
    都快COUNT不解释了
    EXiSTS可以从关系运算的角度给出解释这是我以前测试过的测试数据100000条,测试循环次数100~10000(大数定理,hoho)
    当然也测试了有无索引的情况
      

  8.   

    exists 的确快多,我昨天刚用过:)深有体会
      

  9.   

    if exists(select * from table_name where 条件) 
    与  if exists(select 1 from table_name where 条件) 的执行计划是一样的,
    效率应该是一致的。------------------------------------
    恐怕你的列数比较少---------------------------------
    exists并不需要将列返回,*,列名,1作用几乎相当EXISTS
    指定一个子查询,检测行的存在。 语法
    EXISTS subquery参数
    subquery是一个受限的 SELECT 语句 (不允许有 COMPUTE 子句和 INTO 关键字)。有关更多信息,请参见 SELECT 中有关子查询的讨论。 结果类型
    Boolean结果值
    如果子查询包含行,则返回 TRUE。
      

  10.   

    select * 和select 主健的效率应该是一样的
      

  11.   

    if exists(select * from table_name where 条件)效率明显高。
    因为后者没有使用count函数