当前位置:网站首页>Implementation of MySQL custom sequence number
Implementation of MySQL custom sequence number
2022-06-23 05:29:00 【Daydayup, and】
One 、 Preface
In many cases , We need to use our own unique Id Or a serial number guaranteed not to be repeated , Especially in high concurrency scenarios . So , It is easy to think of the following implementation methods :
- Use through atomic manipulation and locking mechanisms Java Code implementation , In the single JVM Maintain the number of sequences in memory , In addition, you need to ensure that it is persisted to the external storage system ;
- Or use components that ensure high distribution and consistency Zookeeper, With its distributed locks, it is easy to implement , The disadvantage is that you need to introduce Zookeeper The components of depend on , Undoubtedly, it increases the complexity and maintainability of the system ( Switch to a Redis It's the same thing );
- Another easy idea is to use MySQL Transaction operation of , Stored procedures can meet the requirements of transaction , So this method is lighter than the first method .
Here is the introduction of using customization MySQL Sequence number to generate unique Id.
Two 、 Create sequence table
DROP TABLE
IF EXISTS sequence;
CREATE TABLE
sequence
(
name VARCHAR(50) NOT NULL,
current_value BIGINT NOT NULL,
increment INT NOT NULL DEFAULT 1,
PRIMARY KEY (name)
)
ENGINE=InnoDB;3、 ... and 、 Insert defined sequence
INSERT INTO sequence VALUES ('seq1', 0, 1);
INSERT INTO sequence VALUES ('seq2', 0, 1);
...Four 、 Custom function implementation
DROP FUNCTION IF EXISTS `seq`;
DELIMITER $$
CREATE FUNCTION `seq`(seq_name char (20)) returns BIGINT
BEGIN
UPDATE sequence SET current_value=last_insert_id(current_value + increment) WHERE name=seq_name;
RETURN last_insert_id();
END $$
DELIMITER ;5、 ... and 、MyBatis To get the latest sequence number
<select id="getRowkeyByName" parameterType="java.lang.String" resultType="long">
select `seq`(
#{name}
)
</select>above .
边栏推荐
- Mathematical analysis_ Notes_ Chapter 1: set and mapping
- Array The from method creates an undefined array of length n
- [leetcode] longest increasing subsequence problem and its application
- stm32时钟树错误配置导致 开机进入硬中断
- Drag and drop frame
- Mysql入门学习(一)之语法
- 关于信息泄露和防御
- JDBC入门学习(四)之Druid连接池的使用
- CF【1700D】D. River Locks(dp、二分、数学)
- When I was young, I thought my father was omnipotent
猜你喜欢
随机推荐
Get bat command results in bat
Facing new challenges and becoming a better self -- an advanced technology er
Database connection exception: create connection error, url: jdbc: mysql://ip/ Database name, errorcode 0, state 08s01 problem handling
Konva series tutorial 1:what is konva?
STM32 clock tree misconfiguration causes boot to enter hard interrupt
TIOBE 编程语言排行榜是编程语言流行趋势的一个指标
JDBC入门学习(二)之封装工具类
云原生架构(04)-CNCF
Drag and drop frame
GO语言-panic和recover
Fund performance evaluation
JDBC入门学习(三)之事务回滚功能的实现
MCS: continuous random variable chi square distribution
Array The from method creates an undefined array of length n
Drama asking Huamen restaurant Weng
投资风险管理
Spark 离线开发框架设计与实现
MCS: continuous random variable lognormal distribution
Mysql入门学习(二)之子查询+关联
hash---------history









