有2个表A,B,两个表都有30000条记录,根据户号比较,如果相同户号的就用B表的数据update A表的数据,如果B的户号在A没有则insert A表
语句如下
insert into A(x1,x2,x3) select x1,x2,x3 from B where A.x1 not in (select x1 from B)
update A set a.x2=b.x2,a.x3=b.x3 from a,b where a.x1=b.x1
在查询分析器中能够执行,第一条要7分,第而条要23分
在程序中执行就说超时 TimeOut expried 有什么好办法能解决啊

解决方案 »

  1.   

    是不是我的Cnn.CommandTimeOUt的时间设置的短所以会出现此问题
      

  2.   


    1. 两表的x1字段要建索引,加快速度。2. 连接的connectiontimeout和commandtimeout属性都设为0。3. 语句执行的顺序不要随便,先update,再insert才正确。4. 语句优化:   update A set x2=b.x2,x3=b.x3 from a join b on a.x1=b.x1   insert into A (x1,x2,x3) 
       select x1,x2,x3 
       from B left join A on a.x1=b.x1 and a.x1 is null
      

  3.   

    针对的是a表的插入与更新,故你可以在b表的x1字段建立索引
    另外插入后更新的话由于记录增多会增加筛选时间,故先更新在插入会合适些
    如果是vb中运行,建议你改成存储过程调用,另外为了不超时,可以将超时设定为0(永不超时)update A set x2=b.x2,x3=b.x3 from a,b where a.x1=b.x1insert into A (x1,x2,x3) 
    select x1,x2,x3 
    from b
    where not exists(select 1 from a where a.x1=b.x1)或 insert into A(x1,x2,x3) select x1,x2,x3 from B where A.x1 not in (select distinct x1 from B)
      

  4.   

    怎样将这两个Sql写成一个存储工程呢,怎样去调用!呢谢谢