Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the "PARALLEL" equivalent in SQL Server

I have this problem where I need to do a COUNT(COLUMN_NAME) and SUM(COLUMN_NAME) on a few of the tables. The issue is the time it's taking forever on SQL Server to do this.

We have over 2 billion records for which I need to perform these operations.

In Oracle, we can force a parallel execution for a single query/session by using a PARALLEL hint. For example for a simple SELECT COUNT, we can do

SELECT /*+ PARALLEL */ COUNT(1)  
FROM USER.TABLE_NAME;

I searched if there is something available for SQL Server and I couldn't comeup with something concrete where I can specify a table hint for a parallel execution. I believe, SQL Server decides for itself whether to do a parallel or sequential execution depending on the query cost.

The same query in Oracle with a parallel hint takes 2-3 mins to perform whereas on SQL Server it takes about an hour and half.

like image 982
Navyseal Avatar asked Apr 24 '15 09:04

Navyseal


People also ask

What is parallelism in SQL Server?

When an instance of SQL Server runs on a computer that has more than one microprocessor or CPU, the Database Engine detects whether parallelism can be used. The degree of parallelism sets the number of processors employed to run a single statement, for each parallel plan execution.

Does SQL Server have the equivalent of dual?

What is the equivalent of DUAL in SQL Server? None. There is no need of Dual table in SQL Server at all.

Does SQL run in parallel?

The queries run in parallel, as far as possible. The database uses different locks for read and write, on rows, blocks or whole tables, depending on what you do. If one query only reads from a table, another query can also read from the same table at the same time.

Is parallelism good in SQL Server?

SQL Server Degree of Parallelism is especially helpful to the Database Warehouse query engine, Telecommunication database, and partitioned table-based database. The Degree of Parallelism parameter should be configured with small computation and data usage analysis.


1 Answers

I am reading the article Forcing a Parallel Query Execution Plan . For me it looks like you could for testing purpose force a Parallel execution. The author says in the conclution:

Conclusion

Even experts with decades of SQL Server experience and detailed internal knowledge will want to be careful with this trace flag. I cannot recommend you use it directly in production unless advised by Microsoft, but you might like to use it on a test system as an extreme last resort, perhaps to generate a plan guide or USE PLAN hint for use in production (after careful review).

This is an arguably lower risk strategy, but bear in mind that the parallel plans produced under this trace flag are not guaranteed to be ones the optimizer would normally consider. If you can improve the quality of information provided to the optimizer instead to get a parallel plan, go that way :)

The article is refering to a Trace Flag:

There’s always a Trace Flag

In the meantime, there is a workaround. It’s not perfect (and most certainly a choice of very last resort) but there is an undocumented (and unsupported) trace flag that effectively lowers the cost threshold to zero for a particular query

So as far my understanding of this article you could do something like this:

SELECT  
   COUNT(1)  
FROM 
   USER.TABLE_NAME
OPTION (RECOMPILE, QUERYTRACEON 8649)
like image 193
Arion Avatar answered Oct 04 '22 20:10

Arion