首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

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

2012-01-12 
这句SQL语句还能优化吗? select*fromvirtel_telrecordtwheret.snin(selectb.snfromvirtel_telrecordb,virt

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

热点排行