表 mnet_message_send 里放的是所有发送短信记录,group_id 表示某个批次,这个批次里有很多记录 ,当然这个批次里的记录可能是不同的, 但我要找出发送失败的所有批次,我想随便从某个批次里找一条就可以了 于是 达到了去掉重复行的目的 这样写SQL:select t.group_id,t.corp_code,t.corp_name,t.corpuser_code,t.corpuser_name
from mnet_message_send t ,(select s.group_id as group_id,max(s.rowid) as cn from mnet_message_send s group by s.group_id) c
where t.status = '01'
and c.cn = t.rowid and c.group_id = t.group_id 写好发现目的是达到了,但是效率很低,而且只有在数据量小的时候可以,这张表里的数据才 2W 条 就花了 5 秒钟,再多一些就把临时表空间撑死了。想请教下高手如何优化该SQL..
from mnet_message_send t ,(select s.group_id as group_id,max(s.rowid) as cn from mnet_message_send s group by s.group_id) c
where t.status = '01'
and c.cn = t.rowid and c.group_id = t.group_id 写好发现目的是达到了,但是效率很低,而且只有在数据量小的时候可以,这张表里的数据才 2W 条 就花了 5 秒钟,再多一些就把临时表空间撑死了。想请教下高手如何优化该SQL..
select s.group_id as group_id,max(s.rowid) as cn from mnet_message_send s group by s.group_id肯定是这个慢了。
再检查一下索引有没有做好。
select group_id,corp_code,corp_name,corpuser_code,corpuser_name from(
select s.*,row_number()over(partition by s.group_id order by s.corp_code) rn from mnet_message_send s where s.status='01')
where rn=1;
如果相同的话,更简单了
select distinct group_id,corp_code,corp_name,corpuser_code,corpuser_name from mnet_message_send
where status='01'
就搞定了
select t.group_id,t.corp_code,t.corp_name,t.corpuser_code,t.corpuser_name
from
mnet_message_send t,
(select s.group_id as group_id,max(s.rowid) as cn from mnet_message_send s group by s.group_id) c
where
c.cn = t.rowid and c.group_id = t.group_id and t.status = '01'先把01的状态先踢出来!然后在连接.ORACLE执行的条件的顺序是自后向前的!
wildwave 第一种方法可以达到我的目的,效率很好,谢谢, 结贴。