当前位置:网站首页>明明加了唯一索引,为什么还是产生重复数据?
明明加了唯一索引,为什么还是产生重复数据?
2022-08-04 15:02:00 【InfoQ】
前言
mysql8
innodb
表
唯一索引
数据
重复

1.还原问题现场
防重表
CREATE TABLE `product_group_unique` (
`id` bigint NOT NULL,
`category_id` bigint NOT NULL,
`unit_id` bigint NOT NULL,
`model_hash` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL,
`in_date` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
唯一性
alter table product_group_unique add unique index
ux_category_unit_model(category_id,unit_id,model_hash);
唯一索引

2.唯一索引字段包含null
null



- 当model_hash字段不为空时,不会产生重复的数据。
- 当model_hash字段为空时,会生成重复的数据。
3.逻辑删除表加唯一索引
delete
delete from product where id=123;
物理删除
逻辑删除
update
update product set delete_status=1,edit_time=now(3)
where id=123;
name
model
name
model
delete_status
唯一索引
3.1 删除状态+1
- 添加记录a,delete_status=0。
- 删除记录a,delete_status=1。
- 添加记录a,delete_status=0。
- 删除记录a,delete_status=2。
- 添加记录a,delete_status=0。
- 删除记录a,delete_status=3。
3.2 增加时间戳字段
时间戳
1
秒
毫秒
3.3 增加id字段
主键
4. 重复历史数据如何加唯一索引?
历史重复数据
防重表
insert into product_unqiue(id,name,category_id,unit_id,model)
select max(id), select name,category_id,unit_id,model from product
group by name,category_id,unit_id,model;
id
select max(id), select name,category_id,unit_id,model from product
group by name,category_id,unit_id,model;
5.给大字段加唯一索引
5.1 增加hash字段
5.2 不加唯一索引
5.3 redis分布式锁
redis分布式锁
redis分布式锁

6.批量插入数据
for(Product product: list) {
try {
String hash = hash(product);
rLock.lock(hash);
//查询数据
//插入数据
} catch (InterruptedException e) {
log.error(e);
} finally {
rLock.unlock();
}
}
pipeline
边栏推荐
- 关于pnpm包管理器的版本问题
- 7 天找个 Go 工作,Gopher 要学的条件语句,循环语句 ,第3篇
- Find My Technology | Prevent your pet from getting lost, Apple Find My technology can help you
- How to write SQL statements: the usage of Update, Case, and Select together
- 蓝牙技术|上半年全国新增 130 万台充电桩,蓝牙充电桩将成为市场主流
- 聊聊与苹果审核员的爱恨情仇
- 数据链路层-------以太网协议
- Workaround without Project Facets
- leetcode:241. 为运算表达式设计优先级
- Technology sharing | Description of the electronic fence function in the integrated dispatching system
猜你喜欢
快解析结合千方百剂
ASA归因:如何评估关键词的投放价值
MySQL优化学习笔记
JCMsuite应用:倾斜平面波传播透过光阑的传输
蓝牙技术|上半年全国新增 130 万台充电桩,蓝牙充电桩将成为市场主流
Technology sharing | Mini program realizes audio and video calls
Find My Technology | Prevent your pet from getting lost, Apple Find My technology can help you
用于X射线聚焦的复合折射透镜
【Today in History】August 4: First female Turing Award winner; NVIDIA acquires MediaQ; first Cybersecurity Challenge completed
Redis-哨兵模式
随机推荐
Makefile syntax and usage notes
Next -18- 添加代码复制按钮
xampp安装包含的组件有(php,perl,apche,mysql)
License server system does not support this version of this feature
Android Sqlite3基本命令
[Opportunity Enlightenment-60]: "Soldiers, Stupid Ways"-1- Opening: "Death" and "Life" are the way of heaven
剑指Offer 63.股票的最大利润
第十六章 源代码文件 REST API 教程(一)
【云原生 | 从零开始学Kubernetes】kubernetes之StatefulSet详解
C# 谁改了我的代码
AOSP built-in APP franchise rights white list
leetcode: 212. Word Search II
指数族分布与最大熵
《分布式云最佳实践》分论坛,8月11日深圳见
Redis 高可用
SublimeText 粘贴图片保存到本地
leetcode:250. 统计同值子树
7 天能找到 Go 工作吗?学学 Go 数组和指针试试
【硬件架构的艺术】学习笔记(1)亚稳态的世界
【Web技术】1401- 图解 Canvas 入门