日期:2014-05-18  浏览次数:20477 次

求教:查询时间慢的问题!
需求:把B表的数据插入A中,AB表结构一样。
为防止B重复插入A,我以下面的SQL作判断条件:
select   *   from   B   where   id     in
(
select   a1.id   from   A   a1,B   b1
where   a1.name+   a1.time=   b1.name+   b1.time   //注:以名字和时间来确定唯一
)

问题是当然表中有30000行数据时,执行很慢,加了索引也20分钟。
本人接触数据,请指点,是不是我的思路有问题?

------解决方案--------------------
if exists(SELECT b.* FROM B as b1
LEFT JOIN A AS a1 on a1.name+ a1.time= b1.name+ b1.time
WHERE a1.id IS NULL)
------解决方案--------------------
If Exists(Select B.id From B Left Join A On A.name = B.name And A.[time] = B.[time] Where A.id Is Null)

------解决方案--------------------
if exists
(
select 1 from A a1
inner join B b1
on a1.[name]=b1.[name] and a1.[time]=b1.[time]
)
------解决方案--------------------
楼主你Left join A as a1 ...加上一个and where b1.name is null ---结果肯定一行数据都没有,因为这样只存在a1.name is null的值
除非你是Right join A a1...加上一个and where b1.name is null 如果存在数据那么象
TO:hellowork(一两清风) 老大所说的 A表中存在B表中没有的数据