Sql server 存储过程查询优化
我有下面的查询,它不能完全按照我希望的那样工作,而且速度非常慢,所以我想我应该寻求一些帮助Sql server 存储过程查询优化,sql-server,stored-procedures,query-optimization,Sql Server,Stored Procedures,Query Optimization,我有下面的查询,它不能完全按照我希望的那样工作,而且速度非常慢,所以我想我应该寻求一些帮助 CREATE PROCEDURE [dbo].[SummaryReport] @event varchar(7) = null, @pet_num varchar(12) = null AS BEGIN WITH pet_counts AS (SELECT event, pet_num, pageid,
CREATE PROCEDURE [dbo].[SummaryReport]
@event varchar(7) = null,
@pet_num varchar(12) = null
AS
BEGIN
WITH pet_counts
AS (SELECT event,
pet_num,
pageid,
linenum,
tot_sig_page,
IDNUM,
val_date,
obj_type
-- Objections
,case when sum(case when INV_SIG = '1' then 1 else 0 end) > 0 then convert(varchar(5), sum(case when INV_SIG = '1' then 1 else 0 end)) + ' Invalid Sig' else '' end as InvalidSignature
,case when sum(case when INV_ADR = '1' then 1 else 0 end) > 0 then convert(varchar(5), sum(case when INV_ADR = '1' then 1 else 0 end)) + ' Invalid Addr' else '' end as InvalidAddress
,case when sum(case when INV_DIST = '1' then 1 else 0 end) > 0 then convert(varchar(5), sum(case when INV_DIST = '1' then 1 else 0 end)) + ' Invalid Dist' else '' end as InvalidDistrict
,case when sum(case when inc_adr = '1' then 1 else 0 end) > 0 then convert(varchar(5), sum(case when inc_adr = '1' then 1 else 0 end)) + ' Inc Add' else '' end as IncAdd
,case when sum(case when dup_sig = '1' then 1 else 0 end) > 0 then convert(varchar(5), sum(case when dup_sig = '1' then 1 else 0 end)) + ' Dup Sig' else '' end as DupSig
,case when sum(case when Inv_Circulator = '1' then 1 else 0 end) > 0 then convert(varchar(5), sum(case when Inv_Circulator = '1' then 1 else 0 end)) + ' No CRC Date' else '' end as NoCRCDate
,case when sum(case when isnull(REASON,'') <> '' then 1 else 0 end) > 0 then convert(varchar(5), sum(case when isnull(REASON,'') <> '' then 1 else 0 end)) + ' Other' else '' end as OtherReason
,sum(case when INV_SIG = '1' then 1 else 0 end)
+ sum(case when INV_ADR = '1' then 1 else 0 end)
+ sum(case when INV_DIST = '1' then 1 else 0 end)
+ sum(case when inc_adr = '1' then 1 else 0 end)
+ sum(case when dup_sig = '1' then 1 else 0 end)
+ sum(case when Inv_Circulator = '1' then 1 else 0 end)
+ sum(case when isnull(REASON,'') <> '' then 1 else 0 end) as TotalObjections
-- Sustained
,case when sum(case when INV_SIG_ST = 'S' then 1 else 0 end) > 0 then convert(varchar(5), sum(case when INV_SIG_ST = 'S' then 1 else 0 end)) + ' Sustained (Invalid Sig)' else '' end as SustainedInvalidSignature
,case when sum(case when INV_ADR_ST = 'S' then 1 else 0 end) > 0 then convert(varchar(5), sum(case when INV_ADR_st = 'S' then 1 else 0 end)) + ' Sustained (Invalid Addr)' else '' end as SustainedInvalidAddress
,case when sum(case when INV_DIST_ST = 'S' then 1 else 0 end) > 0 then convert(varchar(5), sum(case when INV_DIST_st = 'S' then 1 else 0 end)) + ' Sustained (Invalid Dist)' else '' end as SustainedInvalidDistrict
,case when sum(case when inc_adr_ST = 'S' then 1 else 0 end) > 0 then convert(varchar(5), sum(case when inc_adr_st = 'S' then 1 else 0 end)) + ' Sustained (Inc Add)' else '' end as SustainedIncAdd
,case when sum(case when dup_sig_ST = 'S' then 1 else 0 end) > 0 then convert(varchar(5), sum(case when dup_sig_st = 'S' then 1 else 0 end)) + ' Sustained (Dup Sig)' else '' end as SustainedDupSig
,case when sum(case when Inv_Circulator_ST = 'S' then 1 else 0 end) > 0 then convert(varchar(5), sum(case when Inv_Circulator_ST = 'S' then 1 else 0 end)) + ' Sustained (No CRC Date)' else '' end as SustainedNoCRCDate
,case when sum(case when oth_reas_ST = 'S' then 1 else 0 end) > 0 then convert(varchar(5), sum(case when oth_reas_st = 'S' then 1 else 0 end)) + ' Sustained (Other)' else '' end as SustainedOtherReason
,sum(case when INV_SIG_ST = 'S' then 1 else 0 end)
+ sum(case when INV_ADR_ST = 'S' then 1 else 0 end)
+ sum(case when INV_DIST_ST = 'S' then 1 else 0 end)
+ sum(case when inc_adr_ST = 'S' then 1 else 0 end)
+ sum(case when dup_sig_ST = 'S' then 1 else 0 end)
+ sum(case when Inv_Circulator_ST = 'S' then 1 else 0 end)
+ sum(case when oth_reas_ST = 'S' then 1 else 0 end) as TotalSustained
-- Overruled
,case when sum(case when INV_SIG_ST = 'O' then 1 else 0 end) > 0 then convert(varchar(5), sum(case when INV_SIG_ST = 'O' then 1 else 0 end)) + ' Overruled (Invalid Sig)' else '' end as OverruledInvalidSignature
,case when sum(case when INV_ADR_ST = 'O' then 1 else 0 end) > 0 then convert(varchar(5), sum(case when INV_ADR_st = 'O' then 1 else 0 end)) + ' Overruled (Invalid Addr)' else '' end as OverruledInvalidAddress
,case when sum(case when INV_DIST_ST = 'O' then 1 else 0 end) > 0 then convert(varchar(5), sum(case when INV_DIST_st = 'O' then 1 else 0 end)) + ' Overruled (Invalid Dist)' else '' end as OverruledInvalidDistrict
,case when sum(case when inc_adr_ST = 'O' then 1 else 0 end) > 0 then convert(varchar(5), sum(case when inc_adr_st = 'O' then 1 else 0 end)) + ' Overruled (Inc Add)' else '' end as OverruledIncAdd
,case when sum(case when dup_sig_ST = 'O' then 1 else 0 end) > 0 then convert(varchar(5), sum(case when dup_sig_st = 'O' then 1 else 0 end)) + ' Overruled (Dup Sig)' else '' end as OverruledDupSig
,case when sum(case when Inv_Circulator_ST = 'O' then 1 else 0 end) > 0 then convert(varchar(5), sum(case when Inv_Circulator_ST = 'O' then 1 else 0 end)) + ' Overruled (No CRC Date)' else '' end as OverruledNoCRCDate
,case when sum(case when oth_reas_ST = 'O' then 1 else 0 end) > 0 then convert(varchar(5), sum(case when oth_reas_st = 'O' then 1 else 0 end)) + ' Overruled (Other)' else '' end as OverruledOtherReason
,sum(case when INV_SIG_ST = 'O' then 1 else 0 end)
+ sum(case when INV_ADR_ST = 'O' then 1 else 0 end)
+ sum(case when INV_DIST_ST = 'O' then 1 else 0 end)
+ sum(case when inc_adr_ST = 'O' then 1 else 0 end)
+ sum(case when dup_sig_ST = 'O' then 1 else 0 end)
+ sum(case when Inv_Circulator_ST = 'O' then 1 else 0 end)
+ sum(case when oth_reas_ST = 'O' then 1 else 0 end) as TotalOverruled
-- Cand Exceptions
,sum(case when INV_SIG_EX = 'C' then 1 else 0 end)
+ sum(case when INV_ADR_EX = 'C' then 1 else 0 end)
+ sum(case when INV_DIST_EX = 'C' then 1 else 0 end)
+ sum(case when inc_adr_EX = 'C' then 1 else 0 end)
+ sum(case when dup_sig_EX = 'C' then 1 else 0 end)
+ sum(case when Inv_Circulator_EX= 'C' then 1 else 0 end)
+ sum(case when oth_reas_EX = 'C' then 1 else 0 end) as TotalCandidateExceptions
-- Objector Exceptions
,sum(case when INV_SIG_EX = 'O' then 1 else 0 end)
+ sum(case when INV_ADR_EX = 'O' then 1 else 0 end)
+ sum(case when INV_DIST_EX = 'O' then 1 else 0 end)
+ sum(case when inc_adr_EX = 'O' then 1 else 0 end)
+ sum(case when dup_sig_EX = 'O' then 1 else 0 end)
+ sum(case when Inv_Circulator_EX = 'O' then 1 else 0 end)
+ sum(case when oth_reas_EX = 'O' then 1 else 0 end) as TotalObjectorExceptions
FROM petchl
WHERE event=@event
AND pet_num=@pet_num
GROUP BY event,
pet_num,
pageid,
linenum,
tot_sig_page,
IDNUM,
val_date,
obj_type),
user_info as
(
SELECT vp.IDNUM,
v.full_name,
ltrim((isnull(rtrim(ltrim(v.addr_num)),''))
+ ' ' + isnull(rtrim(ltrim(v.addr_frac)),'')
+ ' ' + isnull(rtrim(ltrim(v.addr_dir)),'')
+ ' ' + isnull(rtrim(ltrim(v.addr_str)),'')
+ ' ' + isnull(rtrim(ltrim(v.addr_type)),'')
+ ' ' + isnull(rtrim(ltrim(v.addr_other)),'')) as address1,
(isnull(v.cityname,'')+ ' ' + isnull(v.addr_zip,'')) as address2,
v.regdate,
v.birthdate,
v.sex,
v.prec,
s.signature
FROM petchl AS vp INNER JOIN
v_JPPUsers AS v ON vp.IDNUM = v.IDNUM LEFT OUTER JOIN
Signatures AS s ON v.IDNUM = s.IDNUM
WHERE vp.event=@event
AND vp.pet_num=@pet_num
UNION ALL
SELECT vp.IDNUM,
v.full_name,
ltrim((isnull(rtrim(ltrim(v.addr_num)),''))
+ ' ' + isnull(rtrim(ltrim(v.addr_frac)),'')
+ ' ' + isnull(rtrim(ltrim(v.addr_dir)),'')
+ ' ' + isnull(rtrim(ltrim(v.addr_str)),'')
+ ' ' + isnull(rtrim(ltrim(v.addr_type)),'')
+ ' ' + isnull(rtrim(ltrim(v.addr_other)),'')) as address1,
(isnull(v.cityname,'')+ ' ' + isnull(v.addr_zip,'')) as address2,
null as regdate,
v.birthdate,
v.sex,
v.prec,
s.signature
FROM petchl AS vp INNER JOIN
v_Cityusers AS v ON vp.IDNUM = v.IDNUM LEFT OUTER JOIN
v_CitySignatures AS s ON v.IDNUM = s.IDNUM
WHERE vp.event=@event
AND vp.pet_num=@pet_num
)
SELECT p.event,
p.PET_NUM,
p.PAGEID,
p.LINENUM,
convert(varchar(10), vp.pet_date, 101) as pet_date,
p.InvalidSignature,
p.InvalidAddress,
p.InvalidDistrict,
p.IncAdd,
p.DupSig,
p.NoCRCDate,
p.OtherReason,
p.TotalObjections,
p.SustainedInvalidSignature,
p.SustainedInvalidAddress,
p.SustainedInvalidDistrict,
p.SustainedIncAdd,
p.SustainedDupSig,
p.SustainedNoCRCDate,
p.SustainedOtherReason,
p.TotalSustained,
p.OverruledInvalidSignature,
p.OverruledInvalidAddress,
p.OverruledInvalidDistrict,
p.OverruledIncAdd,
p.OverruledDupSig,
p.OverruledNoCRCDate,
p.OverruledOtherReason,
p.TotalOverruled,
p.TotalCandidateExceptions,
p.TotalObjectorExceptions,
p.TOT_SIG_PAGE,
v.full_name,
v.address1,
v.address2,
p.IDNUM,
v.regdate,
v.birthdate,
convert(varbinary(max), v.signature) as signature
FROM pet_counts p
LEFT OUTER JOIN user_info v
ON p.IDNUM = v.IDNUM
LEFT OUTER JOIN vrpet vp
ON p.event = vp.event
AND p.PET_NUM = vp.PET_NUM
WHERE p.event = @event
and p.pet_num = @pet_num
ORDER BY pageid,
linenum
END
创建过程[dbo]。[SummaryReport]
@事件varchar(7)=空,
@pet_num varchar(12)=空
作为
开始
有宠物计数
AS(选择事件,
宠物数量,
pageid,
莱纳姆,
总信号页,
IDNUM,
瓦卢日期,
obj_型
--反对意见
,sum时的大小写(INV_SIG='1'然后1 else 0 end时的大小写)>0,然后转换(varchar(5),sum时的大小写(INV_SIG='1'然后1 else 0 end时的大小写)+'Invalid SIG'else'作为无效签名结束
,sum时的case(INV_ADR='1'然后1 else 0 end时的case)>0,然后转换(varchar(5),sum(INV_ADR='1'然后1 else 0 end时的case)+'Invalid Addr'else'作为InvalidAddress结束
,sum时的大小写(INV_DIST='1'然后1 else 0 end时的大小写)>0,然后转换(varchar(5),sum时的大小写(INV_DIST='1'然后1 else 0 end时的大小写)+'Invalid DIST'else'作为InvalidDistrict结束
,sum时的case(inc_adr='1'然后1 else 0 end时的case)>0,然后转换(varchar(5),sum(inc_adr='1'然后1 else 0 end时的case)+'inc Add'else'作为IncAdd结束
,sum时的大小写(dup_sig='1'然后1 else 0 end时的大小写)>0,然后转换(varchar(5),sum(dup_sig='1'然后1 else 0 end时的大小写)+'dup sig'else'作为DupSig结束
,sum时的大小写(Inv_循环器='1'然后1 else 0 end时的大小写)>0,然后转换(varchar(5),sum时的大小写(Inv_循环器='1'然后1 else 0 end时的大小写)+'No CRC Date'else'作为NoCRCDate结束
,sum时的case-when-sum(case-when-isnull(原因“”)“”然后1-else 0-end)>0,然后转换(varchar(5),sum(case-when-isnull(原因“”)“”然后1-else 0-end))+“其他“else”作为其他原因结束
,求和(当INV_SIG='1'然后1其他0结束时的情况)
+总和(当INV_ADR='1'然后1其他0结束时的情况)
+总和(当INV_DIST='1'然后1 else 0结束时的情况)
+总和(当inc_adr='1'时,则为1,否则为0结束)
+总和(当dup_sig='1'然后1 else 0结束时的情况)
+求和(当Inv_循环器='1'然后1 else 0结束时的情况)
+求和(如果为空(原因为“”),则为1,否则为0结束)作为TotalObjections
--持续的
,sum时的case(当INV_SIG_ST='S'然后1 else 0 end时的case)>0,然后转换(varchar(5),sum(当INV_SIG_ST='S'然后1 else 0 end时的case))+'sustainated(无效SIG)'else'作为sustainedValidSignature结束
,sum时的case(INV_ADR_ST='S'然后1 else 0 end时的case)>0,然后转换(varchar(5),sum(INV_ADR_ST='S'然后1 else 0 end时的case))+'sustainated(无效Addr)'else'作为sustainatedValidAddress结束
,sum时的大小写(INV_DIST_ST='S'然后1 else 0 end时的大小写)>0,然后转换(varchar(5),sum时的大小写(INV_DIST_ST='S'然后1 else 0 end时的大小写)+'sustainated(无效DIST)'else'作为sustainatedValidDistrict结束
,sum时的case(inc_adr_ST='S'然后1 else 0 end时的case)>0,然后转换(varchar(5),sum(inc_adr_ST='S'然后1 else 0 end时的case))+'持续(inc Add)'else'作为持续CADD结束
,sum时的大小写(dup_sig_ST='S'然后1 else 0 end时的大小写)>0,然后转换(varchar(5),sum(dup_sig_ST='S'然后1 else 0 end时的大小写)+'sustainated(dup sig)'else'作为sustainateddupsig结束
,sum时的大小写(Inv_circular_ST='S'然后1 else 0 end时的大小写)>0,然后转换(varchar(5),sum时的大小写(Inv_circular_ST='S'然后1 else 0 end时的大小写)+'sustainated(无CRC日期)'else'作为sustainatedNocrcdate结束
,sum时的case when sum(oth_reas_ST='S'然后1 else 0 end时的case)>0,然后转换(varchar(5),sum(oth_reas_ST='S'然后1 else 0 end时的case))+'持续(其他)'else'作为持续其他原因结束
,求和(当INV_SIG_ST='S'然后1或0结束时的情况)
+总和(当库存ADR ST='S'然后1或0结束时的情况)
+总和(当库存区ST='S'然后1或0结束时的情况)
+总和(inc_adr_ST='S'然后1或0结束时的情况)
+总和(当dup_sig_ST='S'然后1或0结束时的情况)
+总和(当Inv_Circular_ST='S'然后1或0结束时的情况)
+总和(当其他值为'S'时,则为1,否则为0结束)作为TotalSustainable
--否决
,sum时的case(INV_SIG_ST='O'然后1 else 0 end时的case)>0,然后转换(varchar(5),sum(INV_SIG_ST='O'然后1 else 0 end时的case))+'overrulled(无效SIG)'else'作为overrulledinvalidSignature结束
,sum时的case(INV_ADR_ST='O'然后1 else 0 end时的case)>0然后转换(varchar(5),sum(INV_ADR_ST='O'然后1 else 0 end时的case))+'overrulled(无效Addr)'else'作为overrulledinvalidAddress结束
,sum时的大小写(INV_DIST_ST='O'然后1 else 0 end时的大小写)>0,然后转换(varchar(5),sum时的大小写(INV_DIST_ST='O'然后1 else 0 end时的大小写)+'Overruled(无效DIST)'else'作为Overruled有效district结束
,sum时的case when sum(inc_adr_ST='O'然后1 else 0 end时的case)>0,然后转换(varchar(5),sum(inc_adr_ST='O'然后1 else 0 end时的case)+'Overruled(inc Add)'else'作为Overruled结束
,sum时的大小写(dup_sig_ST='O'然后1 else 0 end时的大小写)>0,然后转换(varchar(5),sum(dup_sig_ST='O'然后1 else 0 end时的大小写)+'overrulled(dup sig)'else'作为overrulleddupsig结束
,sum时的case when sum(Inv_circular_ST='O'然后1 else 0 end时的case)>0,然后转换(varchar(5),sum(Inv_circular_ST='O'然后1 else 0 end时的case))+“被否决(无CRC日期)”“else”作为被否决的OCRCDATE结束
,sum时的case when sum(oth_reas_ST='O'那么1 else 0 end时的case)>0然后转换(varchar(5),sum(oth_reas_ST='O'那么1 else 0 end时的case))+'Overruled(Other)'else'作为Overruled结束原因
,总和(当库存信号ST='O'然后为1,否则为0结束时)
+总和(当库存ADR ST='O'然后1或0结束时)
+总和(当库存区ST='O'然后1或0结束时的情况)
+总和(inc_adr_ST='O'然后1或0结束时的情况)
+总和(当dup_sig_ST='O'然后1或0结束时的情况)
+总和(cas)
Take it one piece at a time
case
when sum(case when INV_ADR = '1' then 1 else 0 end) > 0
then convert(varchar(5), sum(case when INV_ADR = '1' then 1 else 0 end)) + ' Invalid Addr'
else '' end as InvalidAddress
sum(INV_ADR) AS [NumberOfInvalidAddr]
case
when [NumberOfInvalidAddr] > 0
then convert(varchar(5), [NumberOfInvalidAddr]) + ' Invalid Addr'
else '' end as InvalidAddress