0

I am developing a web page for search. I have two options to build this page

  1. To build the query on page according to parameter enter by the user and send it to server.

  2. To send parameters to stored procedure and build query there and get it executed.

I want to know that which approach I should adopt and why.

I want to know the advantages or disadvantages of both approaches.

Azhar
  • 20,500
  • 38
  • 146
  • 211
  • possible duplicate of [Ad hoc queries vs stored procedures vs Dynamic SQL](http://stackoverflow.com/questions/2934634/ad-hoc-queries-vs-stored-procedures-vs-dynamic-sql) – Earlz Feb 09 '11 at 05:10
  • You can take idea from following links: http://stackoverflow.com/questions/2934634/ad-hoc-queries-vs-stored-procedures-vs-dynamic-sql http://www.simple-talk.com/sql/t-sql-programming/to-sp-or-not-to-sp-in-sql-server/ http://www.techrepublic.com/article/determine-when-to-use-stored-procedures-vs-sql-in-the-code/5766837 – Brij Feb 09 '11 at 05:06

8 Answers8

1

From my side store procedure will be good.

  1. Stored procedure will not compile again and again where simple Query compile every time then execute.

  2. Stored procedure execute server side that's why it reduces network traffic. SQL query also executes on server also but if you have big query then it will take more time comparison to Stored Procedure to traverse from client side to server.

ashutosh raina
  • 9,228
  • 12
  • 44
  • 80
0

by using SQL queries we actually exposing our database design schema(design) in the code which may be changed. so we use stored procedure which are pre-compiled execute-able object which can contain one or more sql statements. Hence stored procedures are the replica of the complex SQL statements. A stored procedure may be written to accept inputs and return output.

user2147280
  • 29
  • 1
  • 1
  • 3
0

If the query is going to be used only sparingly, adhoc query may be an option. On almost all other occasions it is good to go by stored procedures.

Also consider setting optimize for ad hoc workloads option to 1. Following is from msdn

The optimize for ad hoc workloads option is used to improve the efficiency of the plan cache for workloads that contain many single use ad hoc batches. When this option is set to 1, the Database Engine stores a small compiled plan stub in the plan cache when a batch is compiled for the first time, instead of the full compiled plan. This helps to relieve memory pressure by not allowing the plan cache to become filled with compiled plans that are not reused.

References:

  1. optimize for ad hoc workloads Server Configuration Option
  2. Why would I NOT use the SQL Server option “optimize for ad hoc workloads”?
Community
  • 1
  • 1
LCJ
  • 22,196
  • 67
  • 260
  • 418
0

I'm not a database expert by any means, but my experience is that stored procedures are good for encapsulating fixed queries that don't change over long periods. But it's harder to maintain, at least it was back in the day when I was doing it, because there was no way to keep the stored procedures under revision control and do something like an rsync to the server. Therefore it was easier to maintain just building queries on the fly, as I could make all necessary changes in a single script, or a single group of scripts under revision control. Perhaps things have changed in this regard, and stored procedures can be better integrated with your codebase; in that case, stored procedures might make your code more manageable and maintainable.

jcomeau_ictx
  • 37,688
  • 6
  • 92
  • 107
0

Use stored procedures. Not becaue they are faster, but because they enforce a contract with the database (Give the stored procede these parameters and it will return the data).

Let the app decide what to do with the data, like display it in the UI. Let the stored procedure decide how to insert/update/delete/retrieve the data.

It is also much easier to resuse the queries/logic in stored procedures across multiple front ends.

Scott Bruns
  • 1,971
  • 12
  • 12
0

By passing query from front end the chances of sql injection increases . Store prcedure is pre compiled while query first compiled than execute so chances of syntax error increases

SMK
  • 2,098
  • 2
  • 13
  • 21
0

I adopt stored procedure approach as its easy to maintain and easy to trouble shoot.

Azhar
  • 20,500
  • 38
  • 146
  • 211
-1

Using a stored procedure is always better.

1) If your query is long it will consume your network resources. When we send large SQL queries over a network connection It will increase network traffic.

2)Stored procedures are pre compiled and stored hence they are faster.

3) It is also easy to manage when you want to modify the query.

Coder
  • 1
  • 3
    1) yes, true; 2) no sorry, that's an urban myth: once an ad-hoc query is parsed and a query plan has been determined, that query plan is cached and reused and subsequent calls to that ad-hoc query are **just as fast** as calling a stored procedure – marc_s Feb 09 '11 at 05:39
  • 1
    1) A query would have to be pretty freaking long to make this a factor. 2) That is a myth (http://www.scarydba.com/2009/09/30/pre-compiled-stored-procedures-fact-or-myth/). 3) That is very subjective. – JohnFx Feb 09 '11 at 05:40