有两张表TAB1和TAB2,TAB1中有10,000条数据,TAB2中有1000条数据,两张表中都有id和name字段,那么编写一个SQL语句完成下面的操作性能最优:
计算出TAB1中与TAB2中id和name字段完全相同的TAB1的数据条数?

解决方案 »

  1.   

    try--
    select
      count(1)
    from 
      tab1
    where
      exists(select 1 from tab2 where id=tb1.id and name=tb1.name)
      

  2.   

    写错一个表名,修正:
    select
      count(1)
    from 
      tab1
    where
      exists(select 1 from tab2 where id=tab1.id and name=tab1.name)
      

  3.   

    select * from table1 a where exists(select 1 from table2 where a.id= id and a.name=name)select * 
    from table1 a 
    left join table2 b on a.id=b.id and a.name = b.name
    where a.id is not null
      

  4.   


    select count(*) from tb2,tb1 where tb2.id=tb1.id and tb2.name=tb2.name
      

  5.   

    select count(1) from tab1 a ,tab2 b where a.id = b.id and a.name = b.name
      

  6.   

    大家有没有考虑到TAB1和TAB2数据量的问题啊?TAB1数据量是TAB2数据量的100倍啊?
      

  7.   


    和我想的一样,据说exists可以提高效率!
      

  8.   

    这个与数据的重复度,可能的匹配次数都有关
    光给个量很难说怎么最优select count(*) 
    from table2 a 
    left join table1 b on a.id=b.id and a.name = b.name
    where b.id is not null