读书人

这句SQL语句还能优化吗?该如何处理

发布时间: 2012-01-12 22:11:58 作者: rapoo

这句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

读书人网 >SQL Server

热点推荐