1、查找相同C06的记录 我拿字符串NULL来代替空的。
Select DISTINCT id,C06 from hfC where C06 in (Select C06 from hfC where C06 <> 'NULL'group by C06 having count(1)>1)
记录在10K条左右速度十分慢……请问如何优化?
2、A表中的记录与B表的记录数不对 因为a07是字符串只好用like
Select Inid,A01 from hfA where A07 not like (select count(1) from hfC where Inid=hfa.Inid)
同样在10K左右的记录的时候就速度过慢我主要在access上实现…… 但是这个速度太慢了,如何优化?
a:
-----------------------
id inid A07
1  1    2
2  2    1
3  3    1b:
-----------------------
id inid C06
1  1    340103196112172529
2  1    34010319618219241X
………………谢谢大家

解决方案 »

  1.   

    1的优化:
    --建立中间表#temp存储你认可的C06的值
    Select C06 into #temp from hfC where C06 <> 'NULL'group by C06 having count(1)>1
    --和中间表关联,这样速度很快,相比于in
    Select DISTINCT id,C06 
    from hfC  a
    join #temp b on a.C06=b.C06
      

  2.   

    1、
    Select id,C06 from hfC a
    where exists (
    select 1 from hfC where c06=a.c06 and id<>a.id
    )
      

  3.   

    Select a.Inid,a.A01 from hfA a inner join (select inid,count(*) as c from hfc group by inid) as t
    on a.Inid=t.Inid
    where a.a07<>t.c
      

  4.   

    2的优化:
    select count(1) cnt into #temp1
    from hfC a
     join hfa b on a.inid=b.inidselect a.A07 into #temp2
    from hfA a 
    join #temp1 b on a.A07=b.cnt--将=的先查出来,一会匹配标上的就是not likeSelect a.Inid,a.A01 
    from hfa a
    left join #temp2 b on a.A07=b.A07
    where b.A07=null--为null的说明没有匹配上,即左表的A07不在右表中
    drop table #temp1
    drop table #temp2 
    上面的1优化也要添加
    drop table #temp语句没有测试,可能有语法错,但这样的做法在大数据量的情况下效率比用in和not like要高
      

  5.   

    to :Yang_(扬帆破浪) 
     我觉得你对1的优化语句挺巧妙的,用=判断还直接避开了null.但是你这样的处理好像效率还是有问题的.每次都要exist一下,后面的子句由于和前面的表相关,所以每次都要重新执行.我觉得效率比原来还慢.因为原来的in后面的子句是一定的,执行的时候会缓存该子句的结果,不必每次执行.你觉得呢,请指教~
      

  6.   

    to :Yang_(扬帆破浪) 
    第一句 Select id,C06 from hfC a
    where exists (
    select 1 from hfC where c06=a.c06 and id<>a.id
    )1、3条C06一样的记录,其中2条ID一样,你select出来是3条记录,按楼主意思应该只select出来2条。
    2、如果加上distinct的话,2条完全一样的记录(ID和C06都一样)你这个又SELECT不出来。我不会改了:(当然,如果ID唯一肯定没问题了。我是在测试速度的时候发现的,刚好选的测试表ID不唯一