当前位置:网站首页>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 .
边栏推荐
猜你喜欢
SQL Server - window function - solve the problem of filtering consecutive n records
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
Adding, deleting, modifying and querying MySQL tables (basic)
散列表(Hash)-复习
Grasp the detailed procedure of function call stack from instruction reading
CSDN 技能樹使用體驗與產品分析(1)
Redis persistence
数据库锁问题
Database lock problem
一段时间没用思源,升级到最新的 24 版后反复显示数据加密问题
随机推荐
Mass lucky hash game system development - conflict resolution (code analysis)
Database optimization
UE4随笔:FString、FName 与 FText
ABAP-CL_OBJECT_COLLECTION工具类
UE4 realizes long press function
Postman 汉化教程(Postman中文版)
[debug] platform engineering interface debugging
ABAP随笔-面试回忆 望大家 需求不增 人天飙升
1025 PAT Ranking
数据仓库体系之贴源层、历史层
Question brushing record: easy array (continuously updated)
北汽制造全新皮卡曝光,安全、舒适一个不落
Common shell script commands (III)
CSDN 技能树使用体验与产品分析(1)
难怪大家丢掉了postman而选择 Apifox
数智化进入“深水区”,数据治理是关键
BLE蓝牙模块NRF518/NRF281/NRF528/NRF284芯片方案对比
#夏日挑战赛# OpenHarmony HiSysEvent打点调用实践(L2)
云原生存储解决方案Rook-Ceph与Rainbond结合的实践
【bug】上传图片出现错误(413 Request Entity Too Large)