Makes sense to call stored procedure as prepared statement? - sql-server

I have SQL Server stored procedure to insert or update some data (do some logic). I want to call it from .net application multiple times to insert/update many rows.
Does it make sense to call stored procedure as prepared statement, when stored procedure is already compiled and expecting just parameters?

Any stored procedure, like a parametrized query, too, will have to be parsed once before it's executed by SQL Server.
During that parsing, an execution plan is determined and stored in the plan cache of SQL Server, and any subsequent calls to that stored procedure (or parametrized query) will reuse that cached execution plan. That cached execution plan stays in memory until SQL Server restarts, or until the cache has to be cleared out due to memory pressure.
So basically - there's no way around the fact that the stored procedure (or parametrized query) will have to be parsed once (to determine the execution plan) - once that's done, the execution plan is in memory and can be reused. This also means: the first call to a stored procedure (since the last SQL Server restart) will always be a bit slower - after all, more work needs to be performed - and any subsequent calls should be noticeably faster.

Related

Sql server performance changes after recreating the procedure

We have a main stored procedure that returns around 1000 records, changes by the user permissions.
Lately the procedure performance became very bad - but only from the web-service - more than a minute!
but when running the same SP with the same parameters from ssms took only 3 seconds!!
When I tried to check the problem I added writes to log table, and immediately this change improved the performance again to 3 seconds from the web-service.
This is a mystery for me:
1. The difference between running from web-service and ssms
2. The change after adding the logging
Your issue is called parameter sniffing. There were 2 execution plans for this procedure, one created the first time you launched it from web server and another created when you lanuched it from SSMS. And the parameters of these two plans were different. The next time you execute this proc, one of this plans is used: when you execute from SSMS, the second plan is used, and from web service the first plan is used. The parameters passed to this proc were atypical when executed from wb service, and typical when executed from SSMS.
When you altered your procedure, those 2 plans were invalidated as the procedure has changed, then the new execution plan was built for SSMS and for web service, this times both plans were made for the same or similar paremeters.
If you could extract old plans from plan cache you'd see they were different and the parameters sniffed also were different while now the plans are the same and parameter sniffed are the same or similar.
Here you can read more on parameter sniffing: Slow in the Application, Fast in SSMS?
Understanding Performance Mysteries
Please do not use functions on TOP, on recordset, and, on WHERE/JOIN clauses.
When youre calling SP from SSMS, server optimizes it. But, when calling from frontend, it is huge problem. So, eliminate functions, if possible.
If you want to view about what im talking, please start profiler and then log RPC starting/completed, sql statament events. Call quantity is same, as recordset. So, assume, youre calling procedure 1000 times when usig FN on statement , returning recordset.

Why is sql stored procedure called a stored procedure

A few days ago I was asked this question(tel intvw) and I was drawn blank. I said execution plan is stored in the server so its called STORED Procedure. But I am not sure I was correct.
My research after that has shown that there is plan cache or procedure cache inside of SQL Server that's dedicated to storing execution plans. In that article there is also reference to what is called compiled plan stub. So it appears that Compiled Plan Stub is first created and then execution plan is created.
So what I wanted to know is briefly
What are the steps that happen when I create a stored procedure?
Why is a Stored Procedure called stored procedure(if the question makes sense)?
If you can refer me to an existing SO question or some other article, that should also be fine.
Why is a Stored Procedure called stored procedure
Because it is a procedure that is stored in a database.
In other languages/environments, procedures that can be executed aren't usually stored. They are compiled in either bytecode or assembler. I.e. the procedure does not exist in its original textual form anymore. The original procedure cannot be retrieved as it was when it was created in those environments (although reverse engineering can retrieve the essence of that procedure).
When you create a stored procedure in SQL Server, it is completely stored in its original full-text form, same indentation, same casing, same lines, including comments and all. You can retrieve the text with which you created the stored procedure in its entirety.
Simplified explanation about executing a stored procedure
When SQL Server wants to execute a stored procedure, it will first check the cache to see if it has been compiled already. If it finds an entry in the cache (in the form of an execution plan) it will use this entry to execute. If it doesn't find an entry it will compile the procedure into an execution plan, store it in the cache for later use, then execute it using the execution plan.
There are cases that force a stored procedure to be recompiled, e.g. when the execution plan cache is cleared (schema changes, statistics updates, ...) or when supplying commands to the compiler that force recompilation (stored procedure WITH RECOMPILE, query option OPTION(RECOMPILE), ...).
I said execution plan is stored in the server so its called STORED Procedure.
Wrong. Execution plans also are stored on the server for dynamic SQL. I have no real idea why it is called as stored procedure, but the procedure as a whole is stored on the server (code etc.). I can assume this is the reason - but the execution plan (cache) is irrelevant here, because all execution plans are possibly stored there.
For 1: what do you care? Obviously a SQL Statement is executed. There is some parsing, to make sure it is valid. The rest is an implementation detail - and may vary even between versions. I would assume a SP is stored in some level of bytecode - but again, who cares? I do database level development for 25 years and that never even came into my consideration.

SQL Server procedure recompile

I have a question: let's say I have a procedure which contains dynamic SQL inside the definition of procedure, so when I execute the procedure for the first time, it's obvious that it compiles the procedure and stores the plan for first time.
What happens during the second run? Will the same plan be used or will the procedure go for a recompile as it contains dynamic SQL in it?
Dynamic SQL is always compiled. It may result in the same execution plan as the first run (totally dependent on parameters).
I would suggest reading this article from MS. Relevant quotes:
Recompilations: Definition
Before a query, batch, stored procedure, trigger, prepared statement, or dynamic SQL statement (henceforth, "batch") begins execution on a SQL Server, the batch gets compiled into a plan. The plan is then executed for its effects or to produce results.
and
Compiled plans are stored into a part of SQL Server's memory called plan cache . Plan cache is searched for possible plan reuse opportunities. If a plan reuse for a batch happens, its compilation costs are avoided.
A similar question has already been answered in Stack Exchange for Database Administrators. Please refer: https://dba.stackexchange.com/questions/47283/when-does-sp-executesql-refresh-the-query-plan

Where are stored procedures stored at? Does it create an execution plan when you create it?

Where is the execution plan for a stored procedure stored at? When you create a stored procedure does it create an execution plan then or do you have to run it once before anything is created?
The execution plan is created the first time the stored procedure is run - and it's stored in the (volatile) plan cache.
If the server is too busy and needs space for more recent execution plans to be cached, or if the SQL Server service is shut down, that cache goes away and the next time around, the procedure needs to be parsed and an execution plan determined again.
Just by creating the stored procedure, you are not storing any execution plan - the stored procedure aren't pre-compiled or anything like that, as folklore often claims. That is just simply not the case.
As a matter of fact, SQL Server doesn't even check for object existence at the time when you create the stored procedure. You can totally create a stored procedure that select from a non-existing table - and it will be created just fine. The error only occurs at runtime - once the execution plan is attempted to be constructed for the first time (and at that point, of course, the fact that the table isn't present, will cause an error)
The stored procedure body is stores in a system table.
The plan is cached and reused every time it is executed, Though yes, first it has to be created at first execution.

Strange problem with SQL Server procedure execution plan

I was wondering if you guys could help me get to the bottom of a weird problem I have recently had on SQL Server.
I have a stored procedure (lets call SPold) which is reasonably large with a lot of calculations (can't possibly do this in app as info for around 6000 users needs to come back in a one-er (I reduce this to 1000 based on Surname)). The stored procedure usually executes in a couple of seconds, and is called once every couple of minutes.
Now this morning, the stored procedure was suddenly taking 4-10 times as long to execute, causing a number of timeouts. I discovered that by making a copy of the procedure with a new name (SPnew) and executing, I would get the fast execution times again. This indicated to me that the execution plan was the problem with the original, SPold, so I decided to execute it with recompile. This would return the results a quicker (although not as fast as SPnew), but subsequent calls from users to SPold were once again slow. It was like the new plan wasn't being kept.
What I have done is to fix this is put Exec SPnew into SPold, and now calls to SPold are returning fast again.
Does anyone have any idea what is going on here? The only thing that updated overnight was the statistics, although I think that this should affect both SPold and SPnew.
Sounds like you are experiencing an incorrectly cached query plan due to parameter sniffing.
Can you post the stored procedure?
Batch Compilation, Recompilation, and Plan Caching Issues in SQL Server 2005
I Smell a Parameter!
In SQL Server 2005, you can use the OPTIMIZE FOR query hint for preferred values of parameters to remedy some of the problems associated with parameter sniffing:
OPTIMIZE FOR Instructs the query optimizer to use a particular value for a local
variable when the query is compiled and optimized. The value is used
only during query optimization, and not during query execution.
OPTIMIZE FOR can counteract the parameter detection behavior of the
optimizer or can be used when you create plan guides. For more
information, see Recompiling Stored Procedures and Optimizing Queries
in Deployed Applications by Using Plan Guides.
Although SQL Server 2005 does not support OPTIMIZE FOR UNKNOWN (introduced in SQL Server 2008) which
will eliminate parameter sniffing for a given parameter:
OPTION (OPTIMIZE FOR (#myParam UNKNOWN))
You can achieve the same effect in SQL Server 2005 by copying the parameter into a local variable, and then use the local variable in the query.
I've also encounterred two "strange" cases with Sql Server 2005, which might relate to your problem as well.
In the first case my procedure executed prety fast, when being run as dbo, and it was slow when being run from the application, under a different user account.
In the second case the query plan of the procedure got optimized for the parameter values with which the procedure was called for the first time, and this plan was then reused later for other parameter values as well, resulting in a slow execution.
For this second case the solution was to copy the parameter values into local variables in the procedure, and then using the variables in the queries instead of the parameters.

Resources