问题描述
我想使用sql Server过程填充网格,该过程从多个表中检索数据。我已尽我所能优化查询,并加快了从数据库到页面的数据传递速度。目前,我正在使用下面的查询,它在大约8秒内返回1500条记录中的50条。
如果有人可以建议任何修改以使我的查询更快地返回所需的结果,将不胜感激。
ALTER proc [dbo].[LoadPCIList]
@iduser int,@page int,@pagesize int,@filters nvarchar(max),@sortcolumn nvarchar(max),@sortdirection nvarchar(4),@sorttype nvarchar(7)
as
declare
@idpci nvarchar(max) = null,@no nvarchar(max) = null,@pcino nvarchar(max) = null,@pono nvarchar(max) = null,@blno nvarchar(max) = null,@customer nvarchar(max) = null,@pcidate nvarchar(max) = null,@pcidateshamsi nvarchar(max) = null,@vchtype nvarchar(max) = null,@commodity nvarchar(max) = null,@qty nvarchar(max) = null,@amountpci nvarchar(max) = null,@currencypci nvarchar(max) = null,@rate nvarchar(max) = null,@amountpi nvarchar(max) = null,@currencypi nvarchar(max) = null,@settled nvarchar(max) = null,@remained nvarchar(max) = null,@thirdparty nvarchar(max) = null,@step nvarchar(max) = null,@workflow nvarchar(max) = null,@issuer nvarchar(max) = null,@fileqty nvarchar(max) = null,@note nvarchar(max) = null
select
@idpci = idpci,@no = no,@pcino = pcino,@pono = pono,@blno = blno,@customer = customer,@pcidate = pcidate,@pcidateshamsi = pcidateshamsi,@vchtype = vchtype,@commodity = commodity,@qty = qty,@amountpci = amountpci,@currencypci = currencypci,@rate = rate,@amountpi = amountpi,@currencypi = currencypi,@settled = settled,@remained = remained,@thirdparty = thirdparty,@step = step,@workflow = workflow,@issuer = issuer,@fileqty = fileqty,@note = note
FROM OPEnjsON(@filters)
WITH (idpci nvarchar(max) 'strict $.idpci',no nvarchar(max) 'strict $.no',pcino nvarchar(max) 'strict $.pcino',pono nvarchar(max) 'strict $.pono',blno nvarchar(max) 'strict $.blno',customer nvarchar(max) 'strict $.customer',pcidate nvarchar(max) 'strict $.pcidate',pcidateshamsi nvarchar(max) 'strict $.pcidateshamsi',vchtype nvarchar(max) 'strict $.vchtype',commodity nvarchar(max) 'strict $.commodity',qty nvarchar(max) 'strict $.qty',amountpci nvarchar(max) 'strict $.amountpci',currencypci nvarchar(max) 'strict $.currencypci',rate nvarchar(max) 'strict $.rate',amountpi nvarchar(max) 'strict $.amountpi',currencypi nvarchar(max) 'strict $.currencypi',settled nvarchar(max) 'strict $.settled',remained nvarchar(max) 'strict $.remained',thirdparty nvarchar(max) 'strict $.thirdparty',step nvarchar(max) 'strict $.step',workflow nvarchar(max) 'strict $.workflow',issuer nvarchar(max) 'strict $.issuer',fileqty nvarchar(max) 'strict $.fileqty',note nvarchar(max) 'strict $.note'
)
select * into #temp from (
select
a.IdPci,a.No,a.PciNo,a.PoNo,a.BlNo,c.Title as Customer,a.PciDate,a.PciDateShamsi,b.VchType,a.commodity,a.Qty,Format(a.Amount,'N2') as AmountPci,d.Abr as CurrencyPci,a.Rate,Format(a.Amount / a.Rate,'N2') as AmountPi,e.Abr as CurrencyPi,Format(a.Settled,'N2') as Settled,Format(a.Remained,'N2') as Remained,i.Title as ThirdParty,g.Title as Step,h.IdWorkflow,h.Title as Workflow,(select top(1) y.UserName from history as x inner join users as y on x.IdUser = y.IdUser where IdForm = 117 and idvch = a.idpci ) as Issuer,(select count(idattachment) from attachment where idform = 117 and idvch = a.idpci ) as FileQty,a.Note,a.State
from PCI as a
left join pi as b on a.idpi = b.idpi
left join DL as c on b.IdCustomer = c.iddl
left join Currency as d on a.IdCur = d.IdCur
left join Currency as e on b.IdCur = e.IdCur
left join FM_Steps as g on a.IdStep = g.IdStep
left join FM_Workflow as h on a.IdWorkFlow = h.IdWorkflow
left join dl as i on a.IdTrusteeAccount = i.IdDl
where isnull(a.state,0) <> -1
and (@idpci ='' or cast(a.IdPci as nvarchar(max)) like '%'+@idpci+'%')
and (@no ='' or cast(a.No as nvarchar(max)) like '%'+@no+'%')
and (@pcino ='' or a.pcino like '%'+@pcino+'%')
and (@pono ='' or a.pono like '%'+@pono+'%')
and (@blno ='' or a.blno like '%'+@blno+'%')
and (@customer ='' or c.Title like '%'+@customer+'%')
and (@pcidate ='' or cast(a.pcidate as nvarchar(max)) like '%'+@pcidate+'%')
and (@pcidateshamsi ='' or a.PciDateShamsi like '%'+@pcidateshamsi+'%')
and (@vchtype ='' or isnull(b.VchType,'') like '%'+@vchtype+'%')
and (@commodity ='' or a.commodity like '%'+@commodity+'%')
and (@qty ='' or cast(a.Qty as nvarchar(max)) like '%'+@qty+'%')
and (@amountpci ='' or cast(a.amount as nvarchar(max)) like '%'+@amountpci+'%')
and (@currencypci ='' or cast(d.Abr as nvarchar(max)) like '%'+@currencypci+'%')
and (@rate ='' or d.Title like '%'+@rate+'%')
and (@amountpi ='' or cast(a.amount * a.Rate as nvarchar(max)) like '%'+@amountpi+'%')
and (@currencypi ='' or cast(e.Abr as nvarchar(max)) like '%'+@currencypi+'%')
and (@settled ='' or cast(a.Settled as nvarchar(max)) like '%'+@settled+'%')
and (@remained ='' or cast(a.remained as nvarchar(max)) like '%'+@remained+'%')
and (@thirdparty ='' or i.Title like '%'+@thirdparty+'%')
and (@step ='' or g.Title like '%'+@step+'%')
and (@workflow ='' or h.Title like '%'+@workflow+'%')
and (@issuer ='' or (select top(1) y.UserName from history as x inner join users as y on x.IdUser = y.IdUser where IdForm = 116 and idvch = a.idpci ) like '%'+@issuer+'%')
and (@fileqty ='' or (select cast(count(idattachment) as nvarchar(3)) from attachment where idform = 115 and idvch = a.idpci ) like '%'+@fileqty+'%')
and (@note ='' or a.note like '%'+@note+'%')
)s
declare @totalrowscount int = (select count(idpci) from #temp)
declare @totalpagescount int = floor(@totalrowscount / @pagesize)
if(@totalrowscount % @pagesize >=1 )
set @totalpagescount = @totalpagescount + 1
if(@totalrowscount = 0 )
set @totalpagescount = 1 ;
if(@page > @totalpagescount)
set @page = @totalpagescount ;
select cast ((
select
(@totalrowscount) as Qty,(select * from #temp order by
case when @sortdirection = 'asc' and @sorttype = 'numeric' then
case when @sortcolumn = 'colIdPci' then IdPci
when @sortcolumn = 'colNo' then No
when @sortcolumn = 'colFileQty' then FileQty
when @sortcolumn = 'colQty' then Qty
when @sortcolumn = 'colAmountPci' then AmountPci
when @sortcolumn = 'colRate' then Rate
when @sortcolumn = 'colAmountPi' then AmountPi
when @sortcolumn = 'colSettled' then Settled
when @sortcolumn = 'colRemained' then Remained
end
else 1
end asc,case when @sortdirection = 'asc' and @sorttype = '' then
case when @sortcolumn = 'colPciNo' then PciNo
when @sortcolumn = 'colPoNo' then PoNo
when @sortcolumn = 'colCustomer' then Customer
when @sortcolumn = 'colPciDate' then PciDateShamsi
when @sortcolumn = 'colPciDateShamsi' then PciDateShamsi
when @sortcolumn = 'colcommodity' then commodity
when @sortcolumn = 'colVchType' then VchType
when @sortcolumn = 'colCurrencyPci' then CurrencyPci
when @sortcolumn = 'colCurrencyPi' then CurrencyPi
when @sortcolumn = 'colThirdParty' then ThirdParty
when @sortcolumn = 'colStep' then Step
when @sortcolumn = 'colWorkflow' then Workflow
when @sortcolumn = 'colIssuer' then Issuer
when @sortcolumn = 'colNote' then Note
end
else ''
end asc,case when @sortdirection = 'desc' and @sorttype = 'numeric' then
case when @sortcolumn = 'colIdPci' then IdPci
when @sortcolumn = 'colNo' then No
when @sortcolumn = 'colFileQty' then FileQty
when @sortcolumn = 'colQty' then Qty
when @sortcolumn = 'colAmountPci' then AmountPci
when @sortcolumn = 'colRate' then Rate
when @sortcolumn = 'colAmountPi' then AmountPi
when @sortcolumn = 'colSettled' then Settled
when @sortcolumn = 'colRemained' then Remained
end
else 1
end desc,case when @sortdirection = 'desc' and @sorttype = '' then
case when @sortcolumn = 'colPciNo' then PciNo
when @sortcolumn = 'colPoNo' then PoNo
when @sortcolumn = 'colCustomer' then Customer
when @sortcolumn = 'colPciDate' then PciDateShamsi
when @sortcolumn = 'colPciDateShamsi' then PciDateShamsi
when @sortcolumn = 'colcommodity' then commodity
when @sortcolumn = 'colVchType' then VchType
when @sortcolumn = 'colCurrencyPci' then CurrencyPci
when @sortcolumn = 'colCurrencyPi' then CurrencyPi
when @sortcolumn = 'colThirdParty' then ThirdParty
when @sortcolumn = 'colStep' then Step
when @sortcolumn = 'colWorkflow' then Workflow
when @sortcolumn = 'colIssuer' then Issuer
when @sortcolumn = 'colNote' then Note
end
else ''
end desc
offset (@page-1)*@pagesize rows fetch next @pagesize rows only for json auto,include_null_values ) as PCIs
from setting for json auto,include_null_values) as nvarchar(max))
drop table #temp
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)