日期:2014-05-18  浏览次数:20517 次

这句SQL语句还能优化吗?

select   *     from   virtel_telrecord   t
where   t.sn     in
(select   b.sn   from   virtel_telrecord   b   ,virtel_telrecord   c
where   b.source   =   c.source   and   b.dest   =   c.dest    
and   b.sn   !=c.sn   and   b.status   =   'Z '
and     c.end_time   =   b.start_time)

帮帮忙

------解决方案--------------------
select b.* from virtel_telrecord b left join virtel_telrecord c
on b.source = c.source and b.dest = c.dest
where b.sn !=c.sn and b.status = 'Z '
and c.end_time = b.start_time
------解决方案--------------------
select b.* from virtel_telrecord b , virtel_telrecord c
where b.source = c.source and
b.dest = c.dest and
b.start_time = c.end_time and
b.status = 'Z ' and b.sn != c.sn


--不过最后一个b.sn != c.sn,这个条件是否有点奇怪.
------解决方案--------------------
select * --尽量别写*,写全列可以用到索引,而*用不到索引
from virtel_telrecord t
where exists(select * --exists 快过 in
from virtel_telrecord b ,virtel_telrecord c
where b.source = c.source and b.dest = c.dest
and b.sn !=c.sn and b.status = 'Z '
and t.sn = b.sn)
------解决方案--------------------
直接连接就行

select b.* from virtel_telrecord b ,virtel_telrecord c
where b.source = c.source and b.dest = c.dest
and b.sn !=c.sn and b.status = 'Z '
and c.end_time = b.start_time