当前位置:网站首页>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 .

边栏推荐
猜你喜欢

本周二晚19:00战码先锋第8期直播丨如何多方位参与OpenHarmony开源贡献

难怪大家丢掉了postman而选择 Apifox

【STL编程】【竞赛常用】【part 2】

从指令交读掌握函数调用堆栈详细过程

SQL Server - window function - solve the problem of filtering consecutive n records

redis数据结构

蓄力中台,用友iuap筑牢社会级企业数智化新底座

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

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

MongoDB简介及典型应用场景
随机推荐
数据库索引
数据库引擎
Talk about graduation season
Accumulating power in the middle stage, UFIDA IUAP builds a new base for digital intelligence of social enterprises
数智化进入“深水区”,数据治理是关键
移动低代码开发专题月 | 可视化开发 一键生成专业级源码
MySQL beginner benefits
Postman Chinese tutorial (postman Chinese version)
Golang map 并发读写问题源码分析
Summary of submarine cable detection technology
muduo
探秘GaussDB,听听客户和伙伴怎么说
#夏日挑战赛# OpenHarmony HiSysEvent打点调用实践(L2)
Observable, reliable: the first shot of cloudops series Salon of cloud automation operation and maintenance
Rust 所有权进阶 -- 内存管理
【debug】平台工程接口调试
pfSense Plus22.01中文定制版发布
回溯相关问题
429- binary tree (108. convert the ordered array into a binary search tree, 538. convert the binary search tree into an accumulation tree, 106. construct a binary tree from the middle order and post o
MongoDB简介及典型应用场景







