top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

How sql server know when a query has to compile or not?

+4 votes
98 views
How sql server know when a query has to compile or not?
posted Jun 23, 2015 by Mohammed Hussain

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote

SQL Server has a pool of memory that is used to store both execution plans and data buffers. The percentage of the pool allocated to either execution plans or data buffers fluctuates dynamically, depending on the state of the system. The part of the memory pool that is used to store execution plans is referred to as the procedure cache.
For more information:https://technet.microsoft.com/en-us/library/ms181055%28v=sql.105%29.aspx

answer Jun 26, 2015 by Vrije Mani Upadhyay
0 votes

Answer is pretty simple. Before executing any query first sql server checks the cached plan if it doesn't find the query in it then it compiles the query and adds in cached plan otherwise directly execute the query. Now we are going to check the cached plan. Before this it would be good first empty the cached plan:

--To empty cached Query Plan

DBCC FREEPROCCACHE

Now we are executing the first query:

SELECT DISTINCT MsgId * Severity, Msg 
FROM SqlMessage
WHERE MsgId  > 1
answer Jun 27, 2015 by Rahul Singh
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...