有A和B两个数据表,数据量比较大两个表都有一个“tid”字段(非主键字段)如何才能快速检测两表是否存在这个tid字段的交集不需要得到交集的具体记录,只需要知道有没有交集,要效率高的,因为数据量大,轮询的话就暴死了

解决方案 »

  1.   

    SELECT * FROM A WHERE EXISTS(SELECT 1 FROM B WHERE A.TID=TID)
      

  2.   

    [code=SQ]在 两个表的 tid 都建立索引例:
       create index ta_index on A(tid)
       create index tb_index on B(tid)   select count(*) from A ,B where a.tid = b.tid[/code]
      

  3.   

    select top 1 * from a , b where a.tid = b.tidselect top 1 a.* from a where exists(select 1 from b where b.tid = a.tid)
      

  4.   


    SELECT 1 FROM A WHERE EXISTS(SELECT 1 FROM B WHERE A.TID=TID)
      

  5.   

    [code=SQ]
    SELECT COUNT(*) FROM A WHERE EXISTS(SELECT 1 FROM B WHERE A.TID=TID)
    [/code]
      

  6.   

    [code=SQ]select * from a where exists(select 1 from b where b.tid = a.tid)[/code]
      

  7.   

    [code=SQ]select * from a 
    where exists(select 1 from b where b.tid = a.tid)[/code]
      

  8.   

    不好意思,事实上需要可能要更复杂点,用上边的方法不知道怎么写事实是两个表并不一定是一个单独的表,可能是通过inner join 连接起来的,就是说我的具体需求是:select tid from A inner join C on A.field1=C.fild2 where 筛选条件select tid from B inner join D on B.field1=D.fild2 where 筛选条件然后这两个查询得到的结果可能有重复的,所以我想判断有没有重复的,但用exists或者是in的查询速度过慢(索引并不起作用),所以希望有一个较为快速的方法
      

  9.   

    描述有误,不是有没有重复的,是两个查询的结果可能都存在某个tid值的情况比如查询1里有一个tid=101,而查询2中也可能有tid=101,如果两个都有这个101,我想让程序能知道
      

  10.   

    [code=SQ]select count(*)
    from
      (select a.tid from A inner join C on A.field1=C.fild2 where 筛选条件)t
      left join
      (select b.tid from B inner join D on B.field1=D.fild2 where 筛选条件)e
      on t.tid = e.tid
    where e.tie is not null[/code]
      

  11.   

    select count(*) as rn,t.tid as tid 
    from 
      (select a.tid from A inner join C on A.field1=C.fild2 where 筛选条件)t 
      left join 
      (select b.tid from B inner join D on B.field1=D.fild2 where 筛选条件)e 
      on t.tid = e.tid 
    where e.tie is not null rn是看有没重复的,如果有 tid 就是重复的值。
      

  12.   

    select count(*) as rn,t.tid as tid  
    from  
      (select a.tid from A inner join C on A.field1=C.fild2 where 筛选条件)t,
      (select b.tid from B inner join D on B.field1=D.fild2 where 筛选条件)e   
    where t.tid = e.tid