表和查找字段性能问题

TTable and lookup fields performance issue

我有一个 TTable(实际上是一个 UniDac TUniTable),它有 table 字段加上来自另一个 table 的 2 个查找字段(Mysql). 我已经设置了正确的索引并且 table 加载速度非常快。问题是当我手动编辑像

这样的字段时
Table1.FieldByName('discount_value').AsInteger := 10;

即使没有 Post() 每次执行此命令时,它都会非常慢。 如果我删除 2 个查找字段,一切都很好 - 它非常快。

看起来在执行 Post() 之前,查找字段以某种方式加载到每个记录编辑中。

有什么方法可以防止这种情况发生或以某种方式检索查找字段一次然后缓存而不一次又一次地加载?

在 Embarcadero WiKi 中,必须将 AutoCalcFields 设置为 False 以便仅在记录打开时进行计算。

http://docwiki.embarcadero.com/Libraries/XE6/en/Data.DB.TDataSet.AutoCalcFields

When AutoCalcFields is False, Lookup fields are recalculated and the OnCalcFields event occurs only when:
* The dataset is opened.
* The dataset is put into dsEdit state.
* A record is retrieved from database.

您可以尝试 TField.LookupCache 属性,它控制查找字段的值是否被缓存。

Determines whether the values of a lookup field are cached or looked up dynamically every time the current record in the dataset changes.

Set LookupCache to true to cache the values of a lookup field when the LookupDataSet is unlikely to change and the number of distinct lookup values is small. Caching lookup values can speed performance, because the lookup values for every set of LookupKeyFields values are preloaded when the DataSet is opened. When the current record in the DataSet changes, the field object can locate its Value in the cache, rather than accessing the LookupDataSet. This performance improvement is especially dramatic if the LookupDataSet is on a network where access is slow.

上面链接的文档中提供了更多信息,包括有关某些性能注意事项和在运行时手动刷新 LookupList 的信息。