c#用戶端調用sql server 預存程序速度慢的問題

來源:互聯網
上載者:User

標籤:blog   io   os   ar   sp   div   on   2014   art   

1 : c#代碼是

 

 

                        SqlConnection connNew = null;                        connNew = new SqlConnection(@"Data Source="XXX");                        connNew.Open();                        SqlCommand commNew = connNew.CreateCommand();                        commNew.CommandText = "sp_catcher_MeraRptCAV_Insert_detail";                        commNew.CommandType = System.Data.CommandType.StoredProcedure;                        commNew.CommandTimeout = 300;                        commNew.Parameters.Add(new SqlParameter()                        {                            ParameterName = "@TVP",                            SqlDbType = SqlDbType.Structured,                            Value = GetDataTableParamNew(CANewReport)                        });                        //File.AppendAllText(strCurrentPath + @"\test.txt", "Now about to insert report " + reportID + " records into db " + "\r\n");                        try                        {                            commNew.CommandTimeout = 300;                            commNew.ExecuteNonQuery();                        }                        catch (Exception ex)                        {                            //Logger.Error("Execute Procedure ‘sp_merarpt_create‘ Failed, Error Message : ‘" + ex.Message + "‘.");                        }                        finally                        {                            commNew.Dispose();                            if (commNew != null)                            {                                connNew.Close();                                connNew.Dispose();                            }                        }

 

2 : 被調用的預存程序 , 原來的代碼是 :

USE [Smartbilling_AI]GO/****** Object:  StoredProcedure [dbo].[sp_catcher_MeraRptCAV_Insert_detail]    Script Date: 10/29/2014 09:28:39 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER PROCEDURE [dbo].[sp_catcher_MeraRptCAV_Insert_detail](@TVP dbo.MeraRpt_CAV_AI READONLY)ASBEGINdeclare @reportid as intSET NOCOUNT ON;        INSERT INTO MeraRpt_Detail_ByPrefix        ([reportID],[Customer],[Prefix],[Route],[ASRSuccessCalls],[ACDSuccessCalls],[TotalCalls],[TotalMins],[ASR],[ACD],[Fee],[Cost],[Profit])SELECT [reportID],[Customer],[Area],[Vendor],[ASRsuccessCalls],[ACDsuccessCalls],[totalCalls],[totalMins],[ASR],[ACD],[Fee],[Cost],[profix]FROM @TVP tvpselect @reportid = max(reportid) from merarpt--refresh area report--exec MeraRpt_Refresh_Area --exec MeraRpt_Refresh_cr--Update Report By PrefixBEGIN TRYexec [sp_i_Refresh_Cust_Prefix] @reportidEND TRYBEGIN CATCHinsert capturelog (content) values (‘[sp_i_Refresh_Cust_Prefix] Warning Code:‘ + cast (@@error as nvarchar) + ‘ Warning Lines: ‘ + Cast (ERROR_LINE() as nvarchar) + ‘ Warning Msg: ‘+ERROR_MESSAGE())END CATCH--Update Report By Prefix HourlyBEGIN TRYexec [sp_i_Refresh_Cust_Prefix_hour] @reportidEND TRYBEGIN CATCHinsert capturelog (content) values (‘[sp_i_Refresh_Cust_Prefix_hour] Warning Code:‘ + cast (@@error as nvarchar) + ‘ Warning Lines: ‘ + Cast (ERROR_LINE() as nvarchar) + ‘ Warning Msg: ‘+ERROR_MESSAGE())END CATCH----Update Report By AreaBEGIN TRYexec [sp_i_Refresh_Cust_Area] @reportidEND TRYBEGIN CATCHinsert capturelog (content) values (‘[sp_i_Refresh_Cust_Area] Warning Code:‘ + cast (@@error as nvarchar) + ‘ Warning Lines: ‘ + Cast (ERROR_LINE() as nvarchar) + ‘ Warning Msg: ‘+ERROR_MESSAGE())END CATCH----Update Report By Area HourlyBEGIN TRYexec [sp_i_Refresh_Cust_Area_Hour] @reportidEND TRYBEGIN CATCHinsert capturelog (content) values (‘[sp_i_Refresh_Cust_Area_Hour] Warning Code:‘ + cast (@@error as nvarchar) + ‘ Warning Lines: ‘ + Cast (ERROR_LINE() as nvarchar) + ‘ Warning Msg: ‘+ERROR_MESSAGE())END CATCH----Update Report By Area RouteBEGIN TRYexec [sp_i_Refresh_Cust_Area_Route] @reportidEND TRYBEGIN CATCHinsert capturelog (content) values (‘[sp_i_Refresh_Cust_Area_Route] Warning Code:‘ + cast (@@error as nvarchar) + ‘ Warning Lines: ‘ + Cast (ERROR_LINE() as nvarchar) + ‘ Warning Msg: ‘+ERROR_MESSAGE())END CATCH    --以下是 vendor 部分 BEGIN TRYexec [sp_i_Refresh_Vendor_Prefix] @reportid END TRYBEGIN CATCHinsert capturelog (content) values (‘[sp_i_Refresh_Vendor_Prefix] Warning Code:‘ + cast (@@error as nvarchar) + ‘ Warning Lines: ‘ + Cast (ERROR_LINE() as nvarchar) + ‘ Warning Msg: ‘+ERROR_MESSAGE())END CATCHBEGIN TRYexec [sp_i_Refresh_Vendor_Prefix_hour] @reportidEND TRYBEGIN CATCHinsert capturelog (content) values (‘[sp_i_Refresh_Vendor_Prefix_hour] Warning Code:‘ + cast (@@error as nvarchar) + ‘ Warning Lines: ‘ + Cast (ERROR_LINE() as nvarchar) + ‘ Warning Msg: ‘+ERROR_MESSAGE())END CATCHBEGIN TRYexec [sp_i_Refresh_Vendor_Area] @reportid   END TRYBEGIN CATCHinsert capturelog (content) values (‘[sp_i_Refresh_Vendor_Area] Warning Code:‘ + cast (@@error as nvarchar) + ‘ Warning Lines: ‘ + Cast (ERROR_LINE() as nvarchar) + ‘ Warning Msg: ‘+ERROR_MESSAGE())END CATCHBEGIN TRYexec [sp_i_Refresh_Vendor_Area_hour] @reportidEND TRYBEGIN CATCHinsert capturelog (content) values (‘[sp_i_Refresh_Vendor_Area_hour] Warning Code:‘ + cast (@@error as nvarchar) + ‘ Warning Lines: ‘ + Cast (ERROR_LINE() as nvarchar) + ‘ Warning Msg: ‘+ERROR_MESSAGE())END CATCHBEGIN TRYexec [sp_i_Refresh_Vendor_Area_Route] @reportidEND TRYBEGIN CATCHinsert capturelog (content) values (‘[sp_i_Refresh_Vendor_Area_Route] Warning Code:‘ + cast (@@error as nvarchar) + ‘ Warning Lines: ‘ + Cast (ERROR_LINE() as nvarchar) + ‘ Warning Msg: ‘+ERROR_MESSAGE())END CATCH    update merarpt set FinishProcessedTime = getdate() where reportid = @reportidEND

 注意裡面有若干處 調用其它預存程序 , just like

 

BEGIN TRYexec [sp_i_Refresh_Vendor_Prefix] @reportid END TRYBEGIN CATCHinsert capturelog (content) values (‘[sp_i_Refresh_Vendor_Prefix] Warning Code:‘ + cast (@@error as nvarchar) + ‘ Warning Lines: ‘ + Cast (ERROR_LINE() as nvarchar) + ‘ Warning Msg: ‘+ERROR_MESSAGE())END CATCH

 

3 : 然後c#程式啟動並執行很慢,但奇怪的是, 如果在sql server management studio 裡單獨運行這個預存程序, 速度又很快 ,

 

4 : 找不到問題的原因 , 後來是把 所有在預存程序被調用的預存程序的代碼都取出來, 直接寫在 主預存程序裡, 而不再間接調用, 這樣問題得到瞭解決,但是,其中原因何在, 還不清楚, 暫時記在這裡。

 

c#用戶端調用sql server 預存程序速度慢的問題

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.