当前位置:网站首页>mysql处理insert冲突的解决方案
mysql处理insert冲突的解决方案
2022-07-30 05:40:00 【Lucky-boy-kj】
1.采用insert on duplicate key 方法
#展示有多少索引
show index from space_event_warn;
#删除指定表中的指定索引
drop index jkUniqueIndex_group_type on space_event_warn;
#添加唯一联合索引
alter table space_event_warn add unique index jkUniqueIndex_group_type(event_group, event_type);
#insert on duplicate key 测试
insert into space_event_warn(event_type, event_group) values ("asd", "qqq")
on duplicate key
update event_type = "123", event_group = "567";
2.通过检索判断方法
<insert id="saveOrUpdata" parameterType="com.file..InfoMetadata">
<selectKey keyProperty="count" resultType="int" order="BEFORE">
select count(*) from info_metadata where product = #{product} and directory = #{directory}
</selectKey>
<if test="count > 0">
update info_metadata
<set>
<if test="product != null">
product = #{product},
</if>
<if test="size != null">
size = #{size},
</if>
<if test="directory != null ">
directory = #{directory},
</if>
<if test="creationTime != null">
creation_time = #{creationTime},
</if>
<if test="identification != null">
identification = #{identification},
</if>
<if test="productLevel != null ">
product_level = #{productLevel},
</if>
<if test="satellite != null ">
satellite = #{satellite},
</if>
<if test="instrument != null">
instrument = #{instrument},
</if>
<if test="productType != null ">
product_type = #{productType},
</if>
<if test="fileType != null ">
file_type = #{fileType},
</if>
<if test="dataTime != null">
data_time = #{dataTime},
</if>
<if test="path != null">
path = #{path},
</if>
<if test="matchDirFlag != null">
match_dir_flag = #{matchDirFlag},
</if>
<if test="version != null ">
version = #{version},
</if>
<if test="sha1 != null ">
sha1 = #{sha1},
</if>
<if test="suffix != null ">
suffix = #{suffix}
</if>
</set>
where id = #{id}
</if>
<if test="count == 0">
insert into info_metadata(product, size, directory, creation_time, identification, product_level, satellite, instrument, product_type, file_type, data_time, path, match_dir_flag, version, sha1, suffix)
values (#{product}, #{size}, #{directory}, #{creationTime}, #{identification}, #{productLevel}, #{satellite}, #{instrument}, #{productType}, #{fileType}, #{dataTime}, #{path}, #{matchDirFlag}, #{version}, #{sha1}, #{suffix})
</if>
</insert>select for update用法
select * from t for update 会等待行锁释放之后,返回查询结果。
select * from t for update nowait 不等待行锁释放,提示锁冲突,不返回结果
select * from t for update wait 5 等待5秒,若行锁仍未释放,则提示锁冲突,不返回结果
select * from t for update skip locked 查询返回查询结果,但忽略有行锁的记录SELECT…FOR UPDATE 语句的语法如下:
SELECT … FOR UPDATE [OF column_list][WAIT n|NOWAIT][SKIP LOCKED];
其中:
OF 子句用于指定即将更新的列,即锁定行上的特定列。
WAIT 子句指定等待其他用户释放锁的秒数,防止无限期的等待。“使用FOR UPDATE WAIT”子句的优点如下:
1防止无限期地等待被锁定的行;
2允许应用程序中对锁的等待时间进行更多的控制。
3对于交互式应用程序非常有用,因为这些用户不能等待不确定
4 若使用了skip locked,则可以越过锁定的行,不会报告由wait n 引发的‘资源忙’异常报告
边栏推荐
猜你喜欢

50道SQL练习题(刷完直接进大厂)

240.搜索二维矩阵II

MySQL fuzzy query performance optimization

MySQL 用户授权

Navicat cannot connect to mysql super detailed processing method

argparse —— 命令行选项、参数和子命令解析器

navicat连接MySQL报错:1045 - Access denied for user ‘root‘@‘localhost‘ (using password YES)

It is enough for MySQL to have this article (37k words, just like Bojun!!!)

mysql time field is set to current time by default

SRA数据下载方法总结
随机推荐
cJSON开源项目详细解剖
pycharm专业版 配置pytest
【Typescript】学习笔记(三)之接口与泛型的使用
爬虫数据是如何收集和整理的?
It is enough for MySQL to have this article (37k words, just like Bojun!!!)
Qt在QTableWidget、View等表格中添加右击菜单
【Pytorch】torch.manual_seed()、torch.cuda.manual_seed() 解释
Learn FPGA from the underlying structure (6) ---- Distributed RAM (DRAM, Distributed RAM)
4461. Range Partition (Google Kickstart2022 Round C Problem B)
海量号码需要保存,如何才能尽可能少地占用内存?
50道SQL练习题(刷完直接进大厂)
三子棋游戏实现(c语言)
5.6EPOLLONESHOT事件
【线性神经网络】线性回归 / 基础优化方法
字符串(一) 哈希
5.5线程池
P3 元宝第三天的笔记
51.N皇后(回溯法)
240.搜索二维矩阵II
cross_val_score的用法