Which is faster simple query Or Sql stored Procedure

Posted by Ermahesh2009 under ASP.NET on 7/6/2012 | Points: 10 | Views : 25917 | Status : [Member] | Replies : 8
Dear All

In my new project i want to write query and execute instead of making stored procedure.
my goal is to fill combo box with key_value pair so that's a simple select query
like in country combo box my query is
<CODE>
select country_id,country_name
from countryMaster
</CODE>
so for this purpose which is better for fast execution is procedure or direct query execution .
thanks in advance




Responses

Posted by: Kamalakanta.Nayak09 on: 7/6/2012 [Member] Starter | Points: 25

Up
0
Down
store procedure will run faster

Thanks,
K.K

Ermahesh2009, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Johnbhatt on: 7/6/2012 [Member] Starter | Points: 25

Up
0
Down
Stored Procedure is Command that is stored in Database Directly, Time Taken in case of Simple Text Query, you will send command text from Application to Database+Execution Time+Return time. But In case of Stored Procedure Time Taken for Execution+Return Time. So these Are Fast.

But When you Delete Database, and Create a new your Stored Procedures will be Deleted.

John Bhatt
Glad to Know, Free to Share.....
http://www.johnbhatt.com

Ermahesh2009, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Patel28rajendra on: 7/6/2012 [Member] Starter | Points: 25

Up
0
Down
Hi

Answer to your question is Store Procedure Because store procedure has already been executed Where as Query first compile and then execute at particular time So Store Procedure is Faster Than Query.

Regards


R D Patel

Ermahesh2009, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ajay.Kalol on: 7/6/2012 [Member] Starter | Points: 25

Up
0
Down
Store Procedure

Ajay
ajaypatelfromsanthal.blogspot.in

Ermahesh2009, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 7/6/2012 [Member] Bronze | Points: 25

Up
0
Down
Always stored procedure is faster than general query.


Thanks and Regards
Akiii


Ermahesh2009, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Muralidosscm on: 7/6/2012 [Member] Starter | Points: 25

Up
0
Down
Stored procedure is faster than simple query

Reasons for using stored procedures:

Reduce network traffic -- you have to send the SQL statement across the network. With sprocs, you can execute SQL in batches, which is also more efficient.
Caching query plan -- the first time the sproc is executed, SQL Server creates an execution plan, which is cached for reuse. This is particularly performant for small queries run frequently.
Ability to use output parameters -- if you send inline SQL that returns one row, you can only get back a recordset. With sprocs you can get them back as output parameters, which is considerably faster.
Permissions -- when you send inline SQL, you have to grant permissions on the table(s) to the user, which is granting much more access than merely granting permission to execute a sproc
Separation of logic -- remove the SQL-generating code and segregate it in the database.
Ability to edit without recompiling -- this can be controversial. You can edit the SQL in a sproc without having to recompile the application.
Find where a table is used -- with sprocs, if you want to find all SQL statements referencing a particular table, you can export the sproc code and search it. This is much easier than trying to find it in code.
Optimization -- It's easier for a DBA to optimize the SQL and tune the database when sprocs are used. It's easier to find missing indexes and such.
SQL injection attacks -- properly written inline SQL can defend against attacks, but sprocs are better for this protection.

Regards
Muralidoss M

Ermahesh2009, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Mallesh on: 7/6/2012 [Member] Starter | Points: 25

Up
0
Down
Hi friend,
I want to tell something about your question in my point of view as in three aspects.

1. In Maintainability Purpose:
Someone who specializes in database programming may create stored procedures. This allows the application developer to concentrate on the code instead of SQL code. You can modify stored procedures independently of the program source code, the application doesn't have to be recompiled when/if the SQL is altered. If the operation requires a large amount of SQL code or is performed repetitively, stored procedures can be faster than batches of SQL code. According to web application span, stored procedures should be better to integrated with native code in more manageable and maintainable way of manner. But a single instance(line) of query you need not apply any procedures.

2. In Network (Connection) Purpose:
Store procedure execute server side that's why it reduce network traffic. SQL query also execute on server also but if you have big query then it will take more time comparison to Procedure to traverse from client side to server.If you have a slow connection to the sql server, and large queries, you would probably be better off using stored procedures, as they generate less network traffic, and usually less roundtrips between the server and the client.

3. In Execution (Speed) Purpose:
Each time a SQL statement from the client is run, server compiles, optimizes, and executes the statement. Where as stored procedure is compiled when it is submitted for the first time and this compiled content is stored in something called procedure cache, for subsequent calls. Then no compilation is requires that execute the same batch of sql code. Means, the stored procedures are compiled only once, unless specified, and the execution plan is stored, so a restart of the server wouldn't loose it, just execute & getting better performance than query.

4. And, you can grant user permission to execute a stored procedure even if they don't have permission to execute the procedure's statements directly. This provides better security and performance to the application.

I hope you can understand that...!
According to your requirement choose your weapon.

Happy Coding... !
--Malleswara Rao. ?(..)?

== Mallesh

Ermahesh2009, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bhupentiwari on: 7/9/2012 [Member] Starter | Points: 25

Up
0
Down
Offcourse SP.. beacuse it is pre compiled query

Thanks n Regards
Bhupendra Tiwari

Ermahesh2009, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response