1、GUID数据因毫无规律可言造成索引效率低下,影响了系统的性能,那么通过组合的方式,保留GUID的10个字节,用另6个字节表示GUID生成的时间(DateTime),这样我们将时间信息与GUID组合起来,在保留GUID的唯一性的同时增加了有序性,以此来提高索引效率,在NHibernate中,COMB型主键的生成代码如下所示:

        /// <summary>
/// 保留GUID的10个字节,用另6个字节表示GUID生成的时间(DateTime)组合方式
/// </summary>
/// <returns></returns>
public static Guid GenerateComb()
{
byte[] guidArray = Guid.NewGuid().ToByteArray(); DateTime baseDate = new DateTime(, , );
DateTime now = DateTime.Now; // Get the days and milliseconds which will be used to build
//the byte string
TimeSpan days = new TimeSpan(now.Ticks - baseDate.Ticks);
TimeSpan msecs = now.TimeOfDay; // Convert to a byte array
// Note that SQL Server is accurate to 1/300th of a
// millisecond so we divide by 3.333333
byte[] daysArray = BitConverter.GetBytes(days.Days);
byte[] msecsArray = BitConverter.GetBytes((long)
(msecs.TotalMilliseconds / 3.333333)); // Reverse the bytes to match SQL Servers ordering
Array.Reverse(daysArray);
Array.Reverse(msecsArray); // Copy the bytes into the guid
Array.Copy(daysArray, daysArray.Length - , guidArray,
guidArray.Length - , );
Array.Copy(msecsArray, msecsArray.Length - , guidArray,
guidArray.Length - , ); return new Guid(guidArray);
}

上述方法循环测试生成id如下图

结论:适合大型应用。即保留GUID的唯一性的同时增加了GUID有序性,提高了索引效率;解决了关联表业务问题;生成的Id不够友好;占据了32位。

2、将GUID转为了19位数字

        /// <summary>
/// 根据GUID获取19位的唯一数字序列
/// </summary>
public static long GuidToLong()
{
byte[] buffer = Guid.NewGuid().ToByteArray();
return BitConverter.ToInt64(buffer, );
}

上述方法循环测试生成id如下图

结论:适合大型应用,从业务上来说,有一个规则的编码能体现产品的专业成度。

最新文章

  1. Tomcat常见问题及常用命令
  2. Mybatis中#{}和${}传参的区别
  3. Nginx模块之————RTMP模块在Ubuntu 14.04年的设置与搭建
  4. socket初级使用(客户端)
  5. nodejs之获取客户端真实的ip地址+动态页面中引用静态路径下的文件及图片等内容
  6. U盘分区信息清除
  7. 学习 AngularJS 第一天
  8. Repositories.EntityFramework 实现方式
  9. IOS 如何选择delegate、notification、KVO?
  10. vmlinux,vmlinuz,bzimage,zimage,initrd.img的区别与联系
  11. SimpleWiFi模块评估板
  12. 109.110.100.56 samba用户名 PAS, 密码 111111
  13. matlab获取向量中出现次数最多的元素
  14. 【Win 10 应用开发】在代码中加载文本资源
  15. NOIP2018旅行
  16. 【BZOJ1426】收集邮票 期望DP
  17. PHP字符串反转
  18. 【noip模拟赛3】编码
  19. C#的ThreadStart 和 Thread
  20. Beta 冲刺 第三天

热门文章

  1. kindeditor富文本编辑器初步使用教程
  2. 四方定理(递归) --java
  3. Myeclispe 代码自动补全
  4. java static learning
  5. [Asp.net core]使用ssh命令发布asp.net core项目
  6. 12、Java函数接口
  7. css-div翻转动画
  8. 不要使用Integer做HashMap的key,尤其在json序列化的时候
  9. XCode各种问题
  10. codevs 2033 邮票