高并发分布式系统中生成全局唯一(订单号)Id
2024-10-02 20:55:10
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如下图
结论:适合大型应用,从业务上来说,有一个规则的编码能体现产品的专业成度。
最新文章
- Tomcat常见问题及常用命令
- Mybatis中#{}和${}传参的区别
- Nginx模块之————RTMP模块在Ubuntu 14.04年的设置与搭建
- socket初级使用(客户端)
- nodejs之获取客户端真实的ip地址+动态页面中引用静态路径下的文件及图片等内容
- U盘分区信息清除
- 学习 AngularJS 第一天
- Repositories.EntityFramework 实现方式
- IOS 如何选择delegate、notification、KVO?
- vmlinux,vmlinuz,bzimage,zimage,initrd.img的区别与联系
- SimpleWiFi模块评估板
- 109.110.100.56 samba用户名 PAS, 密码 111111
- matlab获取向量中出现次数最多的元素
- 【Win 10 应用开发】在代码中加载文本资源
- NOIP2018旅行
- 【BZOJ1426】收集邮票 期望DP
- PHP字符串反转
- 【noip模拟赛3】编码
- C#的ThreadStart 和 Thread
- Beta 冲刺 第三天