nHibernate与hiLo id生成 - 在保存对象图时选择“N + 1”

时间:2011-04-21 15:18:06

标签: nhibernate

为了示例,让我们说我的应用程序的域名是一个健康俱乐部,客户购买健身俱乐部会员资格。这些会员资格包括一系列特权,以及客户健康状况的记录。

public class Customer
    {
        public int Id { get; set; }
        public string FullName { get; set; }
        //... some more properties
        public Membership Membership { get; set; }
    }

class Membership
{
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public FitnessRecord FitnessRecord { get; set; }
    public IEnumerable<Privilige> MemberPriviliges { get; set; }

}

class FitnessRecord
{
    public DateTime Date { get; set; }
    public int NumberOfPushups { get; set; }
    //etc...
}

class Privilige
{
    public ResourceType Resource { get; set; } //could be 'swimming pool', 'sauna' etc..
    public PriviligeType Type { get; set; } //day pass / month pass etc..
}

创建新客户时,会对所有这些对象进行实例化并保存。 我正在使用hiLo进行Id生成。 这导致'更新N + 1'问题,如下所示:

2011-04-21 17:48:52:468 DEBUG MyApp.Managers - saving Membership with ID 0. 
2011-04-21 17:48:52:484 DEBUG NHibernate.SQL - Reading high value:select next_hi from dbo._uniqueKey with (updlock, rowlock)
2011-04-21 17:48:52:484 DEBUG NHibernate.SQL - Updating high value:update dbo._uniqueKey set next_hi = @p0 where next_hi = @p1;@p0 = 28735, @p1 = 28734
2011-04-21 17:48:52:484 DEBUG NHibernate.SQL - Reading high value:select next_hi from dbo._uniqueKey with (updlock, rowlock)
2011-04-21 17:48:52:484 DEBUG NHibernate.SQL - Updating high value:update dbo._uniqueKey set next_hi = @p0 where next_hi = @p1;@p0 = 28736, @p1 = 28735
2011-04-21 17:48:52:484 DEBUG NHibernate.SQL - Reading high value:select next_hi from dbo._uniqueKey with (updlock, rowlock)
2011-04-21 17:48:52:484 DEBUG NHibernate.SQL - Updating high value:update dbo._uniqueKey set next_hi = @p0 where next_hi = @p1;@p0 = 28737, @p1 = 28736
2011-04-21 17:48:52:500 DEBUG NHibernate.SQL - Reading high value:select next_hi from dbo._uniqueKey with (updlock, rowlock)
2011-04-21 17:48:52:500 DEBUG NHibernate.SQL - Updating high value:update dbo._uniqueKey set next_hi = @p0 where next_hi = @p1;@p0 = 28738, @p1 = 28737
2011-04-21 17:48:52:500 DEBUG MyApp.Managers - commiting transaction
2011-04-21 17:48:52:546 DEBUG NHibernate.SQL - Batch commands:
command 0:INSERT INTO dbo.[FitnessRecord] (..., ..., ..., ..., ..., ..., Id) VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6);@p0 = 21/04/2011 17:48:52, @p1 = 25, @p2 = NULL, @p3 = False, @p4 = NULL, @p5 = 5, @p6 = 28736

2011-04-21 17:48:52:546 DEBUG NHibernate.SQL - Batch commands:
command 0:INSERT INTO dbo.[Membership] (..., ..., ..., ..., ..., FitnessRecord_id,..., Id) VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7);@p0 = 'Stew', @p1 = 'SIMULATED', @p2 = False, @p3 = '300', @p4 = False, @p5 = 28736, @p6 = 28726, @p7 = 28735

2011-04-21 17:48:52:546 DEBUG NHibernate.SQL - Batch commands:
command 0:INSERT INTO dbo.[Privilige] (...,..., ..., ..., ..., ..., ..., ..., ..., Id) VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9);@p0 = 'BRS', @p1 = 'DST', @p2 = 'SN', @p3 = 21/04/2011 00:00:00, @p4 = '2056', @p5 = '010A', @p6 = 'M', @p7 = 'A35', @p8 = False, @p9 = 28737
command 1:INSERT INTO dbo.[Privilige] (...,..., ..., ..., ..., ..., ..., ..., ..., Id) VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9);@p0 = 'BRS', @p1 = 'DST', @p2 = 'SN', @p3 = 21/04/2011 00:00:00, @p4 = '02056', @p5 = '010A', @p6 = 'M', @p7 = 'A31', @p8 = False, @p9 = 28742

2011-04-21 17:48:52:546 DEBUG NHibernate.SQL - Batch commands:
command 0:INSERT INTO dbo.[Customer] (..., ..., ..., ..., Membership_id, ..., Id) VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6);@p0 = 21/04/2011 17:48:52, @p1 = 'ValidEntryScan', @p2 = 'ValidEntryScan', @p3 = False, @p4 = 28735, @p5 = 16, @p6 = 28734

2011-04-21 17:48:52:546 DEBUG NHibernate.SQL - Batch commands:
command 0:UPDATE dbo.[FitnessRecord] SET ... = @p0, ...= @p1, ...= @p2, ...= @p3, Membership_id = @p4, ...= @p5 WHERE Id = @p6;@p0 = 21/04/2011 17:48:52, @p1 = 25, @p2 = NULL, @p3 = False, @p4 = 28735, @p5 = 5, @p6 = 28736

2011-04-21 17:48:52:562 DEBUG NHibernate.SQL - Batch commands:
command 0:UPDATE dbo.[Privilige] SET Membership_id = @p0 WHERE Id = @p1;@p0 = 28735, @p1 = 28737
command 0:UPDATE dbo.[Privilige] SET Membership_id = @p0 WHERE Id = @p1;@p0 = 28735, @p1 = 28742

2011-04-21 17:48:52:562 DEBUG MyApp.Managers - finished 

注意这样一个事实,即使我正在使用批处理,这些记录仍然需要大约100毫秒才能保存;那似乎有点过分,不是吗?有什么方法可以改进吗?

1 个答案:

答案 0 :(得分:0)

没有“更新N + 1”问题。

由于每个max_lo个项目都会检索/更新HiLo,因此这些语句只会在您第一次执行时发生,并且以下调用的时间实际上为零。