()where 中很多or ,會導致性能非常差,
這樣的語句,程序會按blh,cardno,patid,tjbh,一遍遍找數據。應當改為if else
SELECT a.*
FROM TJ_TJRYK a (nolock), SF_BRXXK b (nolock), YY_DQDMK c ,TJ_TJTDK d
where a.patid = b.patid and b.qxdm *= c.id and a.tdxh=d.xh
and (( @codetype =1 and b.blh = @code)
or (@codetype =2 and b.cardno = @code)
or (@codetype in (3,4) and b.patid = @code1)
or (@codetype =6 and a.tjbh = @code)) --add 20070611
order by a.xh desc
(2)subtring和盡量減少sql語句
--exec usp_mz_ys_gdtxs "00","20080903"--【Ztsql】
CREATE proc usp_mz_ys_gdtxs
@ysdm ut_czyh,
@cxrq ut_rq16
as
/**********
[版本號]4.0.0.0.0
[創建時間]2006.9.25
[作者] ziyun
[版權] Copyright ? 2004-2004上海金仕達-衛寧醫療信息技術有限公司
[描述]門診醫生站主界面醫生信息顯示內容
[功能說明]
門診醫生站主界面醫生信息顯示內容
[參數說明]
@Ysdm ut_czyh, --醫生代碼
@cxrq ut_rq16, --查詢日期
@beginrq ut_rq16, --開始時間
@endrq ut_rq16 --結束時間
[返回值]
[結果集、排序]
[調用的sp]
[調用實例]
[修改歷史]
**********/
declare @hour varchar(2),@ghlb ut_bz,@ksdm ut_ksdm
select @hour=convert(varchar(2),getdate(),108)
if @hour<='12'
begin
select @ghlb=ghlb,@ksdm=ksdm from GH_GHZDK a(nolock)
where a.ysdm=@ysdm and a.jlzt=0 and a.fzbz>0 and a.ghrq like substring(@cxrq, 1, 8)+ '%'
and substring(a.ghrq,9,2)<='12'
--agg: (1)substring(a.ghrq,9,2)<='12'的性能是非常差的,在這里a.ghrq like substring(@cxrq, 1, 8)+ '%'
-- and substring(a.ghrq,9,2)<='12' 可以改為a.ghrq>= substring(@cxrq, 1, 8) and a.ghrq<
-- =substring(@cxrq, 1, 8)+’12’and a.ghrq>= substring(@cxrq, 1, 8) and a.ghrq>
-- =substring(@cxrq, 1, 8)+’24’,這樣才可以很好的用到ghrq索引
-- (2)這里如果會取出多條記錄。這樣@ghlb不是會多賦值, 是不是可以加top 1。
end
else
begin
select @ghlb=ghlb,@ksdm=ksdm from GH_GHZDK a(nolock)
where a.ysdm=@ysdm and a.jlzt=0 and a.fzbz>0 and a.ghrq like substring(@cxrq, 1, 8)+ '%'
and substring(a.ghrq,9,2)>'12'
end
--agg: substring(a.ghrq,9,2)<='12'的性能是非常差的,在這里a.ghrq like substring(@cxrq, 1, 8)+ '%'
-- and substring(a.ghrq,9,2)<='12' 可以改為a.ghrq>
-- substring(@cxrq, 1, 8)+’12’,這樣才可以很好的用到ghrq索引
-- (2)這里如果會取出多條記錄。這樣@ghlb不是會多賦值, 是不是可以加top 1。
declare @strsql varchar(1000),
@zrc int,@zje ut_money,@wjz int
|
|