谁能帮我优化一下我这个sql查询语句阿。
select code ,dele='删除',name,yznum,ddnum,unit,guige from tb_farmer_pru where tjid=1
union select code=0,dele='无',name,yznum=0,ddnum=0,unit,id as guige from tb_union_guige where id not in (select code from tb_farmer_pru where tjid=1)
其实tjid=变量 ,不是一定的,我暂时这么写
由于tb_farmer_pru 表的数据量想当多。所以觉得性能可能有问题。
我总觉得应该可以优化一下,但是不知道如何优化。
问题点数:0、回复次数:4Top
1 楼hdhai9451(☆新人类☆)回复于 2004-12-01 13:55:31 得分 0
select a.code ,dele=case when tjid=1 then '删除' else '无' end ,a.name,a.yznum,a.ddnum,a.unit,a.guige
from tb_farmer_pru a
left join tb_union_guige b on a.guige=b.id
Top
2 楼solidpanther(╃╄╃我爱机器猫╄╃╄)回复于 2004-12-01 13:55:32 得分 0
select code ,dele='删除',name,yznum,ddnum,unit,guige from tb_farmer_pru where tjid=1
union
select code=0,dele='无',name,yznum=0,ddnum=0,unit,id as guige from tb_union_guige
where not exists (select 1 from tb_farmer_pru where id=code tjid=1)Top
3 楼lh1979(rocket)回复于 2004-12-01 15:11:07 得分 0
呵呵,刚才在你的那个帖子上说过了:》Top
4 楼chenyuandxm(一剑平江湖)回复于 2004-12-01 15:36:20 得分 0
既然tjid是变量,就写一个存储过程好了,具体的优化好象也没什么,也许我不知道(^-^):
create procedure pro_sel @tjid int
as
select code ,dele='删除',name,yznum,ddnum,unit,guige from tb_farmer_pru where tjid=@tjid
union
select code=0,dele='无',name,yznum=0,ddnum=0,unit,id as guige from tb_union_guige where id not in (select code from tb_farmer_pru where tjid=@tjid)
go
Top




