c#生成唯一编号方法记录,可用数据库主键 唯一+有序
2017-09-06 17:59
483 查看
数据库主键目前主要有两种:
a、自增数值型
优:占用空间小,插入快,有序对索引友好,易懂
缺:多数据库迁移会有重复键值问题,有可能爆表
b、GUID
优:多数据库唯一
缺:占用空间大,无序对索引不友好,不易懂
察看GUD发现最主要的问题还是在于无序对索引不友好,会引起性能问题,已知有以下两种方式可以解决:
1、基于Twitter的snowflake算法,生成一个long型ID,参考代码如下:
2、用NewId开源项目
项目地址:https://github.com/phatboyg/NewId
使用方法:
a、自增数值型
优:占用空间小,插入快,有序对索引友好,易懂
缺:多数据库迁移会有重复键值问题,有可能爆表
b、GUID
优:多数据库唯一
缺:占用空间大,无序对索引不友好,不易懂
察看GUD发现最主要的问题还是在于无序对索引不友好,会引起性能问题,已知有以下两种方式可以解决:
1、基于Twitter的snowflake算法,生成一个long型ID,参考代码如下:
public class IdWorker { private long workerId; private long datacenterId; private long sequence = 0L; private static long twepoch = 1288834974657L; private static long workerIdBits = 5L; private static long datacenterIdBits = 5L; private static long maxWorkerId = -1L ^ (-1L << (int)workerIdBits); private static long maxDatacenterId = -1L ^ (-1L << (int)datacenterIdBits); private static long sequenceBits = 12L; private long workerIdShift = sequenceBits; private long datacenterIdShift = sequenceBits + workerIdBits; private long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits; private long sequenceMask = -1L ^ (-1L << (int)sequenceBits); private long lastTimestamp = -1L; private static object syncRoot = new object(); public IdWorker(long workerId, long datacenterId) { // sanity check for workerId if (workerId > maxWorkerId || workerId < 0) { throw new ArgumentException(string.Format("worker Id can't be greater than %d or less than 0", maxWorkerId)); } if (datacenterId > maxDatacenterId || datacenterId < 0) { throw new ArgumentException(string.Format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId)); } this.workerId = workerId; this.datacenterId = datacenterId; } public long nextId() { lock (syncRoot) { long timestamp = timeGen(); if (timestamp < lastTimestamp) { throw new ApplicationException(string.Format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } if (lastTimestamp == timestamp) { sequence = (sequence + 1) & sequenceMask; if (sequence == 0) { timestamp = tilNextMillis(lastTimestamp); } } else { sequence = 0L; } lastTimestamp = timestamp; return ((timestamp - twepoch) << (int)timestampLeftShift) | (datacenterId << (int)datacenterIdShift) | (workerId << (int)workerIdShift) | sequence; } } protected long tilNextMillis(long lastTimestamp) { long timestamp = timeGen(); while (timestamp <= lastTimestamp) { timestamp = timeGen(); } return timestamp; } protected long timeGen() { return (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds; } }
2、用NewId开源项目
项目地址:https://github.com/phatboyg/NewId
使用方法:
NewId id = NewId.Next(); //produces an id like {11790000-cf25-b808-dc58-08d367322210} // Supports operations similar to GUID NewId id = NewId.Next().ToString("D").ToUpperInvariant(); // Produces 11790000-CF25-B808-2365-08D36732603A // Start from an id NewId id = new NewId("11790000-cf25-b808-dc58-08d367322210"); // Start with a byte-array var bytes = new byte[] { 16, 23, 54, 74, 21, 14, 75, 32, 44, 41, 31, 10, 11, 12, 86, 42 }; NewId theId = new NewId(bytes);
相关文章推荐
- C#生成唯一值的方法汇总
- C#生成唯一值的方法汇总
- 初学:数据库操作(个能记录,生成唯一标识的方法)
- 订单号(或唯一编号)生成方法-_-[商城使用]
- C#生成唯一值的方法汇总
- C#生成唯一值的方法汇总
- C#中插入记录,并同时返回插入后生成的自动编号
- C#生成唯一值的方法汇总
- 分布式ID生成方法-趋势有序的全局唯一ID
- C#生成唯一值的方法汇总
- C#生成唯一码方法
- 转:C#生成唯一值的方法汇总
- 流水号生成方法——按照时间格式生成的有序唯一编码(支持并发)
- C#生成唯一值的方法汇总
- C# 项目迁移 Microsoft.VisualStudio.Tools.Office.BuildTasks 生成解决方法报错:请确认 <UsingTask> 声明正确,该程序集及其所有依赖项都可用
- C#生成唯一值的方法汇总
- VS2010 (MFC)调用 C#生成DLL的两种方法
- c#生成唯一ID(strings&numbers )
- C#生成图片缩略图、水印及上传的几个通用方法
- javaSE中的输入输出流---一个读取流,相应多个输出流。并且生成的碎片文件都有有序的编号