描述如下:
1)有如下二個TABLE:T1,T2,結構相同,下如所示
A varchar(100) not null
B varchar(100) not null
C varchar(100) not null
D varchar(100) not null
E float
F float
注:A+B+C+D構成KEY值(主鍵,並加索引)2)運算為:
a)T1與T2與相比較,KEY值(主鍵)一樣,當字段E與字段F有一不一樣時為所求記錄。
b)T1與T2與相比較,對比KEY值(主鍵)當對方沒有此KEY值時,就為所求。3)例:
T1 Table 數據
a1,b1,c1,d1,1,3
a2,b2,c2,d2,1,2T2 Table 數據
a1,b1,c1,d1,1,2
a3,b3,c3,d3,1,2運算3)a時,得出
a1,b1,c1,d1運算3)b時,得出
a2,b2,c2,d2
a3,b3,c3,d34)數據量:
T1與T2的資料量各為2000萬條記錄的數據量。5)在求2)的運算時,我使用的方法為:
a)
select T1.A,T1.B,T1.C,T1.D, from T1 
inner join  T2
T1.A=T2.A
and T1.A=T2.A
and T1.B=T2.BA
and T1.C=T2.C
and T1.D=T2.D
and (T1.E<>T2.E or T1.F<>T2.F)b)
select T1.A,T1.B,T1.C,T1.D, from  T1 
left join  T2
T1.A=T2.A
and T1.A=T2.A
and T1.B=T2.B
and T1.C=T2.C
and T1.D=T2.D
where 
T2.A is null
union 
select T2.A,T2.BT2.C,T2.D, from  from T1 
Right join  T2
T1.A=T2.A
and T1.A=T2.A
and T1.B=T2.B
and T1.C=T2.C
and T1.D=T2.D
where 
T1.A is null在運行了以上SQL計算時非常慢。第一個SQL將近5分鐘。第二個SQL近15分鐘,而且數據庫運行時內存占用1.5G左右。請問各路高手,這情況可以優化嗎?希望總時間不要超過3分鐘。如果可以怎麼優化?

解决方案 »

  1.   

    可能是服務器還算湊合吧4G的內存,二個3.2G的CUP。4個SISC硬盤,共240G。但這程序運行起來時,服務器響應也很變得很慢。
      

  2.   

    select T1.A,T1.B,T1.C,T1.D, from T1 
    inner join  T2
    on T1.A=T2.A
    and T1.B=T2.B
    and T1.C=T2.C
    and T1.D=T2.D
    where (T1.E>T2.E and t1.e<t2.e)or (T1.F<T2.F and t1.f>t2.f)建立索引e+f+a+b+c+dselect * from t1 where
    not exists(select a from t2 where t2.a=t1.a and and T1.B=T2.B
    and T1.C=T2.C and T1.D=T2.D)
    union 
    select * from t2 where
    not exists(select a from t1 where t2.a=t1.a and and T1.B=T2.B
    and T1.C=T2.C and T1.D=T2.D)这样应该会快一些了
      

  3.   

    UP 速度越來越慢了。現查詢一次需要30分鐘了。簡直無法忍受。DX過來幫幫忙啊。怎麼樣做可以,只要速度能提高,數據能查到就可以了。
      

  4.   

    http://www.microsoft.com/china/MSDN/library/data/sqlserver/FiveWaystoRevupYourSQLPerformanCE.mspx?mfr=true
      

  5.   

    A varchar(100) not null
    B varchar(100) not null
    C varchar(100) not null
    D varchar(100) not null用VARCHAR类型做主键真是会想的出来,这会有效率最低的比较效率
    用DECIMAL类型会好很多