Dynamics AX 2012 R2 Temporary InMemory Tables

Created at 2015-01-05 Updated at 2018-05-01 Category Microsoft Dynamics AX Tag Microsoft Dynamics AX



在 Microsoft Dynamics AX, 有一种类型的临时表叫做 InMemory table. 我们叫它 InMemory tables ,因为他们的 TableType 属性值是 InMemory . 这个值来自于枚举值 TableType::InMemory . The TableType property value can be seen at AOT > Data Dictionary > Tables > MyInMemoryTable > Properties > TableType .











Note
In Microsoft Dynamics AX 2009 and earlier versions, InMemory tables were called temporary tables. Now there are two kinds of temporary tables, namely InMemory tables and TempDB tables. To avoid confusion we do not use the phrase temporary tables to refer to just InMemory tables or to just TempDB tables.




不论是在客户端或者服务端层,InMemory表在进程运行所在的活动内存中被实例化. InMemory表从来不出现在数据库管理系统中.一个InMemory table 留在内存中,知道他的尺寸达到128KB. 该数据集然后被写到服务器层的一个磁盘文件. InMemory表的磁盘文件具有命名约定 $tmp<nnnnnnnn>.$$$.






当第一条记录插入后,InMemory表被实例化. 只要一条引用该表的记录缓冲区变量存在,实例化后的InMemory表就继续存在. 只要记录缓冲区超出范围,内存或硬盘就不给InMemory表分配空间.




To add data to an InMemory table, you must declare the record buffer and call the insert method. The following code example uses the TmpCustLedger table which has its TableTypeproperty set to InMemory in the AOT.








static void TableTmpInsertRecord(Args _args) {
TmpCustLedger custTmpLedger;
;
custTmpLedger.Name = ‘NameValue’;
custTmpLedger.Balance01 = 2345000;
custTmpLedger.insert();
}




要释放InMemory表的内存,并删除它的文件,可以设置 record buffer 变量为 null .

custTmpLedger = null;

下面演示从 CustTable table 复制数据到InMemory table ,它是 CustTable table 的表结构的副本. setTmp 方法用来创建一个与 CustTable table 匹配的 InMemory table. setTmp 方法改变getTableType方法的返回值 getTableType , from TableType::Regular to TableType::InMemory .







static void CopyPersistedTableToInMemoryJob(Args _args) {
CustTable custTable;

CustTable custTmpLedger;
custTmpLedger.setTmp();
custTable.recordLevelSecurity(true);

while select from custTable
where custTable.AccountNum like ‘1

{
custTmpLedger.data(custTable.data());
custTmpLedger.doInsert();
info(strFmt("Inserted a row for AccountNum =
%1",custTable.AccountNum));
}
custTmpLedger = null;
}









InMemory 表可以向 持久化的表一样定义索引. 如果一个InMemory表是通过拷贝一个持久化的表创建的,索引页会被拷贝到InMemory表中. 索引对于快速检索数据很有用,特别是当InMemory表数据在磁盘文件中时.




Microsoft Dynamics AX 支持一种特殊的数据类型,叫做 container .这种数据类型可以像你使用InMemory表一样使用. For more information, see Containers .在容器中的数据是连续地存储和检索,但在InMemory表中,你可以定义索引来加快数据检索. 对于几行数据来说,索引没有益处.在这种情况下,容器可能有更少的开销,更快的执行速度.



另一个重要的不同是,如何在方法调用中使用.当你传递一个InMemeory表到一个方法调用, 它通过引用传递.容器通过值传递.当一个变量通过引用传递,只有指向该对象的指针被传递. 当一个变量通过值传递,该变量的一个新的拷贝被传递给方法.如果计算机有一定限制量的内存, 它会开始交换内存到磁盘,减慢应用程序的执行.当你传递一个变量给方法,InMemory表可能提供 更佳的性能.

For more information about temporary tables, see Greef, Pontoppidan, et al. 2006. _Inside Microsoft Dynamics AX 4.0 _. 351-359. Redmond: Microsoft Press.





你可以通过禁用 configuration key 来禁用一个常规持久化的数据库表,它控制着表.禁用这个键会导致系统自动创建一个, 与数据库表的字段和架构相匹配的TempDB类型的临时表.这个临时表会在SQL Server 数据库的底层存在, 通过AOS管理.自动创建这个TempDB表的目的,是为了能让引用了被禁用表的AOT对象,继续编译和运行. 甚至在configuration key被禁用时,你依然可以读写这个TempDB表.



所有表缓冲区变量都继承 xRecord 类的方法. 其中一个方法是 setTmp , 它创建一个与常规表有着相同架构的InMemory 临时表. 然而, setTmp 方法不能从TempDB表创建InMemory表. 你可以调用 isTempDb 方法确定 setTmp 方法是否可用.


Site by Reinhard Hsu using Hexo & Random

Hide