当前位置:网站首页>Select auto increment or sequence for primary key selection?
Select auto increment or sequence for primary key selection?
2022-06-27 20:27:00 【Ink Sky Wheel】
First of all, the design of different databases is different , Some are self increasing , Some have sequences , How to use it? ? Used many years ago Oracle When we do the public security system every time we pass a car , No matter whether it is a motor vehicle or a non motor vehicle , Places like Shanghai are adding new data every day 1 Up and down .300 More than million local motor vehicles plus some non Shanghai motor vehicles , Less than 400 ten thousand . Normally a private car drives out , Drive back , after 10 It's very common to cross a road , So it is recorded every day 20 Time . So not to 400 ten thousand X20 many times . About 100 million . The situation is similar across the country .
So how to store these data , Ensure efficient query ? First of all, we check the vehicle license plate , Then the license plate must be an index . But our vehicles are mainly photographed and stored with photos . Our photos don't exist in the database ( That's too big , But in the early days, it was stored in the database ), At this time, the primary key and the name of the image should correspond to each other . This requires the uniqueness of the primary key . How to ensure efficiency without conflict .
Suppose we installed in Jiangsu Province 1 Million pieces of equipment from Xuzhou to Suzhou ( The premise is that we have a big job in the whole province ). Each device controls 4-8 A driveway , Almost every second 1k-1w The equipment ( You can think about becoming the Internet of things ) Write the collected data to the database . The actual database writes are in the range of hundreds to thousands per second .
The primary key method we used at that time was administrative division + Equipment serial number + Vehicle Lane + Mm / DD / yyyy HHM / S .
Administrative division is understood as postal code , We mail letters in the mainland 6 Bit number can be distinguished into a town .
Equipment serial number , It can be installed at a township level 100 The devices are already saturated . But we kept it 4 position , Can install 9999 Devices . Even Beijing Tiantongyuan, a community that spans three subway stations , That's enough. . The equipment in each administrative division is from 1 Numbered starting .
Each device has 1-16 A driveway .
Each lane can only have one car at most in the same second .
Therefore, the equipment in Tianning District, Changzhou City, Jiangsu Province is 213000+1234+5+2022 year 6 month 1 Japan 10 spot 01 branch 30 second namely 2130001234520220601100130 This is the primary key . When we look at this number, we can also know where the data comes from .
It can be seen that we are useless Oracle Sequence . For the following reasons :
1、 Take the sequence competition , We have to process hundreds of thousands per second . There is no competition in design , There is no need to redis. Write fails in case of conflict , When the car passes, it passes , It is impossible to resend . The request cannot be delayed , You can't lose .
2、 Natural numbers have no meaning , No practical use .
3、 What to do if you encounter the biggest problem ? Show me
When the a The maximum of the stopped , This is suitable for human intervention to continue to expand , But when is the end ?
If circular reuse is adopted, please see the following . Technically, it can , But if the sequence is a primary key, then there is a conflict .
So we don't use sequences , Not a bad sequence , It's better that we design it this way . So there is no problem with billions or tens of billions on a single watch .
So are other databases the same ? Let me see PG Of . Grammar and Oracle It's a little different
create sequence a increment by 1 minvalue 1 maxvalue 5 start with 1;
create sequence b increment by 1 minvalue 1 maxvalue 5 start with 1 cycle;
Experimental results and Oracle Is exactly the same .
Respectively, stop at the maximum value .
And cyclic reuse .
What if there is no sequence ? Some databases do not have sequences but have self augmentation , such as MySQL. Please do not mention here from redis To take ( At least in my business flow, I will never design like this ), The reasons are all said , In addition, what is the use of meaningless storage ?
that MySQL What is the use of self augmentation ? because Oracle It's a pile of watches ,MySQL It's a clustered index table , Sequential storage is the most compact . that id Self augmentation is to give you compact storage . Never have business implications , Put the business meaning in the second column to create a unique . A few days ago, I encountered several databases to be merged , At this time, it reflects the pain of disobedience . They are all self increasing id Having business meaning or relationship . Now the table data will be merged , Everyone is mutually exclusive . Brain damage . I wish I had listened to you .
So this self increase ID How to choose ? It's usually int,int Related to scope , It's not about length . give an example tinyint Can only save 256 Data , If you take negative, you'll only get to 127 了 .
So here we are 127 in the future , No more , Add more or 127. If you want to continue, you will report primary key conflicts . This type can only be changed .
So compatible MySQL Of Tidb How to deal with it ?
The result looks like Oracle The sequence of reaches the maximum .
Different databases , Different scene designs are different . Don't use it wrong .

边栏推荐
- MySQL beginner benefits
- Linux system plays Oracle database multi table query connection query with a smile
- SQL reported an unusual error, which confused the new interns
- 数据库优化
- 【bug】上传图片出现错误(413 Request Entity Too Large)
- SQL报了一个不常见的错误,让新来的实习生懵了
- 数据库引擎
- 难怪大家丢掉了postman而选择 Apifox
- Wechat IOS version 8.0.24 update release cache subdivision cleaning Online
- Adding, deleting, modifying and querying MySQL tables (basic)
猜你喜欢

低代码开发平台是什么?为什么现在那么火?
一段时间没用思源,升级到最新的 24 版后反复显示数据加密问题

MongoDB简介及典型应用场景

谈谈我写作生涯的画图技巧

Univision hyperinsight: Nuggets' $16.494 billion "gold hoe" in the observable market?

CSDN 技能树使用体验与产品分析(1)

UE4实现长按功能

Summary of submarine cable detection technology

On the drawing skills of my writing career

优维HyperInsight:掘金164.94亿美元可观测市场的“金锄头”?
随机推荐
UE4随笔:FString、FName 与 FText
ABAP essays - interview memories hope that everyone's demand will not increase and the number of people will soar
Web APLS phase - Section 14 - local storage
Manage rust project through cargo
UE4 actor Basics
海量数据出席兰州openGauss Meetup(生态全国行)活动,以企业级数据库赋能用户应用升级
Enumeration and control flow operation in rust
Accumulating power in the middle stage, UFIDA IUAP builds a new base for digital intelligence of social enterprises
UOS提示输入密码以解锁您的登陆密钥环解决办法
SQL Server - window function - solve the problem of filtering consecutive n records
UE4: explanation of build configuration and config
连接集成开发专题月 | 企业主数据治理的驱动因素
Ble Bluetooth module nrf518/nrf281/nrf528/nrf284 chip scheme comparison
Redis persistence
SQL报了一个不常见的错误,让新来的实习生懵了
SQL reported an unusual error, which confused the new interns
# Leetcode 821. 字符的最短距离(简单)
[required reading for high-quality products] sub query of Oracle database in Linux system
Talk about graduation season
openssl客户端编程:一个不起眼的函数导致的SSL会话失败问题







