簡體   English   中英

SQL在SSRS中運行緩慢,但在SSMS中運行速度很快

[英]SQL runs slow in SSRS, but fast in SSMS

我有這個問題:

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 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 (" & Join(Parameters!Terms.Value, ",") & ") "
&"and section.subj_code in (" & Join(Parameters!Subject.Value, ",") & ") "
&"order by name "

我們在這里做的是將SQL表達式轉換為一個字符串,我們將多值參數作為字符串而不是參數提供,從而消除參數嗅探作為查詢運行緩慢的原因。

如果你的查詢在此之后很慢,你知道這不是參數嗅探問題,但至少你會將其視為原因。

當我遇到同樣的事情時,我嘗試了幾個選項。

首先是更改存儲過程中變量的參數,然后在查詢中使用這些新變量。

Declare @TermsNew DataType = @Terms
Declare @SubjectNew DataType = @Subject
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 (@TermsNew) and section.subj_code in (@SubjectNew)
order by name

這里已經提出的另一個選擇是添加

Option(Recompile);

在您的查詢結束時重新編譯執行計划。

雖然嘗試了上述內容但對我有用的東西...如果您正在拉大結果集,請確保在tablix屬性中設置為“如果可能,將結果保留在一個頁面上”。 我關掉了,我的報告從70秒變為7秒。

實際上,事實證明這個查詢不是那個問題,而是另一個同時運行的問題(我認為SSRS可以同時運行多個查詢)。 我認為它是貼出來的,因為它填充了一個字段。 感謝所有建議,我將來可能最終會使用它們。

這聽起來像參數嗅探給我 - 我總是看到這一點,當SELECT完全復雜。 我會將OPTION(RECOMPILE)提示添加到SQL的末尾。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM