我遇到了这个问题,我需要对一些表执行 COUNT(COLUMN_NAME)
和 SUM(COLUMN_NAME)
操作。问题是在 SQL Server 上执行此操作要花费很长时间。
我们有超过 20 亿条记录,我需要对其执行这些操作。
在 Oracle 中,我们可以使用 PARALLEL
提示强制单个查询/ session 并行执行。例如,对于一个简单的SELECT COUNT
,我们可以这样做
SELECT /*+ PARALLEL */ COUNT(1)
FROM USER.TABLE_NAME;
我搜索了是否有可用于 SQL Server 的东西,但我无法想出具体的东西来指定并行执行的表提示。我相信,SQL Server 根据查询成本自行决定是并行执行还是顺序执行。
在 Oracle 中使用并行提示执行相同的查询需要 2-3 分钟才能执行,而在 SQL Server 上则需要大约一个半小时。
最佳答案
我正在阅读这篇文章Forcing a Parallel Query Execution Plan 。对我来说,您似乎可以出于测试目的强制并行执行。作者在结论中说:
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 :)
本文引用了跟踪标志:
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
据我对这篇文章的理解,你可以这样做:
SELECT
COUNT(1)
FROM
USER.TABLE_NAME
OPTION (RECOMPILE, QUERYTRACEON 8649)
关于sql - SQL Server 中的 "PARALLEL"等效项是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29843813/