为什么我的内存优化表变量使用磁盘空间

时间:2018-03-26 20:08:08

标签: sql-server-2016 table-variable in-memory-tables

我在我的数据库中创建了一个内存优化表类型,如下所示:

create type tblLocation_InMem as table(
     IndexLocation int not null index IX_LocInMem,
     IndexRootLocation int not null,
     fldLocCrumbs nvarchar(1024) not null
)
with (memory_optimized=on)

此表类型将用于需要通过多个select语句访问存储在其中的数据的大查询,如下所示:

declare @myLocTbl as tblLocation_InMem;
insert into @myLocTbl <some select statement>;
select <stuff> from <some table> join @myLocTbl on <join stuff>;
select <other stuff> from <other table> join @myLocTbl on <more join stuff>;

一切正常,但我查看了数据库的File_MemOpt文件组的SQL数据文件夹,我在$ HKv2文件夹下看到了这样的内存表类型:

enter image description here

显然我的内存表类型并不存在于内存中。有没有办法我可以使用这种表格类型,并严格保留在内存中,而不是所有这些磁盘写入。我想通过创建一个内存表类型我避免任何磁盘活动。我还尝试创建内存表定义并指定durability = schema_only,但这仍然在磁盘上创建了东西。我应该只使用表变量吗?

我并不担心重启或其他任何事情的持久性。 select请求发生在REST Web请求的上下文中。如果服务器在请求期间重新启动,那么请求必须从头开始。

1 个答案:

答案 0 :(得分:0)

内存优化表仍然保留在磁盘中。如果您真的想要一个没有数据持久性的仅内存表,请使用

创建它
(with memory_optimized=on, durability=schema_only)

阅读文档以获取更多详细信息:

https://docs.microsoft.com/en-us/sql/relational-databases/in-memory-oltp/introduction-to-memory-optimized-tables

https://docs.microsoft.com/en-us/sql/t-sql/statements/create-table-transact-sql