CREATE view [dbo].[v_ypjg_main_wypjg] as select 'x' as pjlx,lszh,spdm ,dwdm=(case when substring(dwdm,1,3)='DYF' then '80-PP' else dwdm end) ,dwmc,mxjls,xcljls,kcsjls,zhsmsj ,zfbz=(case when minclbz<-1 then 'y' else 'n' end) ,xclbz=(case when xcljls>0 then 'y' else 'n' end) ,kcsbz=(case when smlxs=1 and kcsjls>0 and kcsjls=xcljls then 'y' else 'n' end) ,smlx=(case when smlxs=1 and maxsmlx in ('销售出库','退货入库','抽检出库','销毁出库')then maxsmlx else '待定' end) from( select lszh,spdm,dwdm,dwmc ,max(smsj) as zhsmsj ,max(isnull(smlx,'')) as maxsmlx ,min(isnull(clbz,0)) as minclbz ,count(*) as mxjls ,count(case when isnull(clbz,0) in (0,1,2) then lszh else null end) as xcljls ,count(case when isnull(clbz,0)=2 and isnull(smlx,'')in('销售出库','退货入库','抽检出库','销毁出库') then lszh else null end) as kcsjls ,count(distinct (case when isnull(clbz,0)<0 then null else isnull(smlx,'') end)) as smlxs from ypjg_xs_wypjg with(nolock) where lszh not in ( select lszh from ( select distinct lszh,spdm,dwdm,dwmc ,kzbz=(case when isnull(spdm,'')='' or isnull(dwdm,'')='' or isnull(dwmc,'')='' then 1 else 0 end) from ypjg_xs_wypjg with(nolock) where isnull(clbz,0)>=0 )x group by lszh having (count(*)>1 or max(kzbz)=1) ) group by lszh,spdm,dwdm,dwmc having max(smsj)<convert(varchar(10),getdate(),126) )y
union all
select 'g' as pjlx,lszh,spdm ,dwdm=(case when substring(dwdm,1,3)='DYF' then '80-PP' else dwdm end) ,dwmc,mxjls,xcljls,kcsjls,zhsmsj ,zfbz=(case when minclbz<-1 then 'y' else 'n' end) ,xclbz=(case when xcljls>0 then 'y' else 'n' end) ,kcsbz=(case when smlxs=1 and kcsjls>0 and kcsjls=xcljls then 'y' else 'n' end) ,smlx=(case when smlxs=1 and maxsmlx in ('采购入库','退货出库') then maxsmlx else '待定' end) from ( select lszh,spdm,dwdm,dwmc ,max(smsj) as zhsmsj ,max(isnull(smlx,'')) as maxsmlx , min(isnull(clbz,0)) as minclbz,count(*) as mxjls ,count(case when isnull(clbz,0) in (0,1,2) then lszh else null end) as xcljls ,count(case when isnull(clbz,0)=2 and isnull(smlx,'')in('采购入库','退货出库') then lszh else null end) as kcsjls ,count(distinct (case when isnull(clbz,0)<0 then null else isnull(smlx,'') end)) as smlxs from ypjg_gj_wypjg with(nolock) where lszh not in ( select lszh from (
select distinct lszh,spdm,dwdm,dwmc ,kzbz=(case when isnull(spdm,'')='' or isnull(dwdm,'')='' or isnull(dwmc,'')='' then 1 else 0 end) from ypjg_gj_wypjg with(nolock) where isnull(clbz,0)>=0 )x group by lszh having (count(*)>1 or max(kzbz)=1) ) group by lszh,spdm,dwdm,dwmc having max(smsj)<convert(varchar(10),getdate(),126) )y
select lszh,spdm,dwdm,dwmc ,max(smsj) as zhsmsj ,max(isnull(smlx,'')) as maxsmlx ,min(isnull(clbz,0)) as minclbz ,count(*) as mxjls ,count(case when isnull(clbz,0) in (0,1,2) then lszh else null end) as xcljls ,count(case when isnull(clbz,0)=2 and isnull(smlx,'')in('销售出库','退货入库','抽检出库','销毁出库') then lszh else null end) as kcsjls ,count(distinct (case when isnull(clbz,0)<0 then null else isnull(smlx,'') end)) as smlxs from ypjg_xs_wypjg with(nolock) where lszh not in ( select lszh from ( select distinct lszh,spdm,dwdm,dwmc ,kzbz=(case when isnull(spdm,'')='' or isnull(dwdm,'')='' or isnull(dwmc,'')='' then 1 else 0 end) from ypjg_xs_wypjg with(nolock) where isnull(clbz,0)>=0 )x group by lszh having (count(*)>1 or max(kzbz)=1) ) group by lszh,spdm,dwdm,dwmc having max(smsj)<convert(varchar(10),getdate(),126)
1,对lszh 建 INDEX , 2,对 clbz建INDEX , 3,把2句 select lszh from ( 改为 select TOP (2147483647) lszh from ( 4 把 where isnull(clbz,0)>=0 改为 where clbz >=0 or clbz is null
祝你好运。
[解决办法] 把 not in 修改成外连接 and 对应值=null [解决办法]
首先not in 跟外连接 and 对应值=null在语义上不不等的, 只有not exists 才跟跟外连接 and 对应值=null在语义上相等,而通过算法理论跟实现都可以来证明,not exists 的性能永远大于或者等于外连接 and 对应值=null,见过各种SQL SERVER的 MVP 有这种误解。 [解决办法] 1、考虑是否必须用视图。 2、执行计划最大的那些是啥操作 [解决办法]
CREATE VIEW [dbo].[View_X] AS SELECT [Production].[Product].* FROM [Production].[Product] Where [ProductNumber]='FR-M94B-42
开启SQL profiler,选择showplan all for query compile ,showplan all 2个事情 然后执行Select * From [dbo].[View_X] 会发现按如下顺序产生3个事件, showplan all for query compile SQL:Batchstarting showplan all
再次执行Select * From [dbo].[View_X] ,会发现showplan all for query compile 没有了,只有下面2个事件, SQL:Batchstarting showplan all 这就证明2个事情 1,VIEW里的T-SQL 同样也是在真正执行之前编译的。 2,VIEW里的T-SQL 同意享受SP “缓存”的特性,其实在05开始,计划的“缓存”在AD-HOC里也支持。