項目高并發的時候很容易出現數據庫插入相同的數據,雖然可以使用唯一索引避免插入相同數據,但是不斷的程序報錯也是我們要避免的。
MySQL中的插入更新
使用 insert ... on duplicate key update ..
語法可以避免上述情況,舉個例子
drop table if exists `test`;
create table `test` (
`id` int(11) not null AUTO_INCREMENT,
`name` varchar(32) not null default '',
`update_ts` timestamp not null default current_timestamp(),
primary key (`id`)
) engine=InnoDB default charset=utf8mb4;
主鍵 id
是天然的唯一索引,我們插入重復數據時會報錯
> INSERT INTO test (id, name) VALUES (1, 'wxnacy');
> INSERT INTO test (id, name) VALUES (1, 'wxnacy');
Error 1062: Duplicate entry '1' for key 'PRIMARY'
查看插入的數據
> SELECT * FROM `test`;
| id | name | update_ts |
|------------------------------------
| 1 | wxnacy | 2019-05-16 22:26:58 |
下面我們來換個語句
> insert into test (id, name) values (1, 'wxnacy') on duplicate key update update_ts = current_timestamp();
> SELECT * FROM `test`;
+----+--------+---------------------+
| id | name | update_ts |
+----+--------+---------------------+
| 1 | wxnacy | 2019-05-16 22:39:49 |
+----+--------+---------------------+
on duplicate key update
前面是正常的插入語句,其后跟著的是當唯一索引沖突時,想要更新的數據。
再換個使用場景,如果我想讓數據庫中用戶名是唯一的,則可以先建立唯一索引,在使用該語法。
> alter table test add unique index_name (name);
> insert into test (name) values ('wenn') on duplicate key update update_ts = current_timestamp();
> SELECT * FROM `test`;
+----+--------+---------------------+
| id | name | update_ts |
+----+--------+---------------------+
| 1 | wxnacy | 2019-05-16 22:49:29 |
| 2 | wenn | 2019-05-16 22:49:49 |
+----+--------+---------------------+
> insert into test (name) values ('wenn') on duplicate key update update_ts = current_timestamp();
> SELECT * FROM `test`;
+----+--------+---------------------+
| id | name | update_ts |
+----+--------+---------------------+
| 1 | wxnacy | 2019-05-16 23:09:12 |
| 2 | wenn | 2019-05-16 23:11:42 |
+----+--------+---------------------+
這樣及保證了避免插入重復數據,同時程序也沒有報錯,我還可以根據 update
的數據來分析問題的根源。
SQLAlchemy 中的存在即更新
如果你有興趣 可以看下官方文檔 INSERT…ON DUPLICATE KEY UPDATE (Upsert)
我們看先下官方文檔的實例代碼
from sqlalchemy.dialects.mysql import insert
insert_stmt = insert(my_table).values(
id='some_existing_id',
data='inserted value')
on_duplicate_key_stmt = insert_stmt.on_duplicate_key_update(
data=insert_stmt.inserted.data,
status='U'
)
conn.execute(on_duplicate_key_stmt)
上面代碼的意思是 當你有一個已經存在的主鍵some_existing_id
的時候,你去執行上面的插入操作的時候 將會執行下面的對應主鍵的更新操作。
官方代碼總是很抽象,我們來個實際的例子吧。
from sqlalchemy.dialects.mysql import insert
db_client = get_db_client()
insert_stmt = insert(table_sa).values(**data)
on_duplicate_key_stmt = insert_stmt.on_duplicate_key_update(
**data
)
await db_client.execute(on_duplicate_key_stmt)
上個代碼其中的data
是一個字典,其中的key
是數據庫對用的字段。意思是當我插入到數據中的時候,當存在重復的唯一鍵的時候,將會直接更新數據。
注意:想要使用上面的方法,我們需要創建一個唯一索引(即使是聯合唯一索引也行)