Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL execution plan caching

I have a few questions regarding Microsoft SQL Server 2008 performance, mainly about execution plans.

According, to MSDN, stored procedures have better performance compared to direct SQL queries, because:

The database can prepare, optimize, and cache the execution plan so that the execution plan can be reused at a later time.

My first question is why this is the case. I have previously read that when using parameterized queries (prepared statements), the execution plan is cached for subsequent executions with potentially different values (execution context). Would a stored procedure still be more efficient? If so, is a stored procedure's execution plan only recreated on demand, or is it just less likely to be cleared from the cache? Is a parameterized query treated as an ad-hoc query, meaning that the execution plan is more likely to be cleared from the cache?

Also, since I am still a novice in this field, I am wondering if there are certain commands that only work in T-SQL. I have a query that takes ~12 seconds to complete on the first run and then ~3 seconds after that, in both Microsoft SQL Management Studio and ADO.NET. The query is supposed to be ineffective as part of my presentation. The thing is that in my query, I use both CHECKPOINT and DBCC DROPCLEANBUFFERS as per this article and also OPTION (RECOMPILE). However, at least the two first do not seem to make a difference, as the query will still take 3 seconds. My guess would be that it is due to the data cache not being cleared. Any ideas why the cache does not seem to be cleared, or any ideas as to why my query is significantly faster after the first execution?

Those are the questions I could think of for now.

like image 917
ba0708 Avatar asked Jan 24 '12 20:01

ba0708


People also ask

What is cached execution plan in SQL Server?

Applies to: SQL Server (all supported versions) The Plan Cache object provides counters to monitor how SQL Server uses memory to store objects such as stored procedures, ad hoc and prepared Transact-SQL statements, and triggers.

Does SQL Server cache query plan?

Every query requires a query plan before it is actually executed. This query plan is stored in SQL Server query plan cache. This way when that query is run again, SQL Server doesn't need to create another query plan; rather it uses the cached query plan which improved database performance.

Does SQL Server cache views?

Bookmark this question. Show activity on this post. In SQL Server, stored procedures execution plans are cached but view execution plan are never cached.

Does MySQL cache execution plan?

MySQL has no execution plan cache (don't confuse with Query [Result] Cache).


1 Answers

"Would a stored procedure still be more efficient?": Essentially no. It saves very little. From a performance standpoint, you can pretty much use SQL literals in your app (except if they are HUGE). SQL Server will match the string you send to it to a cached plan just fine.

" I have a query that takes ~12 seconds to complete on the first run and then ~3 seconds after " Considering that you cleared all caches, this is probably a statistics issue. SQL Server is auto-creating statistics the first time you access a column. I guess this is what happened once to you. Try running sp_updatestats (before you clear the caches).

like image 91
usr Avatar answered Oct 03 '22 09:10

usr