惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

宝玉的分享
宝玉的分享
IT之家
IT之家
Stack Overflow Blog
Stack Overflow Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
腾讯CDC
P
Palo Alto Networks Blog
Spread Privacy
Spread Privacy
S
Schneier on Security
NISL@THU
NISL@THU
WordPress大学
WordPress大学
酷 壳 – CoolShell
酷 壳 – CoolShell
P
Proofpoint News Feed
T
Threatpost
Scott Helme
Scott Helme
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Exploit Database - CXSecurity.com
I
Intezer
C
Check Point Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
C
Cyber Attacks, Cyber Crime and Cyber Security
S
Securelist
Security Latest
Security Latest
大猫的无限游戏
大猫的无限游戏
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
小众软件
小众软件
www.infosecurity-magazine.com
www.infosecurity-magazine.com
云风的 BLOG
云风的 BLOG
量子位
T
Tor Project blog
博客园 - 叶小钗
The Cloudflare Blog
Simon Willison's Weblog
Simon Willison's Weblog
T
Tailwind CSS Blog
W
WeLiveSecurity
Hacker News - Newest:
Hacker News - Newest: "LLM"
Attack and Defense Labs
Attack and Defense Labs
S
Security Affairs
罗磊的独立博客
Know Your Adversary
Know Your Adversary
Engineering at Meta
Engineering at Meta
G
Google Developers Blog
Help Net Security
Help Net Security
美团技术团队
P
Privacy International News Feed
The Hacker News
The Hacker News
Hugging Face - Blog
Hugging Face - Blog
MongoDB | Blog
MongoDB | Blog
N
Netflix TechBlog - Medium
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知

博客园 - 天际翔龙

一个苹果证书如何多次使用——导出p12文件[多台电脑使用] Log4net系列一:Log4net搭建之文本格式输出【转】 SMMS 2016 啟用深色主題 c#代碼小集 ZenCoding[Emmet]語法簡介【轉】 vscode: Visual Studio Code 常用快捷键【轉】 C#枚举Enum[轉] Android 杂记 解决genymotion-arm-translation.zip无法拖拽安装的问题[转] c#同步調用異步(async)方法【記錄用】 DDD基本概念 server2012/win8 卸载.net framework 4.5后 无法进入系统桌面故障解决【转】 Entity Framework中AutoDetectChangesEnabled為false時更新DB方法 git常用命令备忘录 MSSQL日誌傳輸熱備份注意事項 Angular 隨記 使用dumpbin命令查看dll导出函数及重定向输出到文件【轉】 UML类图与类的关系详解【转】 知識隨記
c#生成唯一编号方法记录,可用数据库主键 唯一+有序
天际翔龙 · 2017-09-06 · via 博客园 - 天际翔龙

数据库主键目前主要有两种:

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);