加入收藏 | 设为首页 | 会员中心 | 我要投稿 衡阳站长网 (https://www.0734zz.cn/)- 数据集成、设备管理、备份、数据加密、智能搜索!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

SQL在SSRS中运行缓慢,但在SSMS中运行速度很快

发布时间:2021-03-15 02:45:36 所属栏目:MsSql教程 来源:网络整理
导读:我有这个问题: Select 'ALL' as name,'ALL' as pid,'ALL' as typeunion allSelect distinct instructor.name as name,instructor.Pid as pid,instructor_type as type From sisinfo.dbo.SISCRSI instructorinner join section_info as section on section.sc

我有这个问题:

Select 
    '<ALL>' as name,'<ALL>' as pid,'<ALL>' as type
union all
Select distinct 
    instructor.name as name,instructor.Pid as pid,instructor_type as type  
From sisinfo.dbo.SISCRSI instructor
inner join section_info as section 
    on section.sctn_id_code = instructor.sctn_id_code
Where section.sctn_term_code in (@Terms) 
    and section.subj_code in (@Subject)
order by name

当我在SSMS中运行它时,它在非常接近零的时间内完成.当我为它提供一些值时,它也可以在SSRS查询设计器中快速运行.但是当我预览报表时,查询至少需要两分钟才能运行.我不确定这里发生了什么,我之前从未遇到过SSRS这样的问题.

解决方法

正如评论中所讨论的,让我们摆脱参数,看看您的查询是否受到参数嗅探的影响.

为此,我们从头开始构建SQL语句. SSRS中的大多数内容都可以是包含SQL查询的表达式,因此您可以将其构建为字符串.使用参数,我们将使用JOIN将它们转换为逗号分隔列表.

因此,对于您的SQL语句,请进入“数据集属性”对话框(而不是查询编辑器),然后按fx表达式编辑器按钮将查询表达式编辑为文本,并使其成为表达式,如下所示:

="Select '<ALL>' as name,'<ALL>' as type "
&"union all "
&"Select distinct instructor.name as name,instructor_type as type " 
&"From sisinfo.dbo.SISCRSI instructor "
&"inner join section_info as section on section.sctn_id_code = instructor.sctn_id_code "
&"Where section.sctn_term_code in (" & Join(Parameters!Terms.Value,",") & ") "
&"and section.subj_code in (" & Join(Parameters!Subject.Value,") & ") "
&"order by name "

我们在这里做的是将SQL表达式转换为一个字符串,我们将多值参数作为字符串而不是参数提供,从而消除参数嗅探作为查询运行缓慢的原因.

如果你的查询在此之后很慢,你知道这不是参数嗅探问题,但至少你会将其视为原因.

(编辑:衡阳站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读