Forum Discussion
does parsing occur each time the same query is executed in sql server?
hi, thanks for your response, but I'm not interested in plan caching. I'm interested in the parsing phase of query execution. Trying to understand if a given query is parsed each time the query is executed. For example, timings for my query show as follows during the first execution of the query.
SQL Server parse and compile time:
CPU time = 313 ms, elapsed time = 414 ms.
When I execute the query a second time in a row (only a few secs apart), the time shows as follows.
SQL Server parse and compile time:
CPU time = 0 ms, elapsed time = 3 ms.
Third time shows as follows.
SQL Server parse and compile time:
CPU time = 0 ms, elapsed time = 0 ms.
To reiterate, do queries get parsed each and every time they are executed?
SQL Server parse and compile time:
arizona95 , guess why it's the "parse and compile" time.
On first execute the query gets first parsed, the compiled and the result = execution plan is placed in the plan cache.
On second execution SQL Server lookup plan cache, finds the query = no need to parse/compile again, just reuse the plan.
Easy test: Exec query, run
DBCC FREEPROCCACHE (Transact-SQL) - SQL Server | Microsoft Learn
it removes the plan and on next exec it has to parse&compile again.