I did notice that I had made a mistake in my code. I had not performed the
"SET ROWCOUNT 10" command which meant my T-SQL was sending back more records
than the SP. I have modified the code so that the T-SQL does perform the
"SET ROWCOUNT 10" and received the following results:
T-SQL: 490.686 ms
SP: 140.196 ms
I would like to point out that your code is really only comparing T-SQL with
T-SQL that calls a stored procedure. Your SQL query "EXEC ""Ten Most
Expensive Products""" is still T-SQL and must be interpreted by the engine
before it can be run. Once interpreted, it then executes the SP which is
already compiled.
Using ADO, a more accurate test would be to use the Command object and set
its CommandType to adCmdStoreProc. The code would look like this:
set cmd = server.createobject("adodb.Command")
set cmd.ActiveConnection = conn
cmd.CommandText = "Ten Most Expensive Products"
cmd.CommandType = adCmdStoreProc
set rs = cmd.Execute
I ran my test in a console app in C#, and I'm including the code below:
using System;
using System.Data;
using System.Data.SqlClient;
namespace testSpeed2
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
SqlConnection conn = new
SqlConnection("server=dev1;database=Northwind;integrated security=sspi");
Console.WriteLine("Starting T-SQL Profile....");
DateTime dt1 = DateTime.Now;
for(int i=0; i < 100; i++)
{
conn = new SqlConnection("server=dev1;database=Northwind;integrated
security=sspi");
using (conn)
{
conn.Open();
SqlCommand cmd = new SqlCommand("SET ROWCOUNT 10\nSELECT
Products.ProductName AS TenMostExpensiveProducts, Products.UnitPrice FROM
Products ORDER BY Products.UnitPrice DESC", conn);
SqlDataReader dr = cmd.ExecuteReader();
while(dr.Read())
{
}
}
}
DateTime dt2 = DateTime.Now;
TimeSpan ts = dt2.Subtract(dt1);
Console.WriteLine( ts.TotalMilliseconds.ToString() + " ms");
Console.WriteLine("Starting SP Profile....");
dt1 = DateTime.Now;
for(int i=0; i < 100; i++)
{
conn = new SqlConnection("server=dev1;database=Northwind;integrated
security=sspi");
using (conn)
{
conn.Open();
SqlCommand cmd = new SqlCommand("Ten Most Expensive Products", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataReader dr = cmd.ExecuteReader();
while(dr.Read())
{
}
}
}
dt2 = DateTime.Now;
ts = dt2.Subtract(dt1);
Console.WriteLine( ts.TotalMilliseconds.ToString() + " ms");
Console.Read();
}
}
}
--
Ben Lucas
Lead Developer
Solien Technology, Inc.
www.solien.com [quoted text, click to view] "Mary Kerrigan" <mkerrigan@ktoys.com> wrote in message
news:652faee5.0408031122.793a04bd@posting.google.com...
> I did the same thing (in ASP) but got these results:
>
> T-SQL: 301 ms
> SP: 390 ms
>
> Here is the code I used:
>
> <script language="javascript" runat="server">
>
> function GetTimeInMillisec()
> {
> var Now = new Date()
> var TimeStamp = Now.getTime()
>
> return TimeStamp
> }
>
> </script>
>
> <%
> myDSN="DSN=Northwind;uid=sa;pwd="
> set conn=server.createobject("adodb.connection")
> conn.open myDSN
>
> 'statement for stored proc
> mySQL1 = "EXEC ""Ten Most Expensive Products"""
>
> 'statement for inline SQL
> mySQL2 = "SET ROWCOUNT 10 " _
> & "SELECT Products.ProductName AS TenMostExpensiveProducts,
Products.UnitPrice " _
> & "FROM Products " _
> & "ORDER BY Products.UnitPrice DESC "
>
> sp_starttime = GetTimeInMillisec()
> for i = 1 to 100
> Response.Write i
> set rs=conn.execute(mySQL1)
> next
> sp_endtime = GetTimeInMillisec()
>
> il_starttime = GetTimeInMillisec()
> for i = 1 to 100
> Response.Write i
> set rs=conn.execute(mySQL2)
> next
> il_endtime = GetTimeInMillisec()
>
> sp_time = sp_endtime - sp_starttime
> il_time = il_endtime - il_starttime
>
> Response.Write "sp_time: " & sp_time & CHR(10) & CHR(13)
> Response.Write "il_time: " & il_time & CHR(10) & CHR(13)
>
>
> conn.Close
> set conn=nothing
> set rs=nothing
> %>
>
>
>
>
> "Ben Lucas" <ben@nospam.solien.nospam.com> wrote in message
news:<sr2dnc1Ga_F845PcRVn-oQ@comcast.com>...
> > In the case of simple statements, those statements still have to be
> > interpreted and compiled, whereas Stored Procedures do not. The
performance
> > advantage may be very small, but the Stored Procedure should still
perform
> > better than the statement.
> >
> > To do a test, I wrote a program that called the "Ten Most Expensive
> > Products" Stored Procedure in the Northwind database and iterated 100
times
> > on that. It also called the equivalent in T-SQL and iterated 100 times
on
> > that. The results:
> >
> > T-SQL: 1552.1855 ms
> > SP: 150.2115 ms.
> >
> > There is definitely a performance advantage to using SPs.
> >
> > --
> > Ben Lucas
> > Lead Developer
> > Solien Technology, Inc.
> >
www.solien.com > >
> > "Mary Kerrigan" <mkerrigan@ktoys.com> wrote in message
> > news:652faee5.0408020634.563e6359@posting.google.com...
> > > I understand that. But in the case of a simple statement like "DELETE
> > > FROM FOO" does it make a real performance difference? Likewise, with
> > > a simple INSERT statement?
> > >
> > > "Ben Lucas" <ben@nospam.solien.nospam.com> wrote in message
> > news:<3ZCdnVbFTfy5Epfc4p2dnA@comcast.com>...
> > > > Yes, there is a performance advantage. Stored Procedures are
already
> > > > compiled and optimized when they are created whereas T-SQL
statements
> > must
> > > > be compiled and optimized every time they are run.
> > > >
> > > > --
> > > > Ben Lucas
> > > > Lead Developer
> > > > Solien Technology, Inc.
> > > >
www.solien.com > > > >
> > > > "Mary Kerrigan" <mkerrigan@ktoys.com> wrote in message
> > > > news:652faee5.0407300731.b4311ac@posting.google.com...
> > > > > Is there any advantage to moving simple INSERT, UPDATE, or DELETE
> > > > > statements to stored procedures rather than just putting them in
the
> > > > > application code? Is it better performance-wise or does it make a
> > > > > difference? The application in question is a ColdFusion Web
> > > > > application.