gpt4 book ai didi

nhibernate - Fluent NHibernate 对数千个对象执行单行选择以链接到父对象

转载 作者:行者123 更新时间:2023-12-04 02:07:18 25 4
gpt4 key购买 nike

以下是我的问题域的简化。我们有一系列交易 , 每个都在 中获得记录估值 每个工作日。

我们过滤我们的估值 用于特定日期的列表,但用于填充 贸易 针对 中的每一个估值 行,NHibernate 在 上触发单行选择交易 评估表中大约 50k 行的表。我怎样才能改变这一点,以便 NHibernate 在交易表上进行一次选择?

CREATE TABLE Trades
( TradeId INT
, InstrumentType VARCHAR(20)
, TradeDate DATETIME

, PRIMARY KEY
( TradeId ) )

CREATE TABLE Valuations
( TradeId INT
, ValueDate DATETIME
, PresentValue NUMERIC(12,4)

, PRIMARY KEY
( TradeId
, ValueDate ) )

.
class Trade
{
public int TradeId;
public string InstrumentType;
public DateTime TradeDate;
}

class Valuation
{
public int TradeId;
public DateTime ValueDate;
public double PresentValue;
public Trade Trade;
}

.
class ValuationMap : ClassMap<Valuation>
{
public ValuationMap()
{
WithTable("Valuations");
UseCompositeId()
.WithKeyProperty(x => x.ValueDate)
.WithKeyProperty(x => x.TradeId);

Map(x => x.PresentValue);

References(x => x.Trade, "TradeId")
.LazyLoad()
.Cascade.None()
.NotFound.Ignore()
.FetchType.Join();
}
}

class TradeMap : ClassMap<Trade>
{
public TradeMap()
{
WithTable("Trades");

Id( x => x.TradeId );

Map(x => x.InstrumentType);
Map(x => x.TradeDate);
Map(x => x.Valuations);
}
}

.
public List<Valuation> GetValuations(DateTime valueDate)
{
return (from p in _nhibernateSession.Linq<Valuation>()
where p.ValueDate == valueDate
select p).ToList();
}

最佳答案

您还应该查看批量获取。这引自 Nhib manual - 实际上 google 的缓存,因为该站点似乎因维护 atm 而关闭:

Imagine you have the following situation at runtime: You have 25 Cat instances loaded in an ISession, each Cat has a reference to its Owner, a Person. The Person class is mapped with a proxy, lazy="true". If you now iterate through all cats and get the Owner of each, NHibernate will by default execute 25 SELECT statements, to retrieve the proxied owners. You can tune this behavior by specifying a batch-size in the mapping of Person:


<class name="Person" lazy="true" batch-size="10">...</class>

NHibernate will now execute only three queries, the pattern is 10, 10, 5. You can see that batch fetching is a blind guess, as far as performance optimization goes, it depends on the number of unitilized proxies in a particular ISession.

关于nhibernate - Fluent NHibernate 对数千个对象执行单行选择以链接到父对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/771747/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com