Multi Thread in SQL?

24,875

If the server computer on which SQL Server is running has multiple CPU's SQL Server can run a single query in parallel using multiple threads. In addition to running user queries on multiple processors SQL Server can also use multiple threads to build indexes. When examining textual or graphical execution plans you will notice exchange operators distribute streams, repartition streams and gather streams if the query is using more than one processor. Typically queries that make heavy use of CPU cycles are good candidates for parallel execution. For example a query joining several large tables and sorting the output before returning it to the user is likely to benefit from a parallel execution plan.

In brief, SQL server itself is a good judge of whether a query can be run in multiple threads or not. Basically query optimizer sees if parts of the query can be run in parallel and takes a call.

If the query contains a scalar operator or relational operators that cannot run in parallel, then it won't be considered for parallel execution. Furthermore, if the number of rows to be operated on is relatively low, query optimizer doesn't consider parallel execution plans.

Going by your words, the function is time consuming. So external select and the function will most definitely be running on different threads. However, without visibility to the ufn_HugeTimeProcessFunction, it will be very difficult to provide a solution to optimize the function for parallel runs. In the absence of that, I would recommend you to have a look at the execution plan for the function and see if you can tweak the query to reduce scalar and relational operations

Quote Source : http://www.toadworld.com/platforms/sql-server/w/wiki/9824.parallel-query-processing.aspx

More info on using parallelism after analyzing execution plans can be found at https://www.simple-talk.com/sql/learn-sql-server/understanding-and-using-parallelism-in-sql-server/

Share:
24,875
vaduganathan
Author by

vaduganathan

Updated on June 26, 2020

Comments

  • vaduganathan
    vaduganathan about 4 years

    I have a SQL query Like

    SELECT Column1, Column2, Column3, **ufn_HugeTimeProcessFunction**(Column1, Column2, @Param1) As Column4
    From Table1
    

    This ufn_HugeTimeProcessFunction function run against large table (in terms of number of rows) and there are several calculation behind to return value.

    Am I able to force the SQL compiler to run that function in another thread (process)?

    Edited : Basically that function get the data from 3 different databases. That's why I am planing to run it "in parallel", moreover it is not possible to change the indexes on the other databases