Thoughts on misuse of Stored Procedures

Source: Internet
Author: User
Tags dotnet
Locally compiling and running stored procedures in the database is obviously the first choice to improve database access efficiency. However, many may misunderstand that stored procedures are high performance, it is believed that the logical operations implemented by the stored procedure are certainly more efficient than the operations performed on the database on the client. However, they forget that the efficiency is largely dependent on the specific implementation. Code . Next, let's make a simple test to prove this.

The data paging function is a frequently discussed topic. In many people's eyes, it is very inefficient to paging data on the DOTNET client, and it is impossible to match the stored procedure; even bad Stored Procedure Code is faster than the DOTNET client. To verify this, a simple DOTNET paging code is compiled and compared with the traditional MSSQL paging storage using a temporary table.

Test Description:

Perform paging operations on the orders table of MSSQL northwind data. The total number of records on the page is 830. Call the relevant paging code 10 times, and execute one page to five pages of the data set for each call.

DOTNETPaging code:

Const string getindex = "select orderid from orders ";

Const string getrecord = "select * from orders where orderid in ({0 })";

Static system. Data. datatable getdatasql (INT page)

{

System. Text. stringbuilder Index = new stringbuilder ();

Sqldataadapter da;

System. Data. dataset DS = new system. Data. dataset ();

Sqlcommand cmd = new sqlcommand ();

Int start, end;

Start = 10 * page;

End = start + 10;

Int cur = 0;

Using (sqlconnection conn = new sqlconnection ("Data Source =.; initial catalog = northwind; user id = sa; Pwd = ;"))

{

Cmd. Connection = conn;

Cmd. commandtext = getindex;

Conn. open ();

Using (sqldatareader reader = cmd. executereader ())

{

While (reader. Read ())

{

 

If (cur> = Start & cur <End)

{

If (index. length> 0)

{

Index. append (",");

}

Index. append ("'" + reader [0]. tostring () + "'");

 

}

If (cur> end)

{

Cmd. Cancel ();

Break;

 

}

Cur ++;

}

 

}

Cmd. commandtext = string. Format (getrecord, index. tostring ());

DA = new sqldataadapter (CMD );

Da. Fill (DS, "Table1 ");

Return Ds. Tables [0];

 

}

 

}

 

Stored Procedure paging code:

Create proc testlist

(

@ Pageindex int,

@ Pagesize int

)

As

Declare @ pagelowerbound int

Declare @ pageupperbound int

Set @ pagelowerbound = @ pagesize * @ pageindex

Set @ pageupperbound = @ pagelowerbound + @ pagesize + 1

 

Create Table # IDS

(

Tempid int identity,

Orderid int not null

)

Insert into # IDS (orderid) Select orderid from orders

Select orders .*

From orders

Inner join # IDs on (orders. [orderid] = # IDs. orderid)

Where # IDs. tempid> @ pagelowerbound

And # IDs. tempid <@ pageupperbound

Go

Static system. Data. datatable getdatasp (INT page)

{

 

Sqldataadapter da;

System. Data. dataset DS = new system. Data. dataset ();

Sqlcommand cmd = new sqlcommand ();

Using (sqlconnection conn = new sqlconnection ("Data Source =.; initial catalog = northwind; user id = sa; Pwd = ;"))

{

Cmd. Connection = conn;

Cmd. commandtext = "testlist ";

Cmd. commandtype = system. Data. commandtype. storedprocedure;

Cmd. Parameters. Add ("@ pageindex", page );

Cmd. Parameters. Add ("@ pagesize", 10 );

Conn. open ();

 

DA = new sqldataadapter (CMD );

Da. Fill (DS, "Table1 ");

Return Ds. Tables [0];

 

}

}

 

Test results:

Stored Procedure page: 74 Ms

DOTNET paging: 64 ms

Stored Procedure page: 21 Ms

DOTNET paging: 10 ms

Stored Procedure pagination: 1023 milliseconds

DOTNET paging: 11 ms

Stored Procedure page: 20 ms

DOTNET paging: 11 ms

Stored Procedure page: 22 Ms

DOTNET paging: 12 ms

Stored Procedure pagination: 1031 milliseconds

DOTNET paging: 10 ms

Stored Procedure page: 20 ms

DOTNET paging: 10 ms

Stored Procedure page: 21 Ms

DOTNET paging: 10 ms

Stored Procedure page: 20 ms

DOTNET paging: 10 ms

Stored Procedure page: 21 Ms

DOTNET paging: 10 ms

What causes efficiency to differ so far, while the inefficiency is the stored procedure rather than the DOTNET code. I believe you should know the reason for taking a look at the processing of the stored procedure.

Download test code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.