Temporary
InMemory Tables [AX 2012]
This
topic has not yet been rated -
Updated: October
12, 2012
Applies
To: Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012
Feature Pack, Microsoft Dynamics AX 2012
在 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 .
<col>

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 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.
X++
staticvoid 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 .
staticvoid 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 .
在容器中的資料是連續地存儲和檢索,但在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.
你可以通過禁用 來禁用一個正常持久化的資料庫表,它控制着表.禁用這個鍵會導緻系統自動建立一個,
與資料庫表的字段和架構相比對的TempDB類型的臨時表.這個臨時表會在SQL
Server 資料庫的底層存在,
通過AOS管理.
自動建立這個TempDB表的目的,是為了能讓引用了被禁用表的AOT對象,繼續編譯和運作.
甚至在configuration
key被禁用時,你依然可以讀寫這個TempDB表.
所有表緩沖區變量都繼承 xRecord 類的方法.
其中一個方法是 setTmp , 它建立一個與正常表有着相同架構的InMemory
臨時表.
然而, setTmp 方法不能從TempDB表建立InMemory表.
你可以調用 isTempDb 方法确定 setTmp 方法是否可用.