SQL Server缓存存储过程

时间:2014-01-18 09:45:43

标签: vb.net tsql sql-server-2005 stored-procedures

我的存储过程存在问题......它比SQL Server 2005中的SQL执行速度慢。

这是我的存储过程代码:

ALTER PROCEDURE [dbo].[spCashBankView]
    @ssql as varchar(15), 
    @from as smalldatetime, 
    @until as smalldatetime, 
    @filter as varchar(250) 
AS
BEGIN           
   select 
      h.code [Kode], h.transDate [Tanggal], 
      isnull(a.description,h.paymentFrom)  [Awal],  
      (case left(h.givenTo, 2) 
          when 'SP' then s.name 
          when 'PT' then p.name 
          when 'CS' then c.name  
          else h.givenTo 
       end) [Akhir],  
      sum(d.amount) as amount,
      h.paymentCode [Transaksi], 
      isnull(b.name,'') [Bank],
      h.refNo [No.Reff], h.description  
   from 
      tbCashBankHeader h  
   left join 
      tbBank b on h.paymentType = b.code  
   left join 
      tbsupplier s on h.givenTo = s.code   
   left join 
      tbPeternak p on h.givenTo = p.code  
   left join 
      tbCustomer c on h.givenTo = c.code  
   left join 
      tbAccount a on h.paymentFrom = a.number   
   left join 
      tbCashBankDetail d on h.code = d.code  
   where 
      h.status <> 'DEL' 
      and left(h.code, 2) = @ssql 
      and h.transDate >= @from and h.transDate <= @until   
      and (h.code like + '%' + @filter + '%' or a.description like + '%' + @filter + '%' or h.description like + '%' + @filter + '%' or s.name like + '%' + @filter + '%' or p.name like + '%' + @filter + '%' or c.name like + '%' + @filter + '%' or h.givenTo like + '%' + @filter + '%' or b.name like + '%' + @filter + '%')  
   group by 
      h.code, h.transdate, a.description, h.paymentfrom,  
      (case left(h.givenTo,2) when 'SP' then s.name when 'PT' then p.name when 'CS' then c.name  else h.givenTo end),  
      h.paymentCode, b.name, h.description, h.refNo    
   order by 
      h.transdate, h.kode
END

我使用此代码从VB.NET执行

data = getDataTable("exec spCashBankView @ssql,@from,@until,@filter", _
                    "@ssql", SqlDbType.VarChar, ssql, _
                    "@from", SqlDbType.DateTime, dtFrom.Value, _
                    "@until", SqlDbType.DateTime, dtUntil.Value, _
                    "@filter", SqlDbType.VarChar, Trim(txFilter.Text))

功能

Function getDataTable(ByVal ssql As String, ByVal ParamArray args() As Object)
        Try
            flagQuery = True
            data = New DataTable
            data.Reset()
            con.Open()
            cmd.Connection = con
            cmd.CommandText = ssql
            cmd.Parameters.Clear()

            For i = LBound(args) To UBound(args) Step 3
                cmd.Parameters.Add(args(i), args(i + 1), 200).Value = args(i + 2)
            Next

            adapter = New SqlDataAdapter(cmd)
            adapter.Fill(data)
        Catch ex As Exception
            MsgBox("Send Query:" & Err.Description, , "Fill Xtra Grid")
            flagQuery = False
        Finally
            con.Close()
        End Try
        Return data
    End Function

我正在使用DEV Express的XtraGridview express。

问题是:当我执行该存储过程并且结果只有448行时,VB.NET Xtragridview会很慢并且冻结大约5秒或更长时间来显示结果。

但是当我从SQL Server执行它时,它只需要1秒钟

enter image description here

感谢阅读和回答我的问题.... GBU

2 个答案:

答案 0 :(得分:0)

您也可以尝试在存储过程中的BEGIN之后添加它:

SET NOCOUNT ON;
SET ARITHABORT ON;

这些在SSMS中默认为ON,但在执行SP时则不行。

答案 1 :(得分:0)

我尝试“RECOMPILE”并且正在工作.....
VB.NET getdatatable运行速度比以前快.... 我是从Stored Procedure runs fast after recompile

得到的

=解决=