当前位置:网站首页>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 引发的‘资源忙’异常报告
边栏推荐
- 650.只有两个键的键盘(动态规划)
- G Bus Count (Google Kickstart2014 Round D Problem B) (DAY 89)
- 453.最小操作数使数组元素相等
- strlen和sizeof的区别
- Navicat connection MySQL error: 1045 - Access denied for user 'root'@'localhost' (using password YES)
- SRA数据下载方法总结
- 相对路径和绝对路径的区别
- 留念 · 大学时代最后的系统设计图
- 【Pytorch】torch.manual_seed()、torch.cuda.manual_seed() 解释
- 进程间的通信方式简介
猜你喜欢
随机推荐
ClickHouse data insert, update and delete operations SQL
cross_val_score的用法
flask使用token认证
C语言指针(指针数组、数组指针、函数指针、传参、回调函数等)超详细
torch.optim.Adam()
navicat连接MySQL报错:1045 - Access denied for user ‘root‘@‘localhost‘ (using password YES)
argparse —— 命令行选项、参数和子命令解析器
G Bus Count (Google Kickstart2014 Round D Problem B) (DAY 89)
留念 · 大学时代最后的系统设计图
每日练习------输出一个整数的二进制数、八进制数、十六进制数。
Countdown (Source: Google Kickstart2020 Round C Problem A) (DAY 88)
爬虫数据是如何收集和整理的?
条件变量解决生产者消费者问题
MySQL 灵魂 16 问,你能撑到第几问?
MySQL Soul 16 Questions, how many questions can you last?
P3 元宝第三天的笔记
MySQL 安装报错的解决方法
0基础玩转C语言—初识C语言(上)
navicat新建数据库
MySql fuzzy query Daquan