当前位置:网站首页>踩坑了!mysql明明加了唯一索引,还是产生了重复数据
踩坑了!mysql明明加了唯一索引,还是产生了重复数据
2022-08-05 13:14:00 【InfoQ】
前言

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



- 当model_hash字段不为空时,不会产生重复的数据。
- 当model_hash字段为空时,会生成重复的数据。
3.逻辑删除表加唯一索引
delete from product where id=123;
update product set delete_status=1,edit_time=now(3)
where id=123;
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 增加时间戳字段
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;
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分布式锁

6.批量插入数据
for(Product product: list) {
try {
String hash = hash(product);
rLock.lock(hash);
//查询数据
//插入数据
} catch (InterruptedException e) {
log.error(e);
} finally {
rLock.unlock();
}
}
边栏推荐
猜你喜欢

详解 SSL(一):网址栏的小绿锁有什么意义?

做个小程序商城大约多少钱?哪些方面会影响价格?

施耐德电气庞邢健:以软件撬动可持续的未来工业

C进阶-动态内存管理

对比服务器,进行正确配置

115. In-depth explanation of the technical implementation of configuring the local SAP UI5 application to the local Fiori Launchpad

【IC5000教程】-02-使用daqIDEA图形化显示“Array数组”和“struct结构体”类型变量的值变化

Sushi Technology IPO was terminated: annual revenue of 1.87 billion, Shunwei Xiaomi Jinglin Kunzhong is a shareholder

【搜索框】通用测试用例

华朗复读衔接营励志开营!全名师阵容护航 解读高考成功秘钥
随机推荐
【CC3200AI 实验教程2】疯壳·AI语音人脸识别(会议记录仪/人脸打卡机)-系统测试
RK3568+鸿蒙工业平板工业看板方案设计
怎么在微信上做小程序?不懂代码怎么开发微信小程序?
双因子与多因子身份验证有什么区别?
pandas连接oracle数据库并拉取表中数据到dataframe中、筛选当前时间(sysdate)到一分钟之前的所有数据(筛选一分钟之内的数据)
记一次 .NET 某工控自动化控制系统 卡死分析
What is the origin of an overnight flight tracking website?
并发刺客(False Sharing)——并发程序的隐藏杀手
Stack和Queue 栈和队列
C进阶 - 指针进阶
Under the heavy pressure of the epidemic, why is Watsons still profitable in the first half of the year?
为什么设计思维是有用的?
The memory problem is difficult to locate, that's because you don't use ASAN
阿里二面:明明加了唯一索引,为什么还是产生重复数据?
likeshop搭建商城系统,一步到位
C进阶-自定义类型:结构体,枚举,联合
得物客服热线的演进之路
R语言ggplot2可视化:使用ggpubr包的ggparagraph函数可视化文本段落(将指定文本段落可视化出来)、使用ggarrange函数将文本数据和可视化图像组合起来(文本数据在可视化图像下方
高薪程序员&面试题精讲系列133之微服务里的网关有哪些实现方案?你熟悉Gateway网关吗?
第三章 调度系统架构设计之获取集群资源信息