怎么优化这条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 表的数据量想当多。所以觉得性能可能有问题。
我总觉得应该可以优化一下,但是不知道如何优化。
问题点数:50、回复次数:6Top
1 楼xingfuniao(幸福鸟)回复于 2004-12-01 13:58:34 得分 20
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 * from tb_farmer_pru where tb_union_guige.id=code and tjid=1)Top
2 楼lsxaa(小李铅笔刀)回复于 2004-12-01 13:59:22 得分 10
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)
改成这样试一下
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 tjid=1 and code=id)Top
3 楼solidpanther(╃╄╃我爱机器猫╄╃╄)回复于 2004-12-01 13:59:44 得分 0
两个帖子?
Top
4 楼tensyy80(程序员玲玲)回复于 2004-12-01 14:44:48 得分 0
我是怕没有看才发两个帖子Top
5 楼lh1979(rocket)回复于 2004-12-01 15:10:04 得分 0
lsxaa(小李铅笔刀) 改的比较好,
如果用in的话系统需要将结果集放在tempdb,增加了系统I/O操作
而EXISTS没有结果集Top
6 楼xinliangyu(yxl)回复于 2004-12-01 16:32:58 得分 20
用In子句作筛选时,如果In子句的返回值较多,则速度不及同样功能的exists子句。建议尽可能使用exists来筛选.Top




