Тёмный

Sql server query plan cache 

kudvenkat
Подписаться 829 тыс.
Просмотров 66 тыс.
50% 1

Text version of the video
csharp-video-tutorials.blogspo...
Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our RU-vid channel. Hope you can help.
/ @aarvikitchen5572
Slides
csharp-video-tutorials.blogspo...
All SQL Server Text Articles
csharp-video-tutorials.blogspo...
All SQL Server Slides
csharp-video-tutorials.blogspo...
All SQL Server Tutorial Videos
• SQL Server tutorial fo...
All Dot Net and SQL Server Tutorials in English
ru-vid.com...
All Dot Net and SQL Server Tutorials in Arabic
/ kudvenkatarabic
In this video we will discuss
1. What happens when a query is issued to SQL Server
2. How to check what is in SQL Server plan cache
3. Things to consider to promote query plan reusability
What happens when a query is issued to SQL Server
In SQl Server, every query requires a query plan before it is executed. When you run a query the first time, the query gets compiled and a query plan is generated. This query plan is then saved in sql server query plan cache.
Next time when we run the same query, the cached query plan is reused. This means sql server does not have to create the plan again for that same query. So reusing a query plan can increase the performance.
How long the query plan stays in the plan cache depends on how often the plan is reused besides other factors. The more often the plan is reused the longer it stays in the plan cache.
How to check what is in SQL Server plan cache
SELECT cp.usecounts, cp.cacheobjtype, cp.objtype, st.text, qp.query_plan
FROM sys.dm_exec_cached_plans AS cp
CROSS APPLY sys.dm_exec_sql_text(plan_handle) AS st
CROSS APPLY sys.dm_exec_query_plan(plan_handle) AS qp
ORDER BY cp.usecounts DESC
As you can see we have sorted the result set by usecounts column in descending order, so we can see the most frequently reused query plans on the top.
usecounts - Number of times the plan is reused
objtype - Specifies the type of object
text - Text of the SQL query
query_plan - Query execution plan in XML format
To remove all elements from the plan cache use the following command
DBCC FREEPROCCACHE
In older versions of SQL Server up to SQL Server 6.5 only stored procedure plans are cached. The query plans for Adhoc sql statements or dynamic sql statements are not cached, so they get compiled every time. With SQL Server 7, and later versions the query plans for Adhoc sql statements and dynamic sql statements are also cached.
Things to consider to promote query plan reusability
For example, when we execute the following query the first time. The query is compiled, a plan is created and put in the cache.
Select * From Employees Where FirstName = 'Mark'
When we execute the same query again, it looks up the plan cache, and if a plan is available, it reuses the existing plan instead of creating the plan again which can improve the performance of the query. However, one important thing to keep in mind is that, the cache lookup is by a hash value computed from the query text. If the query text changes even slightly, sql server will not be able to reuse the existing plan.
For example, even if you include an extra space somewhere in the query or you change the case, the query text hash will not match, and sql server will not be able find the plan in cache and ends up compiling the query again and creating a new plan.
Another example : If you want the same query to find an employee whose FirstName is Steve instead of Mark. You would issue the following query
Select * From Employees Where FirstName = 'Steve'
Even in this case, since the query text has changed the hash will not match, and sql server will not be able find the plan in cache and ends up compiling the query again and creating a new plan.
This is why, it is very important to use parameterised queries for sql server to be able to reuse cached query plans. With parameterised queries, sql server will not treat parameter values as part of the query text. So when you change the parameters values, sql server can still reuse the cached query plan.
The following query uses parameters. So even if you change parameter values, the same query plan is reused.
Declare @FirstName nvarchar(50)
Set @FirstName = 'Steve'
Execute sp_executesql N'Select * from Employees where FirstName=@FN', N'@FN nvarchar(50)', @FirstName
Summary: Never ever concatenate user input values with strings to build dynamic sql statements. Always use parameterised queries which not only promotes cached query plans reuse but also prevent sql injection attacks.

Наука

Опубликовано:

 

7 июл 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 31   
@muhammadrehbarsheikh8498
@muhammadrehbarsheikh8498 7 лет назад
Thank you venkat sir. great explanation. seriously, your tutorials are very helpful for us. keep teaching sir.. god bless you.
@thearchibaldtuttle
@thearchibaldtuttle Год назад
Late to the party but the video is Gold!! Thank you!
@ayanvlogs3049
@ayanvlogs3049 6 лет назад
Really nice video and your voice makes to listen carefully. No hurry :)
@rahmiul1980
@rahmiul1980 Год назад
Thank you venkat sir.. god bless you
@daudulislam156
@daudulislam156 4 года назад
Thank you venkat this is great video...!!!
@shoaibakram1730
@shoaibakram1730 6 лет назад
Thank you sir, it's great your teaching attitude is too much nice and helpful for me, please sir upload more videos about Execution plan in detail that would be awesome to understand this topic deeply .
@dharmharley6871
@dharmharley6871 2 года назад
You are a life saver!!!
@mohamadhelaly4979
@mohamadhelaly4979 7 лет назад
really nice lesson you are awesome
@jackbeyant9116
@jackbeyant9116 3 года назад
So Great,Bro!
@KurtMCox
@KurtMCox 5 лет назад
Well done sir!
@martenhc
@martenhc 7 лет назад
Nice one!
@raqibul1000
@raqibul1000 7 лет назад
Thanks a lot.
@madhukarpatil2514
@madhukarpatil2514 6 лет назад
Very Helpful.. Thanks a lot! Do you have any session for tipping point and cardinality estimation..If yes, Please share the link..
@folorunso
@folorunso 7 лет назад
I hope the new series venkat is preparing to start now is xamarin.. that will be awesome lol
@artemboss1999
@artemboss1999 7 лет назад
Could you make a release about the query execution plan in SQL Server??
@ehoque4
@ehoque4 2 года назад
Very important information
@shatabdidey3056
@shatabdidey3056 4 года назад
Good to know that when we use parametrised query , sql server does not treat the parameter values as part of the sql query & hence goes on to use the same plan in cache..instead of making a new one
@rdg515
@rdg515 6 лет назад
At the end of this tutorial if Venkat searched by last name a new query plan would've been created, right?
@raqibul1000
@raqibul1000 7 лет назад
Allah bless you.
@hussainmadar4
@hussainmadar4 4 года назад
@kudvenkat I not getting plan cahed count firing dynamic management functions. can you please suggest?
@rliy001
@rliy001 3 года назад
You could have prevented the quey you use to return what's in the cache from clouding the results set by putting with(recompile) after the tsql. That way it will never get into the cache
@pavlopotapenko2686
@pavlopotapenko2686 3 года назад
thank you! do you have some video about what exactly means Query Plan, how does it work inside SQL Server and how many execution time we can save using plan caching?
@saikiranp.r6966
@saikiranp.r6966 5 лет назад
So the generated query plan is cached untill the server is restarted ??
@daniyalawan7759
@daniyalawan7759 7 лет назад
Please Create a Series on ASP.NET MVC CORE ... Latest .net framework ..
@kakadiyarajesh8134
@kakadiyarajesh8134 7 лет назад
Thank you. What is the use of dynamic SQL.?
@softtech8184
@softtech8184 Год назад
Hi , i need one help in SQL server
@shatabdidey3056
@shatabdidey3056 4 года назад
How to delete the Plan for just one proc..DBCC FREEPROCCACHE(Planhandle)
@realsolo5569
@realsolo5569 2 года назад
mid wives PPD - post partem
@realsolo5569
@realsolo5569 2 года назад
the druids I read it everyone wants to know where they come from girls scouts an irish beauty ? loyal!! it be stankkkkkk have muchehhhhhhhh Bayern München Bayern Munich Bavaria Frank Kujman got fired - Frisco - baby germany!!!
Далее
exec vs sp executesql in sql server
9:16
Просмотров 45 тыс.
SQL Server Execution Plan Basics
17:26
Просмотров 52 тыс.
ПОЮ ЛЕТНИЕ ПЕСНИ🌞
3:16:55
Просмотров 1,7 млн
Lasagna Soup @Lionfield
00:35
Просмотров 7 млн
Stored procedures in sql server   Part 18
20:11
Просмотров 1,5 млн
SQL Server Execution Plans - Part 1
8:31
Просмотров 74 тыс.
Dynamic SQL in SQL Server
12:12
Просмотров 132 тыс.
SQL Server Execution Plan Operators
11:15
Просмотров 22 тыс.
Ad Hoc Query Caching in SQL Server
9:35
Просмотров 666
Игровой Комп с Авито за 4500р
1:00