当前位置:网站首页>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 引发的‘资源忙’异常报告
边栏推荐
猜你喜欢

C语言(字符串,转义字符,关键字)

EOF的用法——while(scanf(“%d“,&num)!=EOF)

Qt实现单击或双击QTableWidge/View表头进行排序
0基础玩转C语言—初识C语言(下)

208.实现Trie树(字典树与哈希表实现)

Anaconda安装教程

MySql fuzzy query Daquan

How is crawler data collected and organized?

Qt对动态库(*.dll)的封装以及使用

net start mysql MySQL service is starting. MySQL service failed to start.The service did not report any errors.
随机推荐
操作系统面试整理
Qt对动态库(*.dll)的封装以及使用
240.搜索二维矩阵II
SRA数据下载方法总结
np.where()用法
多线程并发服务器
别找了,你要的C语言“数组”在这里
封装Cookie API
Machine Learning - Gradient Descent Optimization - C language implementation
信号量解决生产者消费者问题
navicat连接MySQL报错:1045 - Access denied for user ‘root‘@‘localhost‘ (using password YES)
MySQL database basics (a systematic introduction)
数据操作 / 数据预处理
Numpy 中 np.vstack() 和 np.hstack() 简单解析
Qt实现单击或双击QTableWidge/View表头进行排序
初识C语言
C语言指针(指针数组、数组指针、函数指针、传参、回调函数等)超详细
enumerate() 函数
MySQL 数据库基础知识(系统化一篇入门)
easyexcel使用教程-导出篇