现有一张表A(
subject_id varchar(20),
student_code varchar(20),
sd date 

注意subject_id不是主键,同一个subject_id 可能对应多个student_code表B (
student_code varcahr(20),
student_name varcahr(50)
)
另有一张临时表T (
subject_id varchar(20),
student_code varchar(20),
student_name varcahr(50)
sd date 
)现在我想把当天发生的数据写入T,语句类似这样
insert into T(subject_id,student_code,student_name,sd)
select A.subject_id,B.student_code,B.student_name,A.sd 
from A join B 
on (A.student_code=B.student_code) 
where A.sd='20110705'写完后T中大概有几十万条数据。上述语句执行的效率还可以,因为我在A的student_code和sd上分别建立的索引,在B的student_code上也建立了索引。做完这个操作后,我还需要把与T有关联关系的数据也写入T中,我的语句是这样的
insert into T(subject_id,student_code,student_name,sd)
select A.subject_id,B.student_code,B.student_name,A.sd 
from A join B 
on (A.student_code=B.student_code) 
where A.sd<'20110705' and exists (select 1 from T where A.subject_id=T.subject_id)这样的写法语义上应该没什么问题,但是因为A表是一个非常大的表,可能会有上亿条的数据,B表数据量也很大,也有上千万条的数据,如果按上述的方法执行会很慢,不知道各位大侠有没有什么办法优化查询呢?谢谢了。