当前位置:网站首页>明明加了唯一索引,为什么还是产生重复数据?
明明加了唯一索引,为什么还是产生重复数据?
2022-08-04 15:02:00 【InfoQ】
前言
mysql8innodb表唯一索引数据重复
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.逻辑删除表加唯一索引
deletedelete from product where id=123;
物理删除逻辑删除updateupdate product set delete_status=1,edit_time=now(3)
where id=123;
namemodelnamemodeldelete_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;
idselect 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边栏推荐
猜你喜欢

1403. Minimum Subsequence in Non-Increasing Order

Cisco - Small Network Topology (DNS, DHCP, Web Server, Wireless Router)

Technology sharing | Description of the electronic fence function in the integrated dispatching system
![[The Art of Hardware Architecture] Study Notes (1) The World of Metastability](/img/ac/54e4e13d9df90e96933c69623b770e.png)
[The Art of Hardware Architecture] Study Notes (1) The World of Metastability

数据链路层-------以太网协议

期货开户之前要谈好最低手续费和交返

直播系统开发——直播间架构的设计及难点分析

JCMsuite应用:倾斜平面波传播透过光阑的传输

FRED应用:毛细管电泳系统

输入输出流总结
随机推荐
leetcode:254. 因子的组合
leetcode:250. 统计同值子树
Go 语言快速入门指南: 变量和常量
技术分享| 小程序实现音视频通话
FRED Application: Capillary Electrophoresis System
【硬件架构的艺术】学习笔记(1)亚稳态的世界
leetcode:255 验证前序遍历序列二叉搜索树
Zheng Qing freshmen school competition and middle-aged engineering selection competition
广告电商系统开发功能只订单处理
Basic Introduction for PLSQL
基于数据库实现分布式锁
手搓一个“七夕限定”,用3D Engine 5分钟实现烟花绽放效果
Next -20- 使用自定义样式 (custom style)
【 HMS core 】 【 Media 】 online video editing service 】 【 material can't show, or network anomalies have been Loading state
Android Sqlite3 basic commands
leetcode: 253. How many meeting rooms are required at least
华为云 & 达达,帮有情人“一键送达”
快解析结合千方百剂
指数族分布与最大熵
一看就会的Chromedriver(谷歌浏览器驱动)安装教程