当前位置:网站首页>明明加了唯一索引,为什么还是产生重复数据?
明明加了唯一索引,为什么还是产生重复数据?
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
边栏推荐
- 我爱七夕哈哈哈
- C语言写简单三子棋
- Technology sharing | Description of the electronic fence function in the integrated dispatching system
- 在腾讯,我的试用期总结!
- MVCC实现过程
- 【历史上的今天】8 月 4 日:第一位图灵奖女性得主;NVIDIA 收购 MediaQ;首届网络安全挑战大赛完成
- leetcode: 241. Designing precedence for arithmetic expressions
- 苏秋贵:揭秘绿联科技用5年时间从0做到6亿,如何一枝独秀?
- Nuget 通过 dotnet 命令行发布
- leetcode: 250. Count subtrees of equal value
猜你喜欢
【Today in History】August 4: First female Turing Award winner; NVIDIA acquires MediaQ; first Cybersecurity Challenge completed
Basic Introduction for PLSQL
X射线掠入射聚焦反射镜
【云原生 | 从零开始学Kubernetes】kubernetes之StatefulSet详解
Google plug-in. Download contents file is automatically deleted after solution
Win10无法访问移动硬盘怎么解决
This week to discuss the user experience: Daedalus Nemo to join Ambire, explore the encryption of the ocean
数据链路层-------以太网协议
1401 - Web technology 】 【 introduction to graphical Canvas
Qt的QItemDelegate使用
随机推荐
Hangzhou Electric School Competition (Counter Attack Index)
xampp安装包含的组件有(php,perl,apche,mysql)
Compound Refractive Lenses for X-ray Focusing
This week to discuss the user experience: Daedalus Nemo to join Ambire, explore the encryption of the ocean
企业级优化
手搓一个“七夕限定”,用3D Engine 5分钟实现烟花绽放效果
Almost all known protein structures in the world are open sourced by DeepMind
分布式链路追踪Jaeger + 微服务Pig在Rainbond上的实践分享
Resharper 如何把类里的类移动到其他文件
vim common operation commands
Zheng Qing freshmen school competition and middle-aged engineering selection competition
xpath获取带命名空间节点注意事项
Find My技术|防止你的宠物跑丢,苹果Find My技术可以帮到你
Latex 去掉行号
Oracle 数据库用户创建、重启、导入导出
G.登山小分队(暴力&dfs)
leetcode:241. 为运算表达式设计优先级
C# SolidWorks二次开发---工程图简单版标注孔信息
Technology sharing | Description of the electronic fence function in the integrated dispatching system
Http-Sumggling缓存漏洞分析