I have a query which was executing slowly earlier. Later I found out that it was not running parallel and this is making the query execution slow.
The query involves a big view
and then querying the view with lots of temp tables
and sub query
.
I removed one UDF
from the view and used inline functions
and also used a scalar TVF
and then it started running fast in parallel execution
.
It was going well for few days and one fine day I noticed that the query ran slowly. So I checked the execution plan and found out that the query is executing in serial mode. I checked the plan cache for the query and I saw lots of cached plans involving that view. I deleted plans which are not parallel and then the query runs quickly.
Now I do this every morning to force the query to run parallel.
Additional Details:
- SQL Server 2016 Standard
- Query gets generated via LINQ-SQL from the dot net application. So ad hoc query.
How can I force the query to run parallel forever?