这个sql怎么优化,查询每月最早的一条记录,数据量有点大
batch_no serial trans_date pay_acct_no trans_amt .........(省略)666 789 2015-06-07 123456 6.55
666 788 2015-06-07 123456 67.5
777 123 2015-06-10 123456 63.55
数据大概是这样的,要取出每个月同一个账号最早的那一条记录,同一天batch_no serial 用这两个字段判断。
本菜鸡写的sql是这样的。。。。
select c.trans_date, c.trans_amt
from xxxxxx c
where c.app_code = '123'
and c.trans_date < date'2015-07-01'
and c.trans_date > date'2015-06-01'
and ((c.response = '0003' and c.respdesc like'%欠费%')
or (c.response = '0021' and c.respdesc like'%透支%'))
and not exists (select 1 from xxxxxx
where pay_acct_no = c.pay_acct_no
and (trans_date < c.trans_date
or (batch_no < c.batch_no
or(batch_no = c.batch_no and serial < c.serial))));
跑了好久才出来100条数据,求大牛指点指点。。。。