简体   繁体   English

从内存优化表插入物理表

[英]Insert from memory optimized table to physical table

Imagine this scenario in SQL Server 2016: we have to tables A and B 想象一下在SQL Server 2016中的这种情况:我们必须对表A和B

  • A is a memory optimized table A是内存优化表
  • B is a normal table B是一张普通桌子

We join A and B, and nothing happens and 1000 rows are returned in min time. 我们加入A和B,什么也没有发生,并且在最短时间内返回了1000行。 But when we want to insert this result set into another table (memory optimized table OR normal table or even a temp table), it takes 10 to 20 seconds to insert. 但是,当我们要将结果集插入到另一个表(内存优化表或普通表,甚至是临时表)中时,则需要1020秒的时间插入。

Any ideas? 有任何想法吗?

UPDATE : Execution plans for normal scenario and memory optimized table added 更新:正常情况下的执行计划和内存优化表已添加 使用内存优化表 带普通桌子

When a DML statement targets a Memory-Optimized table, the query cannot run in parallel, and the server will employ a serialized plan. 当DML语句以“内存优化”表为目标时,查询不能并行运行,并且服务器将采用序列化计划。 So, your first statement runs in a single-core mode. 因此,您的第一条语句以单核模式运行。

In the second instance, the DML statement leverages the fact that "SELECT INTO / FROM" is parallelizable. 在第二种情况下,DML语句利用了“ SELECT INTO / FROM”是可并行化的事实。 This behavior was added in SQL Server 2014. Thus, you get a parallel plan for that. SQL Server 2014中已添加了此行为。因此,您将为此制定并行计划。 Here is some information about this: 这是有关此的一些信息:

Reference: What's New (Database Engine) - SQL Server 2014 参考: 新增功能(数据库引擎)-SQL Server 2014

I have run into this problem countless times with Memory-Optimized targets. 对于内存优化目标,我无数次遇到了这个问题。 One solution I have found, if the I/O requirements are high on the retrieval, is to stage the result of the SELECT statement into a temporary table or other intermediate location, then insert from there into the Memory-Optimized table. 我发现的一个解决方案是,如果对检索的I / O要求很高,则将SELECT语句的结果暂存到临时表或其他中间位置,然后从那里插入到Memory-Optimized表中。

The third issue is that, by default, statements that merely read from a Memory-Optimized table, even if that table is not the target of DML, are also run in serialized fashion. 第三个问题是,默认情况下,即使仅从内存优化表中读取的语句(即使该表不是DML的目标)也以序列化方式运行。 There is a hotfix for this, which you can enable with a query hint. 对此有一个修补程序,您可以通过查询提示启用该修补程序。

The hint is used like this: OPTION(HINT USE ('ENABLE_QUERY_OPTIMIZER_HOTFIXES')) 提示的用法如下: OPTION(HINT USE ('ENABLE_QUERY_OPTIMIZER_HOTFIXES'))

Reference: Update enables DML query plan to scan query memory-optimized tables in parallel in SQL Server 2016 参考: Update使DML查询计划能够在SQL Server 2016中并行扫描查询内存优化的表

In either case, any DML that has a memory-optimized table as a target is going to run on a single core. 无论哪种情况,任何以内存优化表为目标的DML都将在单个内核上运行。 This is by design. 这是设计使然。 If you need to exploit parallelism, you cannot do it if the Memory-Optimized table is the target of the statement. 如果需要利用并行性,那么如果“内存优化”表是该语句的目标,则无法做到这一点。 You will need to benchmark different approaches to find the one that performs best for your scenario. 您将需要对不同的方法进行基准测试,以找到最适合您的方案的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM