Forum Discussion
does parsing occur each time the same query is executed in sql server?
hi,
I've got a basic question. Does parsing occur each time a given query is executed in SQL SERVER 2022? My tests show that parsing time shows as 0 ms when executing a query for the second time in a row.
3 Replies
- olafhelperBronze Contributor
arizona95 , your question is vague and may from the terms in the wrong direction.
"Parsing" is to check the SQL syntax for correctness; it's done for objects (views/SP) once on saving and for adhoc queries on every execution.
I guess you mean more compiling to create an execution plan.
Depends on workload/memory if an execution plan is kept in plan cache, see
SQL Server, Plan Cache object - SQL Server | Microsoft Learn
- arizona95Copper Contributor
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?
- olafhelperBronze Contributor
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.