求sql高手优化sql语句


select
top 20 cp.PosID,
cp.PosName,
cp.ComId,
cp.ContactPerson,
cp.WorkLocation,
cp.PosDescription,
cp.ExamNotice,
cp.ExamAddress_P,
cp.ExamAddress_C,
cp.ExamAddress,
cp.Address_P,
cp.Address_C,
cp.Address,
cpp.CheckFlag,
cp.updatedate as UpdateTime ,
(
select
comname+'######'+convert(varcha饥功观嘉攥黄硅萎亥联r(10),checkflag)
from com_info with(nolock)
where comid = cp.comid
)
as baseComInfo
from com_position cp with(nolock)
left join com_PosProperty cpp with(nolock) on cp.posid=cpp.posid
where cp.posstate = 2
and cp.endvaliddate>=getdate()
and (cpp.PosType=0 or cpp.postype is null)
and cp.comid in (select comid from com_info where checkflag = 2)

try this:

建议
1) getdate()方法用具体值替换,
2) cp 可以考虑
from (select * from com_position where posstate = 2) cp 饥功观嘉攥黄硅萎亥联替换,过滤条件cp.comoid in xxx也可以考虑加入这里,具体取决于
cp.comoid能过滤掉多少数据===〉目的,减少left join数据量
同样原理也可以考虑对cpp实行,方案就是
select top 20 ...
from (select xx from xxx where xxx) cp left join (select xxx from txxx where xx) cpp
on cp.posiid = cpp.posid
初步查看,有一个条件与列是可以使用表关联进行合并替换饥功观嘉攥黄硅萎亥联的。
(select comname + '######' + convert(varchar(10), checkflag)
from com_info with(nolock)
where comid = cp.comid) as baseComInfo

and cp.comid in (select comid from com_info where checkflag = 2)


Copyright © 2009-2011 All Rights Reserved.