当前位置:网站首页>明明加了唯一索引,为什么还是产生重复数据?
明明加了唯一索引,为什么还是产生重复数据?
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边栏推荐
- LeetCode_模拟_中等_498.对角线遍历
- Phasecraft连下两城,助力英国量子技术商业化加速!
- 数据链路层-------以太网协议
- Redis-哨兵模式
- 【剑指offer59】队列的最大值
- 7 天找个 Go 工作,Gopher 要学的条件语句,循环语句 ,第3篇
- uni-app 从零开始-生命周期(二)
- This week to discuss the user experience: Daedalus Nemo to join Ambire, explore the encryption of the ocean
- leetcode: 241. Designing precedence for arithmetic expressions
- 微软表示将向内部网络安全专家共享数据 为企业提供更安全保护
猜你喜欢

X射线掠入射聚焦反射镜

How to automatically renew the token after it expires?

16、学习MySQL 正则表达式

NPDP|作为产品经理,如何快速提升自身业务素养?

输入输出流总结

一看就会的Chromedriver(谷歌浏览器驱动)安装教程

Bluetooth Technology|In the first half of the year, 1.3 million charging piles were added nationwide, and Bluetooth charging piles will become the mainstream of the market

FTP协议抓包-工具wireshark与filezilla

Qt的QItemDelegate使用

《分布式云最佳实践》分论坛,8月11日深圳见
随机推荐
Almost all known protein structures in the world are open sourced by DeepMind
X-ray grazing incidence focusing mirror
数据链路层-------以太网协议
xpath获取带命名空间节点注意事项
【北亚数据恢复】IBM System Storage存储lvm信息丢失数据恢复方案
技术分享| 融合调度系统中的电子围栏功能说明
从-99打造Sentinel高可用集群限流中间件
I/O stream summary
Hangzhou electric the competition team arrangement (ACM)
leetcode:259. 较小的三数之和
本周讨论用户体验:Daedalus 的 Nemo 加入 Ambire,探索加密海洋
leetcode:255 验证前序遍历序列二叉搜索树
Cisco - Small Network Topology (DNS, DHCP, Web Server, Wireless Router)
用于X射线聚焦的复合折射透镜
OAID是什么
苏秋贵:揭秘绿联科技用5年时间从0做到6亿,如何一枝独秀?
B.构造一个简单的数列(贪心)
企业级优化
xampp安装包含的组件有(php,perl,apche,mysql)
什么,你告诉我?作用域也分种类?