我想使用客戶端生成的GUID沒有它在數據庫端的碎片成本(我不想考慮生成數據庫端的主鍵,因爲我想利用DDD和單元測試)。客戶端的GUID生成沒有表碎片成本
我使用SQL Server 2012和Entity Framework 6
- 對於這一點,我創建了一個表中的SQL服務器:
與實體
public partial class Mac { public Mac() { this.Id = Guid.NewGuid() } public System.Guid Id { get; set; } public string direccionMac { get; set; } }
- 我明白這個設計會導致表碎片。因此,如下所示:https://stackoverflow.com/a/604842/310107我添加了另一列作爲名爲MyClusterKey(identity(1,1))的聚簇索引。
所以,Id,非集羣和主鍵。 MyClusterKey,聚簇索引,標識(1,1)
與實體:
public partial class Mac
{
public Mac()
{
this.Id = Guid.NewGuid();
}
public System.Guid Id { get; set; }
public long MyClusterKey { get; set; }
public string direccionMac { get; set; }
}
MyClusteredKey加入作爲DatabaseGeneratedOption.Identity:
public class MacMap : EntityTypeConfiguration<Mac>
{
public MacMap()
{
// Primary Key
this.HasKey(t => t.Id);
// Properties
this.Property(t => t.MyClusterKey)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
this.Property(t => t.direccionMac)
.IsRequired()
.IsFixedLength()
.HasMaxLength(16);
// Table & Column Mappings
this.ToTable("Macs");
this.Property(t => t.Id).HasColumnName("Id");
this.Property(t => t.MyClusterKey).HasColumnName("MyClusterKey");
this.Property(t => t.direccionMac).HasColumnName("direccionMac");
}
}
- 另一種選擇是使用選項1(不含其他聚簇索引),但使用序列化升GuidComb在https://github.com/nhibernate/nhibernate-core/blob/master/src/NHibernate/Id/GuidCombGenerator.cs
- 是選項1正確實施?
- 什麼是首選選項:1,2,3都不是,爲什麼?
- 選項3正在生成僅在最後一部分內排序的guid。這不會生成表碎片(如果使用該ID作爲聚簇鍵)?
- 在選項2中,MyClusterKey列僅用於提供聚簇索引並避免表碎片?或者它可以有另一種用途?
這會以某種順序生成的GUID:
問題:
我的投票轉到選項2 – Biscuits
也考慮'NEWSEQUENTIALID()' – Biscuits
MyClusterKey是否在業務層中使用?這只是爲了防止碎片化,根本不會被使用? – nerlijma