查询以检索单页网格的有限记录

如何解决查询以检索单页网格的有限记录

我想使用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 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-